Large loops inside small loops or vice-versa

YasserKhalil
PlatinumLounger
Posts: 4911
Joined: 31 Aug 2016, 09:02

Large loops inside small loops or vice-versa

Post by YasserKhalil »

Hello everyone

Sometimes we have to use nested loops, in that case what is the best and faster? To loop large loops first then inside small loops or the vice-versa
To be more cleared, suppose we have 500 rows and we need with each row to loop through 15 columns, so we use this structure
For i = 1 To 500
for ii=1 To 15

Or to use the structure
For ii=1 To 15
For i = 1 To 500

Or both the approaches are the same and no difference.

User avatar
HansV
Administrator
Posts: 78400
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Large loops inside small loops or vice-versa

Post by HansV »

It shouldn't make any difference: in both cases, the inner statements are executed 15 * 500 = 7500 times
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4911
Joined: 31 Aug 2016, 09:02

Re: Large loops inside small loops or vice-versa

Post by YasserKhalil »

Thanks a lot my tutor.