简体   繁体   English

不区分大小写的字典

[英]Case in-sensitive dictionary

I have set Dictionary as an object an added several items to that dictionary, however it seems to be case-sensitive.我已将字典设置为一个对象,并向该字典添加了几个项目,但它似乎区分大小写。 Is there anyway I can set the dictionary to recognize different versions?无论如何我可以设置字典来识别不同的版本吗?

My Code:我的代码:

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

So I want to recognize "mike" for MIKE and "Phil" for PHIL etc.所以我想识别 MIKE 的“mike”和 PHIL 等的“Phil”。

Thanks in advance!提前致谢!

Adding onto @Ralph添加到@Ralph

dict.CompareMode = TextCompare

is what I changed the file to.是我将文件更改为的内容。


Some clarifications regarding the comments:关于评论的一些说明:

TextCompare is only available with Early Binding, it is a member of Scripting . TextCompare仅适用于 Early Binding,它是Scripting的成员。
vbTextCompare is always available in VBA. vbTextCompare在 VBA 中始终可用。
Both are = 1.两者都是 = 1。

? Scripting.CompareMethod.TextCompare
 1 
? VBA.VbCompareMethod.vbTextCompare
 1 

Note: you can only set dict.CompareMode if dict is empty, ie you haven't added any members yet.注意:如果 dict 为空,您只能设置dict.CompareMode ,即您还没有添加任何成员。 Otherwise you will get an "Illegal procedure call" error.否则,您将收到“非法过程调用”错误。

I always like to set things straight for all of my coding.我总是喜欢为我的所有编码设置直截了当的东西。 So, all modules and code lying on my sheets or in forms start with the following three lines before writing any additional code.因此,在编写任何其他代码之前,我的工作表或表单中的所有模块和代码都以以下三行开头。

Option Base 0
Option Explicit
Option Compare Text

If I want to have something handled differently in a particular Sub for some reason, then I do so in this particular sub only and do as proposed in the comment above (example):如果由于某种原因我想在特定的Sub以不同的方式处理某些事情,那么我只在这个特定的sub这样做,并按照上面的评论(示例)中的建议进行操作:

dict.CompareMode = BinaryCompare 'if I need a case-sensitive compare in this sub

在此处输入图片说明

Since VBE knows that dict is a Dictionary it can provide propositions for auto-complete.由于 VBE 知道dict是一个字典,因此它可以提供自动完成的命题。 This is only possible with early-binding.这只有通过早期绑定才有可能。 With late binding VBE will not provide any auto-complete propositions.后期绑定 VBE 将不提供任何自动完成命题。 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM