I'd say that your use of "should" would be better termed as "could". Different programmers have different styles and there are so many circumstances with a development project that a "should" for a scripting dictionary is just not an unambiguously definable set of circumstances. Personally I almost never use a scripting dictionary, because it has never made sense for me to programmatically create an object whose purpose is to be eliminated. There are better methods to accomplish the same task. But that's me, you'll find others who disagree and say it works for them, and so the world goes...some prefer vanilla ice cream and others prefer chocolate.When we should use scripting dictionary?
Tom, would you like to explain about:
1. "because it has never made sense for me to programmatically create an object whose purpose is to be eliminated", i dont know about object and eliminate what, keys or item or what
2. There are better methods to accomplish the same task, would you like to mention that task, 1 example is more than enough.
Private Sub CommandButton1_Click()
Application.ScreenUpdating = False
With [a1:c65536]
.Formula = "=int(rand()*1000000)" '
.Value = .Value
End With
Application.ScreenUpdating = True
End Sub
Sub sameinthreecols()
t = Timer
Dim a, n, i, j, c(), p
a = Cells(1).CurrentRegion
n = UBound(a, 1)
ReDim c(1 To n, 1 To 1)
With CreateObject("Scripting.Dictionary")
.CompareMode = 1
For i = 1 To n: .Item(a(i, 1)) = 1: Next i
For j = 2 To 3
For i = 1 To n
If .Exists(a(i, j)) Then
If .Item(a(i, j)) = j - 1 Then
.Item(a(i, j)) = j
If j = 3 Then
p = p + 1
c(p, 1) = a(i, j)
End If
End If
End If
Next i
Next j
If p > 0 Then Cells(1, "d").Resize(p, 1) = c
End With
MsgBox "code took " & Format(Timer - t, "0.00") & " secs"
End Sub
Did you read the posts on that link I provided...even the user of the scripting dictionary alternative said it was not an adequate application in that example.It's a useful Excel tool which I don't think Tom has so far adduced good reasons for not using.
OK Northwolves,
Something like thisBut of course I'd be interested to see your fast one. How fast?Code:Sub sameinthreecols() t = Timer Dim a, n, i, j, c(), p a = Cells(1).CurrentRegion n = UBound(a, 1) ReDim c(1 To n, 1 To 1) With CreateObject("Scripting.Dictionary") .CompareMode = 1 For i = 1 To n: .Item(a(i, 1)) = 1: Next i For j = 2 To 3 For i = 1 To n If .Exists(a(i, j)) Then If .Item(a(i, j)) = j - 1 Then .Item(a(i, j)) = j If j = 3 Then p = p + 1 c(p, 1) = a(i, j) End If End If End If Next i Next j If p > 0 Then Cells(1, "d").Resize(p, 1) = c End With MsgBox "code took " & Format(Timer - t, "0.00") & " secs" End Sub
I think I could do one of at least comparable speed using an approach based on sort, but I tend to generally prefer the Scripting Dictionary.
It's a useful Excel tool which I don't think Tom has so far adduced good reasons for not using.
Sub Dictionary()
Dim arr, i As Long, n As Long, b(1000000) As Boolean, c(1000000) As Boolean, t As Single, d As Object
Application.ScreenUpdating = False
Set d = CreateObject("Scripting.Dictionary")
arr = Range("a1:c65536")
n = 65536
t = Timer
For i = 1 To n
b(arr(i, 1)) = True
Next
For i = 1 To n
If b(arr(i, 2)) Then c(arr(i, 2)) = True
Next
For i = 1 To n
If c(arr(i, 3)) Then d(arr(i, 3)) = 1
Next
[d1].Resize(d.Count, 1) = Application.Transpose(d.Keys)
Application.ScreenUpdating = True
MsgBox "code took " & Format(Timer - t, "0.00") & " secs"
End Sub