I understand that VBA can't dynamically refer to variable names, such as b and i in the following code, and interpret it to get the value of b1 as described here.
But I was wondering if there is a more elegant way to accomplish what I am doing below?
Basically, I want to get all combinations of 7 columns.
Sub combination()
Dim w(1 To 7) As Integer
Dim b(1 To 7) As Integer
'get values
For i = 1 To 7
w(i) = Cells(6, i + 1).Value
Next i
newline = 8
total = 0
For b1 = 0 To 1
For b2 = 0 To 1
For b3 = 0 To 1
For b4 = 0 To 1
For b5 = 0 To 1
For b6 = 0 To 1
For b7 = 0 To 1
b(1) = b1
b(2) = b2
b(3) = b3
b(4) = b4
b(5) = b5
b(6) = b6
b(7) = b7
For i = 1 To 7
total = total + b(i) * w(i)
Next i
Cells(newline, 1).Value = total
For i = 1 To 7
Cells(newline, i + 1).Value = b(i)
Next i
total = 0
newline = newline + 1
Next b7
Next b6
Next b5
Next b4
Next b3
Next b2
Next b1
End Sub
CallByName. Also you can load aScriptControland run any vbs script, or part of a script, you want. But your question usually indicates a poor solution (or a lazy solution sometimes) to a problem. What is your problem?