Hi Andrew
Welcome to the board
You want to calculate the Power Set of a Set.
You can try this code:
Code:
Option Explicit
' PGC Oct 2007
' Calculates a Power Set
' Set in A1, down. Result in C1, down and accross. Clears C:Z.
Sub PowerSet()
Dim vElements As Variant, vresult As Variant
Dim lRow As Long, i As Long
vElements = Application.Transpose(Range("A1", Range("A1").End(xlDown)))
Columns("C:Z").Clear
lRow = 1
For i = 1 To UBound(vElements)
ReDim vresult(1 To i)
Call CombinationsNP(vElements, i, vresult, lRow, 1, 1)
Next i
End Sub
Sub CombinationsNP(vElements As Variant, p As Long, vresult As Variant, lRow As Long, iElement As Integer, iIndex As Integer)
Dim i As Long
For i = iElement To UBound(vElements)
vresult(iIndex) = vElements(i)
If iIndex = p Then
lRow = lRow + 1
Range("C" & lRow).Resize(, p) = vresult
Else
Call CombinationsNP(vElements, p, vresult, lRow, i + 1, iIndex + 1)
End If
Next i
End Sub
Test:
- Write a, b, c, d in A1:A4
- run PowerSet
[TABLE="width: 2"]
<tbody>[TR]
[TH][/TH]
[TH="width: 30, align: center"]A[/TH]
[TH="width: 30, align: center"]B[/TH]
[TH="width: 30, align: center"]C[/TH]
[TH="width: 30, align: center"]D[/TH]
[TH="width: 30, align: center"]E[/TH]
[TH="width: 30, align: center"]F[/TH]
[TH="width: 30, align: center"]G[/TH]
[/TR]
[TR]
[TD="align: center"]
1[/TD]
[TD="align: left"]a[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
2[/TD]
[TD="align: left"]b[/TD]
[TD="align: right"][/TD]
[TD="align: left"]a[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
3[/TD]
[TD="align: left"]c[/TD]
[TD="align: right"][/TD]
[TD="align: left"]b[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
4[/TD]
[TD="align: left"]d[/TD]
[TD="align: right"][/TD]
[TD="align: left"]c[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
5[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: left"]d[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
6[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: left"]a[/TD]
[TD="align: left"]b[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
7[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: left"]a[/TD]
[TD="align: left"]c[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
8[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: left"]a[/TD]
[TD="align: left"]d[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
9[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: left"]b[/TD]
[TD="align: left"]c[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
10[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: left"]b[/TD]
[TD="align: left"]d[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
11[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: left"]c[/TD]
[TD="align: left"]d[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
12[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: left"]a[/TD]
[TD="align: left"]b[/TD]
[TD="align: left"]c[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
13[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: left"]a[/TD]
[TD="align: left"]b[/TD]
[TD="align: left"]d[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
14[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: left"]a[/TD]
[TD="align: left"]c[/TD]
[TD="align: left"]d[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
15[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: left"]b[/TD]
[TD="align: left"]c[/TD]
[TD="align: left"]d[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
16[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: left"]a[/TD]
[TD="align: left"]b[/TD]
[TD="align: left"]c[/TD]
[TD="align: left"]d[/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
17[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="colspan: 8"][Book1]Sheet1[/TD]
[/TR]
</tbody>[/TABLE]