vba excel宏不区分大小写

时间:2017-03-22 16:49:39

标签: vba excel-vba excel

我有这个代码,我需要一些帮助,使它不区分大小写。现在serch区分大小写,我有一些excel表包含大写和低级的数据。  感谢

Sub FormatSelection()

    Dim cl As Range
    Dim SearchText As String
    Dim StartPos As Integer
    Dim EndPos As Integer
    Dim TestPos As Integer
    Dim TotalLen As Integer

    On Error Resume Next
    Application.DisplayAlerts = False
    SearchText = Application.InputBox _
    (Prompt:="Enter string.", Title:="Which string to format?", Type:=2)
    On Error GoTo 0
    Application.DisplayAlerts = True
    If SearchText = "" Then
        Exit Sub
    Else
        For Each cl In Selection
          TotalLen = Len(SearchText)
          StartPos = InStr(cl, SearchText)
          TestPos = 0
          Do While StartPos > TestPos
            With cl.Characters(StartPos, TotalLen).Font
              .FontStyle = "Bold"
              .ColorIndex = 3
            End With
            EndPos = StartPos + TotalLen
            TestPos = TestPos + EndPos
            StartPos = InStr(TestPos, cl, SearchText, vbTextCompare)
          Loop
        Next cl
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

更改此一行

StartPos = InStr(Ucase(cl),Ucase(SearchText))