Combinatorial Arrays in VBA

pdevito3

Board Regular
Joined
Dec 17, 2013
Messages
246
Hey everyone, I've got a brain twister here that I could get another set of eyes on.

Goal: I essentially want to put all of the combinations from a set of values in one array into a second array.

So I have a one array with my values:

valueArr(3)
oneArr(0) = A
oneArr(1) = B
oneArr(2) = C
oneArr(3) = D

I want to put all of the combinations of these value into a second array, let's say combArr(5,1) -- arrays start at 0, so 5 because there are 6 total combinations and 1 because I only care about a length of 2-- so:

combArr LocationValuevalueArray Location
combArr(0,0)AvalueArray(0)
combArr(0,1)BvalueArray(1)
combArr(1,0)AvalueArray(0)
combArr(1,1)CvalueArray(2)
combArr(2,0)AvalueArray(0)
combArr(2,1)DvalueArray(3)
combArr(3,0)BvalueArray(1)
combArr(3,1)CvalueArray(2)
combArr(4,0)BvalueArray(1)
combArr(4,1)DvalueArray(3)
combArr(5,0)CvalueArray(2)
combArr(5,1)DvalueArray(3)

<tbody>
</tbody>


So essentially the first dimension of the combArr() is going 0x3, 1x2, 2x1 and the second dimension keeps going 1,2,3, but it after the first dimension number changes it starts one nuber higher [1,2,3 (1st dim change) 2,3 (1st dim change) 3].

Here's a crack at starting it, but I am hitting a wall to finish it. Any help is greatly appreciated.

Code:
Sub combos()

' I am setting up the value array in a form before this, so value array length is established already at this point
' in this case, lets stick with the above example vaule array length -- valArrLength --is 3 (0,1,2,3,4).

Dim totalCombinations
Dim fixedLength 'for the length of the combination
Dim combArr()

'valArrLength = 3 and was established on earlier form
fixedLength = 2 'just need 2-way combinations
totalCombinations = Application.WorksheetFunction.Combin(valArrLength+1, fixedLength) '6 in this case
Redim combArr(totalCombinations -1,fixedLength-1) '-1 because arrays start at 0

' the magical nested for and if loops goes here... I think it is going to be dynamic values that get a +1 after a loop to bump them up. Regardless, I'm having trouble with it. 


' also, I know I can make a min and max length and have that on the outside of the loop in place of fixed length to get a result for
' multiple lengths, but I wanted to simplify this as much as possible




End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.

Forum statistics

Threads
1,215,949
Messages
6,127,880
Members
449,411
Latest member
AppellatePerson

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top