Question : Combination generator

HI

I want to generate all the possible combinations of numbers from a given entry. Whats different here it
the set size can be 1 char to 4 chars. No repeats are allowed,

eg
ABCD

would generate the following.

I should get 2^n - 1, in this case 2^4 - 1 = 15 results

A
AB
ABC
ABCD
B
BC
BCD
C
CD
D
AC
ACD
ABD
AD
BD

is 15

Whats the best VB/VBA code to create this

Thanks

Answer : Combination generator

Hi Henry,

please try this code. It will output to Immediate window, I used Word VBA.

Alex
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
Sub Combinations()
Const MYSET As String = "A,B,C,D,E,F"   'any values delimited by ","
Dim aElems() As String, aNibs(7) As Byte, i As Integer, j As Integer, nElems As Integer, s As String

aElems() = Split(MYSET, ",")
nElems = UBound(aElems)
If nElems > 7 Then MsgBox "Too many elements: " & nElems + 1: Exit Sub

aNibs(0) = 1: aNibs(1) = 2: aNibs(2) = 4: aNibs(3) = 8: aNibs(4) = 16: aNibs(5) = 32: aNibs(6) = 64: aNibs(7) = 128
For i = 1 To 2 ^ (nElems + 1) - 1
    s = ""
    For j = 0 To nElems
        If i And aNibs(j) Then s = s & aElems(j)
    Next
    Debug.Print s
Next
End Sub
Random Solutions  
 
programming4us programming4us