You are here:

Excel/VBA to List all Possible Combinations of Variables and Variants for each variable

Advertisement


Question
Hi Tom

I'm trying to test unique data scenarios within a set of data

The data has lets say 10 columns and column can have a few variants (eg Yes,No,?,etc..)

Can I setup a macro that lists out the unique scenarios combining the variables and their variants?

As a side question how would I calculate the no. of unique combinations using math?

Thanks in advance for your help

Bob

Answer
Bob,
If you want to know how many combinations you would have, then assume

A 10
B 5
C 7
E 2

so you would multiply the number of variants  10 x 5 x 7 x 2 = 700

You can see with 10 columns this would get big fast.

as far as writing them out with vba, yes you could do this

Sub abc()
Dim v, i As Long, j As Long, k As Long, l As Long, m As Long
Dim n As Long, o As Long, p As Long, q As Long, r As Long
Dim max As Long
ReDim v(1 To 10)
For i = 1 To 10
v(i) = Cells(Rows.Count, i).End(xlUp).Row
Next

max = 0
For i = 1 To 10
If max < v(i) Then
   max = v(i)
End If
Next
max = max + 2

For i = 1 To v(1)
For j = 1 To v(2)
  For k = 1 To v(3)
    For l = 1 To v(4)
      For m = 1 To v(5)
        For n = 1 To v(6)
         For o = 1 To v(7)
         For p = 1 To v(8)
         For q = 1 To v(9)
         For r = 1 To v(1)
         Cells(max, 1) = Cells(i, 1)
         Cells(max, 2) = Cells(j, 2)
         Cells(max, 3) = Cells(k, 3)
         Cells(max, 4) = Cells(l, 4)
         Cells(max, 5) = Cells(m, 5)
         Cells(max, 6) = Cells(n, 6)
         Cells(max, 7) = Cells(o, 7)
         Cells(max, 8) = Cells(p, 8)
         Cells(max, 9) = Cells(q, 9)
         Cells(max, 10) = Cells(r, 10)
         max = max + 1
         
Next r, q, p, o, n, m, l, k, j, i
End Sub

That assumes 10 columns in A:J with variants starting in row 1

--
Regards,
Tom Ogilvy  
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.