VBA:区分大小写在if-else语句中的MATCH选项

所以我从这里学到了“Option Compare Text”和“Option Compare Binary”来区分.Match函数的区分大小写。

现在,这个if-else语句显示了我正在尝试做什么(尽pipe有错误):

If dictionaryData.Cells(4, 2).Value = "Yes" Then caseSensitive = True NetworkNameDict.CompareMode = vbBinaryCompare Option Compare Binary Else caseSensitive = False NetworkNameDict.CompareMode = vbTextCompare Option Compare Text End If 

我需要这个if else语句来检查用户是否要区分大小写比较。 “选项”被放置在那里为我的.Match函数工作(稍后在代码中find)。

我明白,“选项”代码必须在顶部input,但我需要这个选项保持dynamic,因为这个选项被赋予给用户在电子表格中设置。

所以我的问题是,有没有办法以某种方式做一个if-else语句中的.Match函数的区分大小写设置?

这两个选项不能在一个模块中使用,所以方法是从两个分离的模块中调用代码,一个使用选项compare text and another使用option compare binary
另一种方法是使用option compare binarylcaseucase比较,如下例所示:

 Option Compare Binary Sub test() Debug.Print TypeName("YES") & " comparing" Debug.Print "YES" = "yes", "case sensitive" Debug.Print LCase("YES") = "yes", "not case sensitive" Debug.Print "YES" = UCase("yes"), "not case sensitive" Debug.Print UCase("Yes") = UCase("yEs"), "not case sensitive" Debug.Print TypeName(1.1) & " vs " & TypeName("1.1") Debug.Print 1.1 = "1.1", "not case sensitive" Debug.Print TypeName(1) & " vs " & TypeName("1") Debug.Print 1 = "1", "not case sensitive" Debug.Print TypeName(10000001) & " vs " & TypeName("10000001") Debug.Print 10000001 = "10000001", "not case sensitive" End Sub 

输出在立即窗口将

 String comparing False case sensitive True not case sensitive True not case sensitive True not case sensitive Double vs String True not case sensitive Integer vs String True not case sensitive Long vs String True not case sensitive 

创build两个单独的模块; 一个用OPTION COMPARE TEXT调用MText ,一个用OPTION COMPARE BINARY调用MBinary ,并根据需要从正确的模块中调用相应的函数。

或者,对于更多面向对象的方法,创build两个类CBinaryCText ,它们实现MATCHtesting的相同接口,并在任何给定的时间实例化您所需的接口。