VBA – Compute All Combinations of a Given Set

Yesterday I received an email, refering to some 2 old articles in VitoshAcademy for nested loops without recursion:

As far as the person from the email was taking a look at the code and was struggling, wanting to perform the combinations of the set [1,2,3,4,5,6] with 4 samples, I thought it would be interesting, if I give it a try, rewriting the old code I was using almost 5 years ago.

It was fun to see how I was thinking about programming back then and that I actually have changed slightly after. Anyway, while rewriting the code I noticed, that there are 2 main differences between permutations and combinations:

  • In combinations there are no values repeated;
  • In combinations every value is bigger than the previous one;

The latter is actually not true in general, but if we follow this rule, we are forced to skip adding “1243” after having already written “1234”. So, it is a kind of logic I decided to use, as far as sorting and implementing to a set was requiring more words to explain.

Long story short, implementing the old code I wrote 5 years ago, I have added 2 new conditions:

And it actually brought me to the desired result:

And this is the code, which probably looks better than the one I have written in 2015:

Actually, there are plenty of faster ways to make it happen, but my idea was to rewrite the old code with some new conditions. A quick good look for the faster ways – https://stackoverflow.com/questions/127704/algorithm-to-return-all-combinations-of-k-elements-from-n

A picture from Rila mountain, Bulgaria. Taken by me.

Hope you are enjoying it. 🙂

Tagged with: , , , , ,