Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

Unfortunately, I am having a recurrent problem with the case sensitivity.

For example, when I am using this code :

For i = 11 To lRowB Range("B" & i).Activate myResult = IsNumeric(Application.Match(ActiveCell.Value, manilaListRange, 0)) 

It is gonna compare the value of the active cell to a list of words from my database. Problem is, if in my active cell the word is "miami" or "MIAMI" and only "Miami" is in the database, it won't work...

Other example:

If Range("J6").Value = "tawi" Then Range("J6").Value = "Tawi-Tawi" End If 

Same problem, only the word written with the same case is gonna work.

How can I get rid of this? It's particularly annoying and I can't rewrite my database in every case combination possible!

Thanks in advance !

like image 334
Phalanx Avatar asked Jun 11 '13 02:06

Phalanx


People also ask

Is VBA string comparison case sensitive?

StrComp VBA can perform both case sensitive and case insensitive string comparisons.

How do I turn off case sensitive in Excel?

The solution is to... By default, Excel is not case-sensitive. For example, with "APPLE" in A1, and "apple" in A2, the following formula will return TRUE: = A1 = A2 // returns TRUE To compare text strings in a case-sensitive way, you can use the EXACT function .

Are VBA functions case sensitive?

By default, Excel VBA code is case sensitive and uses what is known as binary comparisons. This means that it sees Cat and cat as two different words.


1 Answers

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.

like image 191
Floris Avatar answered Sep 28 '22 03:09

Floris