0

I am writing a VBA excel code to find the all different combinations for 2-level factors. Each factor would have 2 possible levels. For example, if i have 3 factors the number of possible combinations of levels would be 2^3 = 8. My goal is to print out the 8 combinations in Excel.

For example, my factors(levels) are Weight(50,60) and height(160,170) unique combinations would be

50,160
50,170
60,160
60,170

with weight in column A and height in column B

My question would be is it possible to write out a VBA code to print out all the possible combinations for factors (ranging from 2-12) and code should be written in excel VBA

Rebec.L
  • 1
  • 1
  • 2
    Since you are writing code, would you please add it to your post and turn it into an actual question? – JvdV Jan 25 '19 at 13:19
  • 1
    And please add more example data (at least 3 or 4 factors) for better understanding. – Pᴇʜ Jan 25 '19 at 13:25
  • 1
    **Start Here:** https://stackoverflow.com/questions/48651400/how-to-list-all-possible-combinations-of-the-values-in-three-columns-in-excel – Gary's Student Jan 25 '19 at 13:31

1 Answers1

0

Here is a tiny example for two factors using nested loops:

Sub Kombination()
    Dim N As Long, M As Long, i As Long, j As Long
    Dim K As Long

    N = Cells(Rows.Count, "A").End(xlUp).Row
    M = Cells(Rows.Count, "B").End(xlUp).Row
    K = 2

    For i = 2 To N
        For j = 2 To M
            Cells(K, 3).Value = Cells(i, 1).Value & "," & Cells(j, 2).Value
            K = K + 1
        Next j
    Next i
End Sub

enter image description here

Once you have determined the number of factors, modify the code to nest at that level.

Gary's Student
  • 95,722
  • 10
  • 59
  • 99