VBA List all possible combination of variable number of items (number of nested loops as variable) - vba

gentleman! I am having trouble with figuring out a way to define the number of elements as variable when listing all possible combinations. I have a hard coded example of this where number of elements = 3
'Declare variables
Dim a as long
Dim b as Long
Dim C as Long
Dim ElementsArray as variant
'Array
ElementsArray = array("1400","1900","2400")
'Loop through combinations
for a = lbound(ElementsArray) to ubound(ElementsArray)
for B= lbound(ElementsArray) to ubound(ElementsArray)
for c = lbound(ElementsArray) to ubound(ElementsArray)
debug.print(ElementsArray(a) & " - " & ElementsArray(b) & " - " & ElementsArray(c))
next c
next b
next a
But What I am looking for is a code in which perhaps the number of nested For loops is a variable or some other ways to permutate through all possible combinations. Please help solve this problem.

Here is an example of a recursive implementation. Just be warned that you shouldn't make your array too large as you will get n to the power of n solutions - for 4 elements, that's 256, for 5 elements 3'125, for 6 you get 46'656 and for 7 already 823'543 - don't complain if the program takes a long time to execute. And of course you need a way to do something with every permutation.
Option Explicit
Sub test()
Dim ElementsArray As Variant
ElementsArray = Array("1400", "1900", "2400")
ReDim SolutionArray(LBound(ElementsArray) To UBound(ElementsArray))
recursion ElementsArray, SolutionArray, LBound(ElementsArray)
End Sub
Sub recursion(elements, solution, level As Long)
Dim i As Long
For i = LBound(elements) To UBound(elements)
solution(level) = elements(i)
If level = UBound(elements) Then
Debug.Print Join(solution, " - ")
Else
recursion elements, solution, level + 1
End If
Next i
End Sub
Update: This is the result:
Update
Still not sure if I understand. The following code will create a list of n-Tupel out of an array of values.
In the example (test), we have an array of 4 values and set n to 3 (defined as constant).
Sub test()
Const n = 3
Dim ElementsArray As Variant
ElementsArray = Array("1400", "1900", "2400", "9999")
ReDim SolutionArray(0 To n - 1)
recursion ElementsArray, SolutionArray, LBound(ElementsArray)
End Sub
Sub recursion(elements, solution, level As Long)
Dim i As Long
For i = LBound(elements) To UBound(elements)
solution(level) = elements(i)
If level = UBound(solution) Then
Debug.Print Join(solution, " - ")
Else
recursion elements, solution, level + 1
End If
Next i
End Sub

Related

Using If Conditionals to Exit For Loops VBA/VB

I am creating a third party add in for my CAD program that has a sub in it that goes through a drawing and finds all the parts lists (BOMS), if any items in the parts list are shared between the BOM (1 part being used in 2 weldments for example) then it changes the item number of the second instance to be that of the first instance. It does this by comparing full file names between the two values. When they match change the number to that of the matcher. I have got this to work but it runs a little slow because for a 100 item BOM each item is compared to 100 and thus that takes a little longer then I would like (about 60seconds to run). After thinking about it I realized I did not need to compare each item to all the items, I just needed to compare until it found a duplicate and then exit the search loop and go to the next value. Example being Item 1 does not need to compare to the rest of the 99 values because even if it does have a match in position 100 I do not want to change item 1s number to that of item 100. I want to change item 100 to that of 1(ie change the duplpicate to that of the first encountered double). For my code however I am having trouble exiting the comparison for loops which is causing me trouble. An example of the trouble is this:
I have 3 BOMs, each one shares Part X, and is numbered 1 in BOM 1, 4 in BOM 2, and 7 in BOM 3. when I run my button because I cannot get it to leave the comparison loop once it finds it first match all the Part X's ended up getting item number 7 from BOM 3 because it is the last instance. (I can get this to do what I want by stepping through my for loops backwards and thus everything ends up as the top most occurrence, but I would like to get my exit fors working because it saves me on unnecessary comparisons)
How do I go about breaking out of the nested for loops using an if conditional?
Here is my current code:
Public Sub MatchingNumberR1()
Debug.Print ThisApplication.Caption
'define active document as drawing doc. Will produce an error if its not a drawing doc
Dim oDrawDoc As DrawingDocument
Set oDrawDoc = ThisApplication.ActiveDocument
'Store all the sheets of drawing
Dim oSheets As Sheets
Set oSheets = oDrawDoc.Sheets
Dim oSheet As Sheet
'Loop through all the sheets
For Each oSheet In oSheets
Dim oPartsLists As PartsLists
Set oPartsLists = oSheet.PartsLists
'Loop through all the part lists on that sheet
Dim oPartList As PartsList
'For every parts list on the sheet
For Each oPartList In oPartsLists
For i3 = 1 To oPartList.PartsListRows.Count
'Store the Item number and file referenced in that row to compare
oItem = FindItem(oPartList)
oDescription = FindDescription(oPartList)
oDescripCheck = oPartList.PartsListRows.Item(i3).Item(oDescription).Value
oNumCheck = oPartList.PartsListRows.Item(i3).Item(oItem).Value
'Check to see if the BOM item is a virtual component if it is do not try and get the reference part
If oPartList.PartsListRows.Item(i3).ReferencedFiles.Count = 0 Then
oRefPart = " "
End If
'Check to see if the BOM item is a virtual component if it is try and get the reference part
If oPartList.PartsListRows.Item(i3).ReferencedFiles.Count > 0 Then
oRefPart = oPartList.PartsListRows.Item(i3).ReferencedFiles.Item(1).FullFileName
End If
MsgBox (" We are comparing " & oRefPart)
'''''Create a comparison loop to go through the drawing that checks the oRefPart against other BOM items and see if there is a match.'''''
'Store all the sheets of drawing
Dim oSheets2 As Sheets
Set oSheets2 = oDrawDoc.Sheets
Dim oSheet2 As Sheet
'For every sheet in the drawing
For Each oSheet2 In oSheets2
'Get all the parts list on a single sheet
Dim oPartsLists2 As PartsLists
Set oPartsLists2 = oSheet2.PartsLists
Dim oPartList2 As PartsList
'For every parts list on the sheet
For Each oPartList2 In oPartsLists2
oItem2 = FindItem(oPartList2)
oDescription2 = FindDescription(oPartList2)
'Go through all the rows of the part list
For i6 = 1 To oPartList2.PartsListRows.Count
'Check to see if the part is a not a virtual component, if not, get the relevent comparison values
If oPartList2.PartsListRows.Item(i6).ReferencedFiles.Count > 0 Then
oNumCheck2 = oPartList2.PartsListRows.Item(i6).Item(oItem2).Value
oRefPart2 = oPartList2.PartsListRows.Item(i6).ReferencedFiles.Item(1).FullFileName
'Compare the file names, if they match change the part list item number for the original to that of the match
If oRefPart = oRefPart2 Then
oPartList.PartsListRows.Item(i3).Item(1).Value = oNumCheck2
''''''''This is where I want it to exit the loop and grab the next original value'''''''
End If
'For virtual components get the following comparison values
ElseIf oPartList2.PartsListRows.Item(i6).ReferencedFiles.Count = 0 Then
oNumCheck2 = oPartList2.PartsListRows.Item(i6).Item(oItem2).Value
oDescripCheck2 = oPartList2.PartsListRows.Item(i6).Item(oDescription2).Value
'Compare the descriptions and if they match change the part list item number for the original to that of the match
If oDescripCheck = oDescripCheck2 Then
oPartList.PartsListRows.Item(i3).Item(1).Value = oNumCheck2
''''''''This is where I want it to exit the loop and grab the next original value'''''''
End If
Else
''''''''This is where if no matches were found I want it to continue going through the comparison loop'''''''
End If
Next
Next
Next
Next
Next
Next
'MsgBox ("Matching Numbers has been finished")
End Sub
For escape from nested for loop you can use GoTo and specify where.
Sub GoToTest()
Dim a, b, c As Integer
For a = 0 To 1000 Step 100
For b = 0 To 100 Step 10
For c = 0 To 10
Debug.Print vbTab & b + c
If b + c = 12 Then
GoTo nextValueForA
End If
Next
Next
nextValueForA:
Debug.Print a + b + c
Next
End Sub
Here are a few examples that demonstrate (1) breaking out of (exiting) a loop and (2) finding the values in arrays.
The intersection of 2 arrays example can be modified to meet your need to "Create a comparison loop to go through the drawing that checks the oRefPart against other BOM items and see if there is a match." Note, you may find multiple matches between 2 arrays.
Option Explicit
Option Base 0
' Example - break out of loop when condition met.
Public Sub ExitLoopExample()
Dim i As Integer, j As Integer
' let's loop 101 times
For i = 0 To 100:
j = i * 2
'Print the current loop number to the Immediate window
Debug.Print i, j
' Let's decide to break out of the loop is some
' condition is met. In this example, we exit
' the loop if j>=10. However, any condition can
' be used.
If j >= 10 Then Exit For
Next i
End Sub
' Example - break out of inner loop when condition met.
Public Sub ExitLoopExample2()
Dim i As Integer, j As Integer
For i = 1 To 5:
For j = 1 To 5
Debug.Print i, j
' if j >= 2 then, exit the inner loop.
If j >= 2 Then Exit For
Next j
Next i
End Sub
Public Sub FindItemInArrayExample():
' Find variable n in array arr.
Dim intToFind As Integer
Dim arrToSearch As Variant
Dim x, y
intToFind = 4
arrToSearch = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
x = FindItemInArray(FindMe:=intToFind, _
ArrayToSearch:=arrToSearch)
If IsEmpty(x) Then
Debug.Print intToFind; "not found in arrToSearch"
Else
Debug.Print "found "; x
End If
intToFind = 12
y = FindItemInArray(FindMe:=intToFind, _
ArrayToSearch:=arrToSearch)
If IsEmpty(y) Then
Debug.Print intToFind; "not found in arrToSearch"
Else
Debug.Print "found "; y
End If
End Sub
Public Function FindItemInArray(FindMe, ArrayToSearch As Variant):
Dim i As Integer
For i = LBound(ArrayToSearch) To UBound(ArrayToSearch)
If FindMe = ArrayToSearch(i) Then
FindItemInArray = ArrayToSearch(i)
Exit For
End If
Next i
End Function
' Create a comparison loop to go through the drawing that checks
' the oRefPart against other BOM items and see if there is a match.
Public Sub ArrayIntersectionExample():
Dim exampleArray1 As Variant, exampleArray2 As Variant
Dim arrIntersect As Variant
Dim i As Integer
' Create two sample arrays to compare
exampleArray1 = Array(1, 2, 3, 4, 5, 6, 7)
exampleArray2 = Array(2, 4, 6, 8, 10, 12, 14, 16)
' Call our ArrayIntersect function (defined below)
arrIntersect = ArrayIntersect(exampleArray1, exampleArray2)
' Print the results to the Immediate window
For i = LBound(arrIntersect) To UBound(arrIntersect)
Debug.Print "match " & i + 1, arrIntersect(i)
Next i
End Sub
Public Function ArrayIntersect(arr1 As Variant, arr2 As Variant) As Variant:
' Find items that exist in both arr1 and arr2 (intersection).
' Return the intersection as an array (Variant).
Dim arrOut() As Variant
Dim matchIndex As Long
Dim i As Long, j As Long
' no matches yet
matchIndex = -1
' begin looping through arr1
For i = LBound(arr1) To UBound(arr1)
' sub-loop for arr2 for each item in arr1
For j = LBound(arr2) To UBound(arr2)
' check for match
If arr1(i) = arr2(j) Then
' we found an item in both arrays
' increment match counter, which we'll
' use to size our output array
matchIndex = matchIndex + 1
' resize our output array to fit the
' new match
ReDim Preserve arrOut(matchIndex)
' now store the new match our output array
arrOut(matchIndex) = arr1(i)
End If
Next j
Next i
' Have the function return the output array.
ArrayIntersect = arrOut
End Function

Simple way to find max level 1 WBS and put all Level 2 WBS into array?

I can't seem to find anything about how I might do this from the documentation. My question basically has it all. I need the max WBS level 1 value as an integer, and then to loop through all its level2 subtasks/summaries and put a couple of their values into an array.
It would also be handy if I could get number of subtasks that belong to that summary before iterating so I could dim my array with the correct rows/columns and not have to transpose it after-the-fact.
Any help or guidance would be appreciated, MS Project documentation is awful and the internet doesn't have much else on a lot of this.
I Don't want to have to do this:
Dim TopVal As Integer
For Each t in ActiveProject.Tasks
Dim tVal As Integer
tVal = t.WBS.Split("."c)(0)
If tVal > TopVal Then TopVal = tVal
Next t
Unfortunately, you will have to loop to figure things out. MS Project doesn't allow you to pull in a set of fields (like all the WBSs) into an array without looping through everything. For this problem, you'll need to determine two different bits of information: what level WBS you're working with and how many levels of sub-tasks are underneath that given WBS.
At the main program level, you'll need to run through ALL the tasks and determine the WBS level of each task. Once you get the level you want, then you can determine the number of sub-tasks.
Private Sub test()
With ThisProject
Dim i As Long
For i = 1 To .Tasks.count
Dim subWBSCount As Long
If .Tasks.Item(i).OutlineLevel = 2 Then
subWBSCount = GetSubWBSCount(.Tasks.Item(i).wbs, i)
Debug.Print "At level 2 (" & .Tasks.Item(i).wbs & _
") there are " & subWBSCount & " sub tasks"
'-----------------------------------------------
' you can properly dimension your array here,
' then fill it with the sub-task information
' as needed
'-----------------------------------------------
End If
Next i
End With
End Sub
When you need to count the sub-tasks under the level 2 WBS, it's easiest to break into a separate function to keep the logic straight. What it does it to start with the given task and work down, comparing each subsequent task's WBS "prefix" -- meaning if you're looking for sub-tasks under WBS 1.1, then when you see WBS 1.1.1 and 1.1.2, you need to really compare the "1.1" parts of each of them. Count until you run out of sub-tasks.
Private Function GetSubWBSCount(ByVal topWBS As String, ByVal wbsIndex As Long) As Long
'--- loop to find the given WBS, then determine how many
' sub tasks lie under that WBS
With ThisProject
Dim j As Long
Dim count As Long
For j = (wbsIndex + 1) To .Tasks.count
Dim lastDotPos As Long
lastDotPos = InStrRev(.Tasks.Item(j).wbs, _
".", , vbTextCompare)
Dim wbsPrefix As String
wbsPrefix = Left$(.Tasks.Item(j).wbs, _
lastDotPos - 1)
If wbsPrefix = topWBS Then
count = count + 1
'--- check for the edge case where this is
' the very last task, and so our count is
' finished
If j = .Tasks.count Then
GetSubWBSCount = count
Exit Function
End If
Else
'--- once we run out of sub-wbs tasks that
' match, we're done
GetSubWBSCount = count
Exit Function
End If
Next j
End With
End Function
Here's the whole test module:
Option Explicit
Private Sub test()
With ThisProject
Dim i As Long
For i = 1 To .Tasks.count
Dim subWBSCount As Long
If .Tasks.Item(i).OutlineLevel = 2 Then
subWBSCount = GetSubWBSCount(.Tasks.Item(i).wbs, i)
Debug.Print "At level 2 (" & .Tasks.Item(i).wbs & _
") there are " & subWBSCount & " sub tasks"
'-----------------------------------------------
' you can properly dimension your array here,
' then fill it with the sub-task information
' as needed
'-----------------------------------------------
End If
Next i
End With
End Sub
Private Function GetSubWBSCount(ByVal topWBS As String, ByVal wbsIndex As Long) As Long
'--- loop to find the given WBS, then determine how many
' sub tasks lie under that WBS
With ThisProject
Dim j As Long
Dim count As Long
For j = (wbsIndex + 1) To .Tasks.count
Dim lastDotPos As Long
lastDotPos = InStrRev(.Tasks.Item(j).wbs, _
".", , vbTextCompare)
Dim wbsPrefix As String
wbsPrefix = Left$(.Tasks.Item(j).wbs, _
lastDotPos - 1)
If wbsPrefix = topWBS Then
count = count + 1
'--- check for the edge case where this is
' the very last task, and so our count is
' finished
If j = .Tasks.count Then
GetSubWBSCount = count
Exit Function
End If
Else
'--- once we run out of sub-wbs tasks that
' match, we're done
GetSubWBSCount = count
Exit Function
End If
Next j
End With
End Function
I'm not sure what you mean by "I need the max WBS level 1". Wouldn't this just be the first task in your project?.. i.e. ActiveProject.Tasks.Item(1)
As for level 2 tasks in an array: Take a look at the .outlineLevel property of the task. This property tells you if the task is WBS level 1, 2, 3, etc.
See https://msdn.microsoft.com/en-us/vba/project-vba/articles/task-outlinelevel-property-project for further details
As for "dim my array with the correct rows/columns": while you could use an array and either first figure out its size, or keep resizing it as you find more elements; another approach I'd suggest is use a data structure that you can add elements to. My top choice for this is the Collection data type. It is built-in and easy to use, but there are others available too that may be more appropriate for your situation.
I think this snippet should do what your asking for:
Function getLevel2Tasks() As Collection
Dim t As Task
Dim level2Tasks As Collection
Set level2Tasks = New Collection
For Each t In ActiveProject.Tasks
If t.outlineLevel = 2 Then
level2Tasks.Add Item:=t
End If
Next
Set getLevel2Tasks = level2Tasks
End Function
Consider use t.OutlineLevel to sort them
This code finds the task with the highest WBS (e.g. maximum of first part of WBS code), and counts its subtasks based on the outline structure of the schedule.
Sub GetMaxWBSTaskInfo()
Dim MaxWBS As Integer
Dim tsk As Task
Dim MaxWbsTask As Task
Dim NumSubtasks As Integer
' expand all subprojects so loop goes through all subproject tasks
Application.SelectAll
Application.OutlineShowAllTasks
Application.SelectBeginning
For Each tsk In ActiveProject.Tasks
If Split(tsk.WBS, ".")(0) > MaxWBS Then
MaxWBS = Split(tsk.WBS, ".")(0)
Set MaxWbsTask = tsk
End If
Next
NumSubtasks = ChildCount(MaxWbsTask)
Debug.Print "Max WBS level=" & MaxWBS, "Task: " & MaxWbsTask.Name, "# subtasks=" & NumSubtasks
End Sub
Function ChildCount(tsk As Task) As Integer
Dim s As Task
Dim NumTasks As Integer
For Each s In tsk.OutlineChildren
NumTasks = NumTasks + 1 + ChildCount(s)
Next s
ChildCount = NumTasks
End Function

Function will not return array when range contains only one value

I have a function meant to return an array which is created out of a single-column list of data. I have been using this function's return value essentially as a pseudo-global variable (LINENAMES_ARRAY) which I pass to many functions. Those functions than do checks on it such as If Len(Join(LINENAMES_ARRAY)) = 0 Then or go through items with For Each statements. Here is the code:
Function LINENAMES_ARRAY() As Variant
'returns an array of all items in the main sheet linenames column
LINENAMES_ARRAY = Application.Transpose(MAIN.Range( _
MAIN.Cells(MAIN_HEAD_COUNT + 1, MAIN_LINENAMES_COLUMN), _
MAIN.Cells(LINENAMES_COUNT + 1, MAIN_LINENAMES_COLUMN)))
End Function
I recently stumbled on one of those you-don't-see-it-till-you-see-it problems while using this workbook for a new project, where if the array happens to be only 1 element, everything fails. Apparently in that case, this returns a single value so Join() will fail For Each __ in LINENAMES_ARRAY will too. Why won't it treat this as a 1x1 array rather than a free value? I have started to mitigate the problem by rewriting functions where this is called, to check whether it is an array, then do some other procedure. Things like:
For j = 1 To LINENAMES_COUNT
LINES_BOX.AddItem lineNames(j)
Next j
is changed to:
If Not IsArray(LINENAMES_ARRAY) Then
myListBox.AddItem CStr(LINENAMES_ARRAY)
Else
For j = 1 To LINENAMES_COUNT
LINES_BOX.AddItem LINENAMES_ARRAY(j)
Next j
End If
However this becomes messy and is adding many extra checks to my code that I would prefer to handle in the LINENAMES_ARRAY function. Is there a way to return a 1x1 array? Or any other workaround?
An array can have a single element if you create it as a single element array and populate it in an array manner.
Option Explicit
Dim MAIN_HEAD_COUNT As Long
Dim LINENAMES_COUNT As Long
Dim MAIN_LINENAMES_COLUMN As Long
Dim MAIN As Worksheet
Sub stuff()
Dim arr As Variant
Set MAIN = Worksheets("Sheet1")
MAIN_LINENAMES_COLUMN = 2
MAIN_HEAD_COUNT = 2
LINENAMES_COUNT = 2
arr = LINENAMES_ARRAY()
Debug.Print IsArray(arr)
Debug.Print LBound(arr) & ":" & UBound(arr)
End Sub
Function LINENAMES_ARRAY() As Variant
Dim a As Long, tmp() As Variant
ReDim tmp(0 To LINENAMES_COUNT - MAIN_HEAD_COUNT)
For a = 0 To LINENAMES_COUNT - MAIN_HEAD_COUNT
tmp(a) = MAIN.Range(MAIN.Cells(MAIN_HEAD_COUNT + 1, MAIN_LINENAMES_COLUMN), _
MAIN.Cells(LINENAMES_COUNT + 1, MAIN_LINENAMES_COLUMN)).Cells(a).Value2
Next a
'returns an array of all items in the main sheet linenames column
LINENAMES_ARRAY = tmp
End Function
Results from the VBE's Immediate window:
True
0:0

Making Selected Range into an Array and doing stuff with it

I'm trying to teach myself VBA writing some little things. I'm trying to make something that allows you to select some data and then calculates the mean and variance. My code is as follows :
Sub VarianceCalculator()
Dim k As Integer
Dim SelectedData As Range
Dim SelectedDataArray() As Variant
Dim Var As Double
Dim Mu As Double
On Error Resume Next
Set SelectedData = Application.InputBox("Select a range of data to be
calculated", Default:=Selection.Address, Type:=8)
On Error GoTo 0
SelectedDataArray = Range(SelectedData.Address)
k = UBound(SelectedDataArray)
Call VarianceCalculatorWithArray(SelectedDataArray, k)
MsgBox ("The selected data has variance " & Var & " and has mean " & Mu)
End Sub
Sub VarianceCalculatorWithArray(Data() As Variant, k As Integer)
Dim Var As Double
Dim Mu As Double
Dim j As Integer
Dim i As Integer
ReDim Data(k) As Variant
Mu = 0
Var = 0
For j = 0 To k
Mu = Mu + (Data(j)) / (k + 1)
Next j
For i = 0 To k
Var = Var + ((Data(i) - Mu) ^ (2)) / (k + 1)
Next i
End Sub
I think the error is that somehow the data is not getting transferred into the array but I can't find a solution to this.
Thanks!!
There are two major problems:
(1) If you want to pass the variables Var and Mu from one procedure to another you'll have to declare them as public variables before the first procedure. The alternative is to setup VarianceCalculatorWithArray as a function.
(2) The array Data() is 2D as a range consists of rows and columns. So, if you want to use an element from this array, you'll have to address it as Data(1, 1). Also note, that this range array starts with row 1 and column 1. Therefore your for...next statements should start with 1 and not with 0.
Note, that you can always set a breakpoint in order to check if data is transferred into the array.

Unique Combinations in an array using VBA

I need a code that could give me a list of unique combinations from a set of elements in an array, something like this:
Say myArray contains [A B C]
So, the output must be:
A
B
C
A B
A C
B C
A B C
or
A B C
B C
A C
A B
A
B
C
either output is OK for me (Starts with 1 combination, followed by 2 combinations and ends with all combination OR vice versa).
The position of the letters are not critical and the order of letters within the same combination type is also not critical.
I'd found a suggestion by 'Dick Kusleika' in a thread: Creating a list of all possible unique combinations from an array (using VBA) but when I tried, it did not present me with the arrangement that I wanted.
I'd also found a suggestion by 'pgc01' in a thread: http://www.mrexcel.com/forum/excel-questions/435865-excel-visual-basic-applications-combinations-permutations.html and it gave me the arrangement that I wanted however, the combinations was not being populated in an array but it was being populated in excel cells instead, using looping for each combination.
So, I wanted the arrangement of combinations to be like what 'pgc01' suggested and being populated in an array as what 'Dick Kusleika' presented.
Anyone can help? Appreciate it.
Start from here:
Sub TestRoutine()
Dim inputt() As String, i As Long
Dim outputt As Variant
inputt = Split("A B C", " ")
outputt = Split(ListSubsets(inputt), vbCrLf)
For i = LBound(outputt) + 2 To UBound(outputt)
MsgBox i & vbTab & outputt(i)
Next i
End Sub
Function ListSubsets(Items As Variant) As String
Dim CodeVector() As Long
Dim i As Long
Dim lower As Long, upper As Long
Dim SubList As String
Dim NewSub As String
Dim done As Boolean
Dim OddStep As Boolean
OddStep = True
lower = LBound(Items)
upper = UBound(Items)
ReDim CodeVector(lower To upper) 'it starts all 0
Do Until done
'Add a new subset according to current contents
'of CodeVector
NewSub = ""
For i = lower To upper
If CodeVector(i) = 1 Then
If NewSub = "" Then
NewSub = Items(i)
Else
NewSub = NewSub & " " & Items(i)
End If
End If
Next i
If NewSub = "" Then NewSub = "{}" 'empty set
SubList = SubList & vbCrLf & NewSub
'now update code vector
If OddStep Then
'just flip first bit
CodeVector(lower) = 1 - CodeVector(lower)
Else
'first locate first 1
i = lower
Do While CodeVector(i) <> 1
i = i + 1
Loop
'done if i = upper:
If i = upper Then
done = True
Else
'if not done then flip the *next* bit:
i = i + 1
CodeVector(i) = 1 - CodeVector(i)
End If
End If
OddStep = Not OddStep 'toggles between even and odd steps
Loop
ListSubsets = SubList
End Function
Note we discard the first two elements of the output array.