Hi Andrew
Welcome to the board
You want to calculate the Power Set of a Set.
You can try this code:
Test: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
- Write a, b, c, d in A1:A4
- run PowerSet
A B C D E F G 1 a 2 b a 3 c b 4 d c 5 d 6 a b 7 a c 8 a d 9 b c 10 b d 11 c d 12 a b c 13 a b d 14 a c d 15 b c d 16 a b c d 17 [Book1]Sheet1


LinkBack URL
About LinkBacks



Reply With Quote


Bookmarks