View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Ben McClave Ben McClave is offline
external usenet poster
 
Posts: 173
Default Excel Macro Programing Problem

Hi Mr. Nemo,

I found a code that came close to doing this he http://stackoverflow.com/questions/1...ion-of-a-range.

I have adapted the solution found there to your data (add four rows of data where the value from each row can be one of three potential values). This sub will populate the permutations beginning in cell G6.

I added some lines to also show the values being added or the ranges being added in case you are interested in where the figures came from.

Hope this helps.

Ben

Sub Perumutations()
'Adapted from a post at: _
http://stackoverflow.com/questions/10692653/ _
excel-vba-to-create-every-possible-combination-of-a-range

'This code assumes your data is stored in the range A1:C4 _
with each row being added together and all possible values _
of each row being stored in columns. (i.e. row 1 has three _
possible values, stored in cells A1, B1, and C1)

Dim x As Long
Dim i As Long, j As Long, k As Long, l As Long
Dim lastrow As Long

x = 3 'How many possible values?

Application.ScreenUpdating = False

lastrow = 6 'Permutations to begin in row 6 of columns F & G

For i = 1 To x: For j = 1 To x
For k = 1 To x: For l = 1 To x
Range("G" & lastrow).Value = Cells(1, i).Value + _
Cells(2, j).Value + _
Cells(3, k).Value + _
Cells(4, l).Value
'Uncomment next line to show the calculations
'Range("F" & lastrow).Value = Cells(1, i).Value & "+" & _
Cells(2, j).Value & "+" & _
Cells(3, k).Value & "+" & _
Cells(4, l).Value & "="
'Uncomment next line to show cell references
'Range("F" & lastrow).Value = Cells(1, i).Address & "+" & _
Cells(2, j).Address & "+" & _
Cells(3, k).Address & "+" & _
Cells(4, l).Address & "="
lastrow = lastrow + 1
Next: Next
Next: Next


Application.ScreenUpdating = True
End Sub