Tracking all combinations

mac_see

Active Member
Joined
Oct 15, 2002
Messages
419
This is a tough one. I hope I will be able to explain the scnerio. Ok.. Let me try.

I have ten workers "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" and "K". I have kept them in the range A1:A11

I want to create groups. Each group will consist of 4 workers. Every worker should get an opportunity to work with all the other workers.

Example:
"A", "B", "C" & "D"
"A", "C", "D" & "E"
"A", "B", "D" & "E"
"A", "B", "C" & "E"

and so on...................

Can this be done by using a formula.... Tracking all combinations would be very difficult to do it manually.

Maxi
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
ToranCalculation.xls
ABCDEFGH
1
2123546065
3234556166
4345566267
5456576368
6567586469
7678596570
8789606671
98910616772
1091011626873
11101112636974
12111213647075
13121314657176
141415667277
151516677378
161617687479
171718697580
1818197076
1919207177
20217278
21227379
222374
232475
242576
252677
262778
27
Sheet1


I have 25 rows and 6 columns in the range B2:G26. Some of the cells are blank.

Question:
---------
I want to make combinations of 5 numbers.

Criteria:
---------
1. There should be no duplicates in any combination of 5 numbers.
2. Every number in a combination of 5 numbers should be taken from different columns.

For example:
1 5 8 20 26 is NOT a valid combination because numbers given in the last three columns are not used.
8 12 21 55 58 is also NOT a valid combination because numbers given in the last two columns are not used.

We can skip one column but not more than that.

Result:
-------
1. How many combinations can we get keeping the criteria in mind?
2. Is it possible to automate this and see which are those combinations?

Maxi
 
Upvote 0
I'm interested in multiplying all permutations of an array x times. This looks pretty much like what I need except I don't know how to modifiy it to mulitply the values (and then divide by the number of values to get the average of each permutation product.

Any help would be appreciated

Thanks
 
Upvote 0
am using this formula in VB. The only problem is excel runs out of rows in row A…how do I continue the macro in Row B and so on?

Sub Combinations()
Dim n As Integer, m As Integer, numcomb
numcomb = 0
n = InputBox("Number of items?", "Combinations")
m = InputBox("Taken how many at a time?", "Combinations")
Application.ScreenUpdating = False
Comb2 n, m, 1, ""
End Sub

'Generate combinations of integers k..n taken m at a time, recursively
Private Function Comb2(ByVal n As Integer, ByVal m As Integer, _
ByVal k As Integer, ByVal s As String)
If m > n - k + 1 Then Exit Function
If m = 0 Then
ActiveCell = s
ActiveCell.Offset(1, 0).Select
Exit Function
End If
Comb2 n, m - 1, k + 1, s & k & " "
Comb2 n, m, k + 1, s
End Function
 
Upvote 0
As you can see, by posting this question/issue in three separate threads, people are less likely to help since all of the history on the issue moves around. If you keep the question on one thread, you stand a much better chance of getting help.
 
Upvote 0
Read my question below this quote

Ekim said:
Maxi,

I appreciate that you want a formula based solution, but the following macro based solution form Myrna Larson (Microsoft MVP) offers a number of advantages.

1. It allows Combinations or Permutations (see note below).
2. The macro handles numbers, text strings, words (e.g. names of people) or symbols.
3. The combinations are written to a new sheet.
4. Results are returned almost instantaneously.

Setup:
In sheet1:
Cell A1, put “C” (Combinations) or “P” (Permutations).
Cell A2, put the number of items in the subset – in your case it’s 4.
Cells A3 down, your list.
Book2
ABCDEFGHIJK
1C
24
3TomSawyer
4SueHarker
5MikeMcHenry
6HarryKewell
7FredSmith
8MarySmith
9KevinRudd
10LorisLane
11JohnHoward
12NicoleBryant
13MaxGallop
14
15
Sheet1


Note that your list, A:K, is actually 11 people. Using the COMBIN function, the combinations are 330.

The macro (standard module):

Code:
Option Explicit

Dim vAllItems As Variant
Dim Buffer() As String
Dim BufferPtr As Long
Dim Results As Worksheet
'
' Myrna Larson,  July 25, 2000,  Microsoft.Public.Excel.Misc

Sub ListPermutationsOrCombinations()
  Dim Rng As Range
  Dim PopSize As Integer
  Dim SetSize As Integer
  Dim Which As String
  Dim n As Double
  Const BufferSize As Long = 4096

    Worksheets("Sheet1").Range("A1").Select
  Set Rng = Selection.Columns(1).Cells
  If Rng.Cells.Count = 1 Then
    Set Rng = Range(Rng, Rng.End(xlDown))
  End If

  PopSize = Rng.Cells.Count - 2
  If PopSize< 2 Then GoTo DataError

  SetSize = Rng.Cells(2).Value
  If SetSize > PopSize Then GoTo DataError

  Which = UCase$(Rng.Cells(1).Value)
  Select Case Which
  Case "C"
    n = Application.WorksheetFunction.Combin(PopSize, SetSize)
  Case "P"
    n = Application.WorksheetFunction.Permut(PopSize, SetSize)
  Case Else
    GoTo DataError
  End Select
  If n > Cells.Count Then GoTo DataError

  Application.ScreenUpdating = False

  Set Results = Worksheets.Add

  vAllItems = Rng.Offset(2, 0).Resize(PopSize).Value
  ReDim Buffer(1 To BufferSize) As String
  BufferPtr = 0

  If Which = "C" Then
    AddCombination PopSize, SetSize
  Else
    AddPermutation PopSize, SetSize
  End If
  vAllItems = 0

  Application.ScreenUpdating = True
  Exit Sub

DataError:
  If n = 0 Then
    Which = "Enter your data in a vertical range of at least 4 cells." _
      & String$(2, 10) _
      & "Top cell must contain the letter C or P, 2nd cell is the Number" _
      & "of items in a subset, the cells below are the values from Which" _
      & "the subset is to be chosen."

  Else
    Which = "This requires " & Format$(n, "#,##0") & _
      " cells, more than are available on the worksheet!"
  End If
  MsgBox Which, vbOKOnly, "DATA ERROR"
  Exit Sub
End Sub

Private Sub AddPermutation(Optional PopSize As Integer = 0, _
  Optional SetSize As Integer = 0, _
  Optional NextMember As Integer = 0)

  Static iPopSize As Integer
  Static iSetSize As Integer
  Static SetMembers() As Integer
  Static Used() As Integer
  Dim i As Integer

  If PopSize<> 0 Then
    iPopSize = PopSize
    iSetSize = SetSize
    ReDim SetMembers(1 To iSetSize) As Integer
    ReDim Used(1 To iPopSize) As Integer
    NextMember = 1
  End If

  For i = 1 To iPopSize
    If Used(i) = 0 Then
      SetMembers(NextMember) = i
      If NextMember<> iSetSize Then
        Used(i) = True
        AddPermutation , , NextMember + 1
        Used(i) = False
      Else
        SavePermutation SetMembers()
      End If
    End If
  Next i

  If NextMember = 1 Then
    SavePermutation SetMembers(), True
    Erase SetMembers
    Erase Used
  End If

End Sub  'AddPermutation

Private Sub AddCombination(Optional PopSize As Integer = 0, _
  Optional SetSize As Integer = 0, _
  Optional NextMember As Integer = 0, _
  Optional NextItem As Integer = 0)

  Static iPopSize As Integer
  Static iSetSize As Integer
  Static SetMembers() As Integer
  Dim i As Integer

  If PopSize<> 0 Then
    iPopSize = PopSize
    iSetSize = SetSize
    ReDim SetMembers(1 To iSetSize) As Integer
    NextMember = 1
    NextItem = 1
  End If

  For i = NextItem To iPopSize
    SetMembers(NextMember) = i
    If NextMember<> iSetSize Then
      AddCombination , , NextMember + 1, i + 1
    Else
      SavePermutation SetMembers()
    End If
  Next i

  If NextMember = 1 Then
    SavePermutation SetMembers(), True
    Erase SetMembers
  End If

End Sub  'AddCombination

Private Sub SavePermutation(ItemsChosen() As Integer, _
  Optional FlushBuffer As Boolean = False)

  Dim i As Integer, sValue As String
  Static RowNum As Long, ColNum As Long

  If RowNum = 0 Then RowNum = 1
  If ColNum = 0 Then ColNum = 1

  If FlushBuffer = True Or BufferPtr = UBound(Buffer()) Then
    If BufferPtr > 0 Then
      If (RowNum + BufferPtr - 1) > Rows.Count Then
        RowNum = 1
        ColNum = ColNum + 1
        If ColNum > 256 Then Exit Sub
      End If

      Results.Cells(RowNum, ColNum).Resize(BufferPtr, 1).Value _
        = Application.WorksheetFunction.Transpose(Buffer())
      RowNum = RowNum + BufferPtr
    End If

    BufferPtr = 0
    If FlushBuffer = True Then
      Erase Buffer
      RowNum = 0
      ColNum = 0
      Exit Sub
    Else
      ReDim Buffer(1 To UBound(Buffer))
    End If

  End If

  'construct the next set
  For i = 1 To UBound(ItemsChosen)
    sValue = sValue & ", " & vAllItems(ItemsChosen(i), 1)
  Next i

  'and save it in the buffer
  BufferPtr = BufferPtr + 1
  Buffer(BufferPtr) = Mid$(sValue, 3)
End Sub  'SavePermutation
Permutations

This does not apply to you but may be useful in some other application. A permutation is any set or subset of objects or events where internal order is significant. For example, take the team consisting of Tom, Sue, and Mike. There are 6 permutations:

Tom , Sue, Mike
Tom , Mike, Sue
Sue , Tom, Mike
Sue , Mike, Tom
Mike , Tom, Sue
Mike , Sue, Tom

Which is confirmed with Excel’s PERMUT function:

=PERMUT(3,3)
= 6

With your 11 names in groups of 4, there are 330 groups but 7,920 permutations.

HTH


Mike

What changes do I need to make to this VBA code to get multiple combinations in just one go. Example:

If I have C in A1, 3 in A2 and Numbers from 1-5 in the range A3:A7 and if I run the macro, it will give me all possible combinations of 3 in sheet2

If I have two conditions

1. If I have C in A1, 3 in A2 and Numbers from 1-5 in the range A3:A7 and if I run the macro, it will give me all possible combinations of 3 in sheet2

2. Lets say if I have C in B1, 3 in B2 and Numbers from 1-5 in the range B3:B7 and if I run the macro, it should give me all possible combinations of 3 in sheet2 in columns A and B

Maxi
 
Upvote 0
I have not replied with an answer to pignotts's question. The last post on this thread is my question and I am looking forward for an answer.

Can anybody help?

Sorry for the confusion.

Maxi
 
Upvote 0

Forum statistics

Threads
1,217,364
Messages
6,136,117
Members
449,993
Latest member
Sphere2215

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top