In VBA get rid of the case sensitivity when comparing words?

后端 未结 4 1137
清酒与你
清酒与你 2020-12-08 06:58

I am working on an VBA program which would allow the user to type an address and find the location by matching elements of the address with a database.

Unfortunatel

相关标签:
4条回答
  • 2020-12-08 07:28

    There is a statement you can issue at the module level:

    Option Compare Text
    

    This makes all "text comparisons" case insensitive. This means the following code will show the message "this is true":

    Option Compare Text
    
    Sub testCase()
      If "UPPERcase" = "upperCASE" Then
        MsgBox "this is true: option Compare Text has been set!"
      End If
    End Sub
    

    See for example http://www.ozgrid.com/VBA/vba-case-sensitive.htm . I'm not sure it will completely solve the problem for all instances (such as the Application.Match function) but it will take care of all the if a=b statements. As for Application.Match - you may want to convert the arguments to either upper case or lower case using the LCase function.

    0 讨论(0)
  • 2020-12-08 07:28

    It is a bit of hack but will do the task.

    Function equalsIgnoreCase(str1 As String, str2 As String) As Boolean
        equalsIgnoreCase = LCase(str1) = LCase(str2)
    End Function
    
    0 讨论(0)
  • 2020-12-08 07:29

    You can convert both the values to lower case and compare.

    Here is an example:

    If LCase(Range("J6").Value) = LCase("Tawi") Then
       Range("J6").Value = "Tawi-Tawi"
    End If
    
    0 讨论(0)
  • 2020-12-08 07:32

    If the list to compare against is large, (ie the manilaListRange range in the example above), it is a smart move to use the match function. It avoids the use of a loop which could slow down the procedure. If you can ensure that the manilaListRange is all upper or lower case then this seems to be the best option to me. It is quick to apply 'UCase' or 'LCase' as you do your match.

    If you did not have control over the ManilaListRange then you might have to resort to looping through this range in which case there are many ways to compare 'search', 'Instr', 'replace' etc.

    0 讨论(0)
提交回复
热议问题