Monday, 26 August 2019

microsoft excel - how to generate all possible 3 digit combination of the numbers 0-6


I'm using Excel and am trying to generate a table (3 columns) that presents all possible combinations of the numbers 0,1,2,3,4,5, and 6. Digits are allowed to be repeated (example, 2-2-5). To do that manually it will take forever and frankly speaking, I'm not an expert here and was not able to solve this by my own.


In other words, each row would have three digits each placed on a separate cell; but three of them represent one possible combination of the digits 0-6. I need to have all the possible combinations. I have been looking allot for this but unfortunately couldn't.



Answer



This small macro will get your data:


Sub qwerty()
For i = 0 To 6
For j = 0 To 6
For k = 0 To 6
LL = LL + 1
Cells(LL, 1) = i
Cells(LL, 2) = j
Cells(LL, 3) = k
Next k
Next j
Next i
End Sub

Macros are very easy to install and use:



  1. ALT-F11 brings up the VBE window

  2. ALT-I ALT-M opens a fresh module

  3. paste the stuff in and close the VBE window


If you save the workbook, the macro will be saved with it. If you are using a version of Excel later then 2003, you must save the file as .xlsm rather than .xlsx


To remove the macro:



  1. bring up the VBE window as above

  2. clear the code out

  3. close the VBE window


To use the macro from Excel:



  1. ALT-F8

  2. Select the macro

  3. Touch RUN


To learn more about macros in general, see:


http://www.mvps.org/dmcritchie/excel/getstarted.htm


and


http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx


Macros must be enabled for this to work!


No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...