vba to generate all combinations, no repeat, from a range?

excelNewbie22

Well-known Member
Joined
Aug 4, 2021
Messages
527
Office Version
  1. 365
Platform
  1. Windows
hi,
i googled quite a lot, found a lot but not what i'm looking for,
i need a simple macro to generate an x numbers out of a range,
so i can edit it each time per my need,
with no repeation

for example
range will be a2:h2 (or more or less)
numbers of combinations 5 (or more or less)
if possible with hyphens seperate between i.e 1-2-3-4-5
all results in one column

can you please help?

New Microsoft Excel Worksheet.xlsx
ABCDEFGHIJ
1of 6of 5
2123456781-2-3-4-5-61-2-3-4-5
31-2-3-4-5-71-2-3-4-6
41-2-3-4-5-81-2-3-4-7
51-2-3-4-6-71-2-3-4-8
61-2-3-4-6-81-2-3-5-6
71-2-3-4-7-81-2-3-5-7
81-2-3-5-6-71-2-3-5-8
91-2-3-5-6-81-2-3-6-7
101-2-3-5-7-81-2-3-6-8
111-2-3-6-7-81-2-3-7-8
121-2-4-5-6-71-2-4-5-6
131-2-4-5-6-81-2-4-5-7
141-2-4-5-7-81-2-4-5-8
151-2-4-6-7-81-2-4-6-7
161-2-5-6-7-81-2-4-6-8
171-3-4-5-6-71-2-4-7-8
181-3-4-5-6-81-2-5-6-7
191-3-4-5-7-81-2-5-6-8
201-3-4-6-7-81-2-5-7-8
211-3-5-6-7-81-2-6-7-8
221-4-5-6-7-81-3-4-5-6
232-3-4-5-6-71-3-4-5-7
242-3-4-5-6-81-3-4-5-8
252-3-4-5-7-81-3-4-6-7
262-3-4-6-7-81-3-4-6-8
272-3-5-6-7-81-3-4-7-8
282-4-5-6-7-81-3-5-6-7
293-4-5-6-7-81-3-5-6-8
301-3-5-7-8
311-3-6-7-8
321-4-5-6-7
331-4-5-6-8
341-4-5-7-8
351-4-6-7-8
361-5-6-7-8
372-3-4-5-6
382-3-4-5-7
392-3-4-5-8
402-3-4-6-7
412-3-4-6-8
422-3-4-7-8
432-3-5-6-7
442-3-5-6-8
452-3-5-7-8
462-3-6-7-8
472-4-5-6-7
482-4-5-6-8
492-4-5-7-8
502-4-6-7-8
512-5-6-7-8
523-4-5-6-7
533-4-5-6-8
543-4-5-7-8
553-4-6-7-8
563-5-6-7-8
574-5-6-7-8
test
 
Correct, this was for single numbers. How about:

VBA Code:
Dim ar, a, sq, x As Long

Sub jec()
 ar = [transpose(transpose(A2:H2))]
 a = Application.InputBox("Combin of...", , , , , , , 1)
 ReDim sq(Application.Combin(UBound(ar), a), 0)
 x = 0
 
 Application.ScreenUpdating = False
 
 Gen_comb "", -1, UBound(ar) - a
    
 With Cells(2, 13)
   .CurrentRegion.ClearContents
   .Resize(x) = sq
 End With
End Sub

Sub Gen_comb(xStr As String, y As Long, n As Long)
 Dim sp, j As Long
 For j = y + 1 To n
    sp = xStr & ar(j + 1)
    If UBound(Split(sp, "-")) = a - 1 Then
      sq(x, 0) = sp
      x = x + 1
    Else
      Gen_comb xStr & ar(j + 1) & "-", j, n + 1
   End If
 Next
End Sub

That version is admitingly close to 25% faster than the code I submitted. NICE!
 
  • Like
Reactions: JEC
Upvote 0

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
johnny,
can you please update the macro to ignore blank cells?
like if i set a2:h2
and d2 is empty
can it ignore d2 and still generate all possible combinations from the remaining cells?
 
Upvote 0

Forum statistics

Threads
1,221,814
Messages
6,162,133
Members
451,743
Latest member
matt3388

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