Is there a more efficient loop for combinations?

Wook

Board Regular
Joined
Apr 29, 2003
Messages
78
Hi,
Hoping someone might be able to improve my code! I've got a bit of code that works, but is clearly very inefficient:

For UserTypeA = 0 To Total_Users
For UserTypeB = 0 To Total_Users
For UserTypeC = 0 To Total_Users
If UserTypeA + UserTypeB + UserTypeC = Total_Users Then
'do something
End If
Next UserTypeC
Next UserTypeB
Next UserTypeA

Effectively it's creating all the different combinations for a given number of user based on 3 different types.
This works but is very slow at high number of users as most of the looping is unnecessary as doesn't add up to total number of users.

Any suggestions?

Thanks, Wook
 
Last edited:

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
You could improve it with some exits - i will think on it.
 
Last edited:
Upvote 0
Try this:

Code:
For UserTypeA = 0 To Total_Users
If UserTypeA + UserTypeB + UserTypeC > Total_Users Then Exit For
For UserTypeB = 0 To Total_Users
If UserTypeA + UserTypeB + UserTypeC > Total_Users Then Exit For
For UserTypeC = 0 To Total_Users
If UserTypeA + UserTypeB + UserTypeC > Total_Users Then Exit For
If UserTypeA + UserTypeB + UserTypeC = Total_Users Then
' do something here
End If
Next UserTypeC
UserTypeC = 0
Next UserTypeB
UserTypeB = 0
Next UserTypeA
 
Upvote 0
Thanks jmacleary!
Agreed, I think this would be more efficient, basically you are removing any loops once they exceed the total.. Neat..
So now the only inefficient loops are the ones that are less than the total. Ill give it a go!
Cheers
 
Upvote 0
I think that this will catch all combinations but without any If statements

Code:
For UserTypeA = 0 To Total_Users
    For UserTypeB = 0 To Total_Users - UserTypeA
        For UserTypeC = 0 To Total_Users - (UserTypeA + UserTypeB)
 
            'do something

        Next UserTypeC
    Next UserTypeB
Next UserTypeA
 
Upvote 0
What have you got in the
'do something
because this is often the cause of slow vba. If you are accessing the worksheet as part of the "do something" this will make the routine very slow. if you load the worksheet into a variant array instead of accessing the worksheet it would speed up your routine by a factor of about 1000 without bothering about what happens about the triple loop
 
Upvote 0
mikerickson, thanks that works too, and agreed more tidy code without the ifs.

Offthehip, I had already moved all the calculation work into VBA for this very reason, but realise whilst not writing, I was still reading from the spreadsheet in the loop, sure enough moving it all into variants worked a dream!

All in all dropped the macro run time from 2 hours+ to less than 1 minute!
I love this forum, thanks all!

Cheers, Wook
 
Upvote 0
mikerickson, thanks that works too, and agreed more tidy code without the ifs....
Not only tidy code, but If statements take time to execute (my feeling is that If branching (either true or false) is one of the more time consuming VBA things). Getting them outside of loops is one good way to speed things up.
 
Upvote 0
Hi both - just for fun I did a very quick benchmark of the if-less v iffy solutions - there is approximately a factor of 3 difference in favour of the if-less solution - kudos to mikerickson.
 
Upvote 0

Forum statistics

Threads
1,215,053
Messages
6,122,888
Members
449,097
Latest member
dbomb1414

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