+ Reply to Thread
Results 1 to 14 of 14

Which is faster: For-loop or For Each-loop with integer?

  1. #1
    Registered User
    Join Date
    08-10-2020
    Location
    Netherlands
    MS-Off Ver
    2019
    Posts
    81

    Lightbulb Which is faster: For-loop or For Each-loop with integer?

    I often find posts about how using the For Each-loop tends to be faster then using the For-loop.
    But what if I need an incremental integer? Then which loop is faster?
    I couldn't find it on Google so I thought I asked around here ;-)
    I mean to use it with Arrays.

    So when I need an incremental integer, which Loop is faster?
    This one:
    Please Login or Register  to view this content.
    or this one:
    Please Login or Register  to view this content.

  2. #2
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,903

    Re: Which is faster: For-loop or For Each-loop with integer?

    For Each is generally slower with arrays.
    Rory

  3. #3
    Registered User
    Join Date
    08-10-2020
    Location
    Netherlands
    MS-Off Ver
    2019
    Posts
    81

    Re: Which is faster: For-loop or For Each-loop with integer?

    Really? Didn't expect that, thanks!

  4. #4
    Registered User
    Join Date
    04-19-2021
    Location
    Dublin, Ireland
    MS-Off Ver
    O365, Win10 and Mac
    Posts
    47

    Re: Which is faster: For-loop or For Each-loop with integer?

    Depends on what you are looping through. There is a good review at excelmacromastery.com/vba-for-loop

  5. #5
    Registered User
    Join Date
    08-10-2020
    Location
    Netherlands
    MS-Off Ver
    2019
    Posts
    81

    Re: Which is faster: For-loop or For Each-loop with integer?

    Quote Originally Posted by Corbett View Post
    Depends on what you are looping through. There is a good review at excelmacromastery.com/vba-for-loop
    I'll look into that, thanks!

  6. #6
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,712

    Re: Which is faster: For-loop or For Each-loop with integer?

    Not entirely sure if my example is correct, but based on this I am getting lower times with For Next loop ...
    Please Login or Register  to view this content.
    If I was able to help, you can thank me by clicking the * Add Reputation under my user name

  7. #7
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Which is faster: For-loop or For Each-loop with integer?

    Hint: Don't declare numbers as Integer - declare them as Long. With VBA, numbers declared as Integer undergo a Long conversion, which adds processing overhead. See: https://docs.microsoft.com/en-us/pre...6(v=office.10)
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  8. #8
    Registered User
    Join Date
    08-10-2020
    Location
    Netherlands
    MS-Off Ver
    2019
    Posts
    81

    Re: Which is faster: For-loop or For Each-loop with integer?

    Quote Originally Posted by macropod View Post
    Hint: Don't declare numbers as Integer - declare them as Long. With VBA, numbers declared as Integer undergo a Long conversion, which adds processing overhead. See: https://docs.microsoft.com/en-us/pre...6(v=office.10)
    Good point! At work we are using Excel 2013 though, so does it apply to that version too....?

  9. #9
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Which is faster: For-loop or For Each-loop with integer?

    It applies to VBA generally.

  10. #10
    Registered User
    Join Date
    08-10-2020
    Location
    Netherlands
    MS-Off Ver
    2019
    Posts
    81

    Re: Which is faster: For-loop or For Each-loop with integer?

    Quote Originally Posted by macropod View Post
    It applies to VBA generally.
    Got it. I'll be off replacing all my Integers with Longs then
    Thanks for the heads up!

  11. #11
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,903

    Re: Which is faster: For-loop or For Each-loop with integer?

    Just note that in spite of what it suggests at that site, For Each is not always faster.

  12. #12
    Registered User
    Join Date
    08-10-2020
    Location
    Netherlands
    MS-Off Ver
    2019
    Posts
    81

    Re: Which is faster: For-loop or For Each-loop with integer?

    thanks!

  13. #13
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Which is faster: For-loop or For Each-loop with integer?

    Quote Originally Posted by Rezzy777 View Post
    I'll be off replacing all my Integers with Longs then
    Less typing, too!

  14. #14
    Registered User
    Join Date
    08-10-2020
    Location
    Netherlands
    MS-Off Ver
    2019
    Posts
    81

    Re: Which is faster: For-loop or For Each-loop with integer?

    Quote Originally Posted by macropod View Post
    Less typing, too!
    Hahaha true!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Is there a faster way to run this For Next Loop?
    By JeffGrant in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-10-2021, 11:33 PM
  2. VBA Loop Until cell = integer
    By Koehlr2 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 04-07-2020, 08:47 PM
  3. [SOLVED] Loop on the Row as integer, getting integer into a range
    By Fean in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-20-2018, 07:16 AM
  4. [SOLVED] For Each Loop (Make it Faster)
    By ExcelQuestion in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 10-03-2015, 11:36 PM
  5. [SOLVED] faster way to loop
    By mattress58 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-22-2015, 12:09 PM
  6. Dim integer in For loop does not work, why?
    By 123wc in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 12-30-2011, 12:48 PM
  7. [SOLVED] Faster For-Next Loop?
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-07-2005, 06:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1