Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

Thanks in advance!

like image 322
Tony Avatar asked Jul 24 '15 08:07

Tony


2 Answers

Adding onto @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.
vbTextCompare is always available in VBA.
Both are = 1.

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

Note: you can only set dict.CompareMode if dict is empty, i.e. you haven't added any members yet. Otherwise you will get an "Illegal procedure call" error.

like image 147
John M Avatar answered Oct 08 '22 18:10

John M


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):

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

enter image description here

Since VBE knows that dict is a Dictionary it can provide propositions for auto-complete. This is only possible with early-binding. With late binding VBE will not provide any auto-complete propositions. enter image description here

like image 25
Ralph Avatar answered Oct 08 '22 18:10

Ralph