Looping for speed

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,832
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I read that purely for speed purposes, you should use a For Each loop when looping through a collection.

For arrays, use a For Next loop.

What about for a dictionary? Which is faster?

Thanks
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Which type of loop you do doens't make that much difference, the commonest reason for slow loops is multiple access to the worksheet in a loop. See my signature
 
Upvote 0
You can't use a For Each loop on a dictionary. You can only access the members through the key.
 
Upvote 0
@Kyle123
Not sure if I've misunderstood what you're saying, but you can use a for each loop on dictionary items.
Along the lines of
Code:
      For Each Itm In Dic.Items
         Ws1.Range("A" & Itm.Row).EntireRow.Insert
         Itm.EntireRow.Copy Ws1.Range("A" & Itm.Row)
      Next Itm
 
Upvote 0
@Fluff

Items returns an array, your code is simply looping through the array returned by the items property.


It's shorthand for:

Rich (BB code):
Sub tes()

    Dim dic As Object
    Set dic = CreateObject("scripting.dictionary")
    
    dic.Add "a", 1
    dic.Add "b", 2
    dic.Add "c", 3
    
    Dim items() As Variant
    items = dic.items
    
    For Each itm In items
        Debug.Print itm
    Next itm

End Sub

So a dictionary is not like a collection, one cannot enumerate its values as one can in a collection
 
Last edited:
Upvote 0
@Kyle123
Not sure if I've misunderstood what you're saying, but you can use a for each loop on dictionary items.
Along the lines of
Code:
      For Each Itm In Dic.Items
         Ws1.Range("A" & Itm.Row).EntireRow.Insert
         Itm.EntireRow.Copy Ws1.Range("A" & Itm.Row)
      Next Itm

@Fluff
Items returns an array, your code is simply looping through the array returned by the items property.

It's shorthand for:
Code:
Sub tes()
    Dim dic As Object
    Set dic = CreateObject("scripting.dictionary")
    
    dic.Add "a", 1
    dic.Add "b", 2
    dic.Add "c", 3
    
    Dim items() As Variant
    items = dic.items
    
    For Each itm In items
        Debug.Print itm
    Next itm
End Sub
So a dictionary is not like a collection, one cannot enumerate its values as one can in a collection


Gents, I'm confused!

This code is taken from another thread I posted:

Code:
https://www.mrexcel.com/forum/excel-questions/1055806-pass-more-than-one-argument.html

Is this NOT using a For Each loop, looping through a dictionary?

Code:
Dim DIC As Scripting.Dictionary
    Set DIC = New Scripting.Dictionary
    
    DIC.Add Key:="Apple", Item:="Large"
    DIC.Add Key:="Orange", Item:="Small"
    
    Dim DICElement As Variant
    
    For Each DICElement In DIC
    
        Call SomeSub(Arg1:=DICElement, Arg2:=DIC.Item(DICElement))
    
    Next DICElement
 
Last edited:
Upvote 0
No, it's not. What you have written is shorthand for:

Rich (BB code):
    Dim DIC As Scripting.Dictionary
    Set DIC = New Scripting.Dictionary
    
    Dim keys() As Variant
    
    DIC.Add Key:="Apple", Item:="Large"
    DIC.Add Key:="Orange", Item:="Small"
    
    keys = DIC.keys
    
    Dim DICElement As Variant
    
    For Each DICElement In keys
    
        Call somesub(Arg1:=DICElement, Arg2:=DIC.Item(DICElement))
    
    Next DICElement

The Keys is implicit as it's the default member. So your code is looping through the array of keys returned by the key property.

You are then accessing the dictionary's values using the key.
 
Upvote 0
No, it's not. What you have written is shorthand for:

Rich (BB code):
    Dim DIC As Scripting.Dictionary
    Set DIC = New Scripting.Dictionary
    
    Dim keys() As Variant
    
    DIC.Add Key:="Apple", Item:="Large"
    DIC.Add Key:="Orange", Item:="Small"
    
    keys = DIC.keys
    
    Dim DICElement As Variant
    
    For Each DICElement In keys
    
        Call somesub(Arg1:=DICElement, Arg2:=DIC.Item(DICElement))
    
    Next DICElement

The Keys is implicit as it's the default member. So your code is looping through the array of keys returned by the key property.

You are then accessing the dictionary's values using the key.


Thanks for the explanation.
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,427
Members
448,961
Latest member
nzskater

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top