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:

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]combArr Location[/TD]
[TD]Value[/TD]
[TD]valueArray Location[/TD]
[/TR]
[TR]
[TD]combArr(0,0)[/TD]
[TD]A[/TD]
[TD]valueArray(0)[/TD]
[/TR]
[TR]
[TD]combArr(0,1)[/TD]
[TD]B[/TD]
[TD]valueArray(1)[/TD]
[/TR]
[TR]
[TD]combArr(1,0)[/TD]
[TD]A[/TD]
[TD]valueArray(0)[/TD]
[/TR]
[TR]
[TD]combArr(1,1)[/TD]
[TD]C[/TD]
[TD]valueArray(2)[/TD]
[/TR]
[TR]
[TD]combArr(2,0)[/TD]
[TD]A[/TD]
[TD]valueArray(0)[/TD]
[/TR]
[TR]
[TD]combArr(2,1)[/TD]
[TD]D[/TD]
[TD]valueArray(3)[/TD]
[/TR]
[TR]
[TD]combArr(3,0)[/TD]
[TD]B[/TD]
[TD]valueArray(1)[/TD]
[/TR]
[TR]
[TD]combArr(3,1)[/TD]
[TD]C[/TD]
[TD]valueArray(2)[/TD]
[/TR]
[TR]
[TD]combArr(4,0)[/TD]
[TD]B[/TD]
[TD]valueArray(1)[/TD]
[/TR]
[TR]
[TD]combArr(4,1)[/TD]
[TD]D[/TD]
[TD]valueArray(3)[/TD]
[/TR]
[TR]
[TD]combArr(5,0)[/TD]
[TD]C[/TD]
[TD]valueArray(2)[/TD]
[/TR]
[TR]
[TD]combArr(5,1)[/TD]
[TD]D[/TD]
[TD]valueArray(3)[/TD]
[/TR]
</tbody>[/TABLE]


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

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

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