Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.
import itertools
row = ['bbContag', 'bbED', 'bbENN_MN', 'bbLPI', 'bbLSI']
# If you want to do also permutations from using not all
# values in the list, then you can add the second argument
# of the itertools.permutations(row, n) and loop for n
# from 1 to n inclusively.
for n in range(1, len(row)+1):
print '\nPermutations of the length', n
print '-' * 50
for t in itertools.permutations(row, n):
print '+'.join(t)
result.txt
Sub Combinatrix()
'Forms all the combinations of one fixed value with varying numbers of other values
'Uses binary counting method
Dim i As Long, j As Long, k As Long, N As Long, NN As Long
Dim v As Variant, vInputs As Variant, vResults As Variant
Dim s As String
N = Application.CountA(Selection.Value)
ReDim vInputs(1 To N)
For i = 1 To N
If Selection.Cells(i) <> "" Then
j = j + 1
vInputs(j) = Selection.Cells(i)
End If
Next
ReDim Preserve vInputs(1 To j)
NN = 2 ^ (N - 1) - 1
ReDim vResults(1 To NN, 1 To 1)
For i = 1 To NN
vResults(i, 1) = vInputs(1)
For j = 2 To N
k = IIf((i Mod 2 ^ (j - 1)) >= 2 ^ (j - 2), 1, 0)
If k = 1 Then vResults(i, 1) = vResults(i, 1) & "+" & vInputs(j)
Next
Next
Selection.Offset(1, 0).Resize(NN, 1).Value = vResults
End Sub
Sub Combinatrix()
'Forms all the combinations of one fixed value with varying numbers of other values
'Uses binary counting method
Dim i As Long, iCol As Long, j As Long, k As Long, N As Long, NN As Long
Dim v As Variant, vInputs As Variant, vResults As Variant
Dim rw As Range, rg As Range
Dim s As String
Set rg = Selection
For Each rw In Selection.Rows
N = Application.CountA(rw)
ReDim vInputs(1 To N)
j = 0
For i = 1 To N
If rw.Cells(i) <> "" Then
j = j + 1
vInputs(j) = rw.Cells(i)
End If
Next
ReDim Preserve vInputs(1 To j)
NN = 2 ^ (N - 1) - 1
ReDim vResults(1 To NN, 1 To 1)
For i = 1 To NN
vResults(i, 1) = vInputs(1)
For j = 2 To N
k = IIf((i Mod 2 ^ (j - 1)) >= 2 ^ (j - 2), 1, 0)
If k = 1 Then vResults(i, 1) = vResults(i, 1) & "+" & vInputs(j)
Next
Next
iCol = iCol + 1 'Put the results on Sheet2 in adjacent columns
Worksheets("Sheet2").Cells(1, iCol).Resize(NN, 1).Value = vResults
Next
End Sub
If you are experiencing a similar issue, please ask a related question
Title | # Comments | Views | Activity |
---|---|---|---|
Excel VBA - UserForm Label Caption to show recordset data dynamically | 12 | 17 | |
Left trim cells in column A Excel vba | 2 | 29 | |
Excl VBA Find last column in disjointed range selection | 18 | 20 | |
VBA taking too long | 5 | 16 |
Join the community of 500,000 technology professionals and ask your questions.