Formula to get all possible combinations of 2 separate cells? Includes Screenshot of the issue

marshen

New Member
Joined
Apr 4, 2016
Messages
35
Office Version
  1. 365
Platform
  1. Windows
Hi all.
This forum has provided me with great assistance in the past and hopefully you can help me today.

I have a sheet that has 90 rows and I want to add a formula to it that will give me all the possible combinations of the 2 cells on the corresponding row, for each of the 90 rows.

Below is an example....

The values are that require the combinations to be calculated are in columns B & C

The possible combinations in my example are in columns D-I, however I'd like a formula to populate these combinations for me, which IO can then just drag down for each of the 90 rows.

1672158190812.png


It's worth nothing that some of the cells are blank and therefore I'd like the result as shown below, ie, the only possible combination is HH.
1672158167698.png


On a final note, the maximum characters in either column is 9, which of course will returns many different combinations of the 2 columns in that particular row.

I hope this all makes sense and you can assist me.

Thanks again.

Mark
 

Attachments

  • 1672158092125.png
    1672158092125.png
    2.5 KB · Views: 17

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
VBA Code:
Sub GetString()
  Dim xStr As String
  Dim FRow As Long, i As Long
  Dim xScreen As Boolean
  
  Application.ScreenUpdating = False
  lRow = Cells(Rows.count, 2).End(xlUp).Row
    
  For i = 1 To lRow
    xStr = Cells(i, 2).Value & Cells(i, 3).Value
      If Len(xStr) < 2 Then Exit Sub
      If Len(xStr) >= 8 Then
        MsgBox "Too many permutations!", vbInformation, "Permutation"
        Exit Sub
      Else
        ActiveSheet.Columns(1).Clear
        FRow = 1
        Call GetPermutation("", xStr, FRow, i)
      End If
    Next
    Application.ScreenUpdating = Ture
End Sub
Sub GetPermutation(Str1 As String, Str2 As String, ByRef xRow As Long, r As Long)
  Dim i As Integer, xLen As Integer
  Dim valExist As Boolean
  xLen = Len(Str2)
  If xLen < 2 Then
    valExist = False
    For c = 4 To xRow + 2
      If Cells(r, c).Value = Str1 & Str2 Then valExist = True
    Next
    If Not valExist Then
      Cells(r, xRow + 3).Value = Str1 & Str2
      xRow = xRow + 1
    End If
  Else
    For i = 1 To xLen
      Call GetPermutation(Str1 + Mid(Str2, i, 1), Left(Str2, i - 1) + Right(Str2, xLen - i), xRow, r)
    Next
  End If
End Sub
 
Upvote 0
VBA Code:
Sub GetString()
  Dim xStr As String
  Dim FRow As Long, i As Long
  Dim xScreen As Boolean
 
  Application.ScreenUpdating = False
  lRow = Cells(Rows.count, 2).End(xlUp).Row
   
  For i = 1 To lRow
    xStr = Cells(i, 2).Value & Cells(i, 3).Value
      If Len(xStr) < 2 Then Exit Sub
      If Len(xStr) >= 8 Then
        MsgBox "Too many permutations!", vbInformation, "Permutation"
        Exit Sub
      Else
        ActiveSheet.Columns(1).Clear
        FRow = 1
        Call GetPermutation("", xStr, FRow, i)
      End If
    Next
    Application.ScreenUpdating = Ture
End Sub
Sub GetPermutation(Str1 As String, Str2 As String, ByRef xRow As Long, r As Long)
  Dim i As Integer, xLen As Integer
  Dim valExist As Boolean
  xLen = Len(Str2)
  If xLen < 2 Then
    valExist = False
    For c = 4 To xRow + 2
      If Cells(r, c).Value = Str1 & Str2 Then valExist = True
    Next
    If Not valExist Then
      Cells(r, xRow + 3).Value = Str1 & Str2
      xRow = xRow + 1
    End If
  Else
    For i = 1 To xLen
      Call GetPermutation(Str1 + Mid(Str2, i, 1), Left(Str2, i - 1) + Right(Str2, xLen - i), xRow, r)
    Next
  End If
End Sub
Hi thanks for your reply.
I know how to run macros, however that's about as far as it goes.

I pasted the code in and changed the = Ture to = True, see below
1672240913755.png


But unfortunately when I un the macro nothing happens :-(

Any advice would be appreciated.

Thanks
Mark
 
Upvote 0
I pasted the code in and changed the = Ture to = True, see below
Yes, it should be True. Sorry for that. I added this in the last minute.

Weird. It was working for me when I put
B1:HH C1:AA
B2:GG C2:HH

Maybe I got your scenario wrong. I was working fine for me for the scenario above.
 
Upvote 0
Oh I think I know what happened :) I think your screen updating left False when you got the error. Run this function for once seperately and try the previous code again:
VBA Code:
Sub test()
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Oh I think I know what happened :) I think your screen updating left False when you got the error. Run this function for once seperately and try the previous code again:
VBA Code:
Sub test()
  Application.ScreenUpdating = True
End Sub
Thanks but no joy :-(

Nothing happens when either macro is ran :-(

Sadly I don't know much about macros :-(
Maybe you have a screenshot of you trying to replicate the scenario?
I suspect it'll be me and not the code!

Thanks
Mark
 
Upvote 0
Hi thanks for your efforts, it's really appreciated.

I'm thinking i've explained the requirement incorrectly.

This is to work out all the combinations regarding the order of goals scored within a football match. Basically the H means Home Team and the A means Away team.

The requirement is to get all the possible combinations regarding the order the goals are scored.

So for
1-0 the only combination is H
2-0 the only combination is HH
2-1 the combinations are HHA, HAH, AHH
3-0 the only combination is HHH
3-1 the combinations are HHHA, HHAH, HAHH, AHHH

So the above gives an idea of what i'm looking for.

In the 3-1 example the results are broken down as follows

HHHA The home team scores the first 3 goals and then the away team scores the last goal of the match
HHAH The home team scores the first 2 goals of the match, the away team then scores 1 and then the home team scores the last goal of the match
HAHH The home teams scores the first goal of the match, the away team then score the 2nd goal of the match, then the home team go on and score the 3rd and the 4th goal of the match
AHHH The away team score the first goal of the match, then the home team score the next 3 goals

All the above combinations result in a 3-1 result, however the goals could be scored in any of the above orders.

Below are 3 screenshots showing the whole sheet. As you can see when the scores get to 9-8, there will be many possible combinations in regards to the order of the goals, hence the need for a macro or formula to calculate these.

In an ideal world, a formula / macro would populate each possible outcome starting in column D for the corresponding row, so for our 3-1 example and all the scores above, the first screenshot below shows the desired outcome, with all the combinations shown in red.

1672250389809.png


Below shows the full sheet.

I hope this all makes sense and isn't too difficult.

Thanks
Mark
1672249543558.png

1672250063805.png

1672250085220.png
 

Attachments

  • 1672250372382.png
    1672250372382.png
    5 KB · Views: 17
Upvote 0
I still think my code does the thing you are after :)
I have no orher idea as far as I can understand. Sorry...
 
Upvote 0

Forum statistics

Threads
1,223,162
Messages
6,170,432
Members
452,326
Latest member
johnshaji

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