【发布时间】:2018-12-30 20:51:10
【问题描述】:
我已将 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
所以我想识别 MIKE 的“mike”和 PHIL 的“Phil”等。
提前致谢!
【问题讨论】:
-
您可以使用 lcase() msdn.microsoft.com/en-us/library/office/gg264497.aspx来添加和比较元素
-
在创建 scripting.dictionary 对象后添加一行,如
Dict.CompareMode = vbTextCompare。见CompareMode property。 -
你应该试试@Jeeped的方法。
标签: vba excel dictionary