不区分大小写的字典

Ton*_*ony 5 excel vba dictionary excel-vba

我已将Dictionary设置为对象,并向该字典添加了几项,但是似乎区分大小写。无论如何,我可以设置字典来识别不同的版本吗?

我的代码:

Sub Test()

Dim sheet1 As String
Dim Dict As Object
Dim c As Range

Sheet1= "TEST"
Set Dict = CreateObject("Scripting.Dictionary")

Dict.Add "MIKE", 0
Dict.Add "PHIL", 0
Dict.Add "Joe", 0

For Each c In ActiveWorkbook.Worksheets(Sheet1).UsedRange
If Dict.Exists(ActiveWorkbook.Worksheets(Sheet1).Cells(c.Row, c.Column).Value) Then
        Dict(ActiveWorkbook.Worksheets(Sheet1).Cells(c.Row, c.Column).Value) = Dict(ActiveWorkbook.Worksheets(Sheet1).Cells(c.Row, c.Column).Value) + 1
End If
Next

Sheet1.Cells(25, 3) = Dict("MIKE")
Sheet1.Cells(25, 3) = Dict("PHIL")
Sheet1.Cells(25, 3) = Dict("Joe")

Set Dict = Nothing

End Sub
Run Code Online (Sandbox Code Playgroud)

因此,我想识别MIKE的“迈克”和PHIL等的“菲尔”。

提前致谢!

Joh*_*n M 5

添加到@Ralph

dict.CompareMode = TextCompare
Run Code Online (Sandbox Code Playgroud)

是我将文件更改为的内容。


关于评论的一些说明:

TextCompare仅适用于早期绑定,它是Scripting.
vbTextCompare在 VBA 中始终可用。
两者都是 = 1。

? Scripting.CompareMethod.TextCompare
 1 
? VBA.VbCompareMethod.vbTextCompare
 1 
Run Code Online (Sandbox Code Playgroud)

注意:您只能dict.CompareMode在dict为空时进行设置,即您还没有添加任何成员。否则,您将收到“非法过程调用”错误。