vba 在VBA中比较单词时摆脱区分大小写?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17035660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 15:50:09  来源:igfitidea点击:

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

excelvbaexcel-vbacase-sensitive

提问by Phalanx

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.

我正在开发一个 VBA 程序,该程序允许用户键入地址并通过将地址的元素与数据库进行匹配来查找位置。

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...

它会将活动单元格的值与我的数据库中的单词列表进行比较。问题是,如果在我的活动单元格中,单词是“miami”或“MIAMI”,而数据库中只有“Miami”,则它不起作用...

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 !

提前致谢 !

回答by Floris

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.Matchfunction) but it will take care of all the if a=bstatements. As for Application.Match- you may want to convert the arguments to either upper case or lower case using the LCasefunction.

参见例如http://www.ozgrid.com/VBA/vba-case-sensitive.htm。我不确定它是否会完全解决所有实例(例如Application.Match函数)的问题,但它会处理所有if a=b语句。至于Application.Match- 您可能希望使用该LCase函数将参数转换为大写或小写。

回答by Vasanth

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

回答by dra_red

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.

如果要比较的列表很大(即上例中的 manilaListRange 范围),则使用 match 函数是明智之举。它避免使用可能减慢过程的循环。如果您可以确保 manilaListRange 全部为大写或小写,那么这对我来说似乎是最好的选择。在进行比赛时可以快速应用“UCase”或“LCase”。

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.

如果您无法控制ManilaListRange,那么您可能不得不求助于遍历这个范围,在这种情况下,有很多方法可以比较“搜索”、“指令”、“替换”等。

回答by MeLine

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