Help with vba code

lind33

New Member
Joined
Jun 25, 2019
Messages
14
Office Version
  1. 2016
Platform
  1. Windows
I modified this vba code to generate combinations of six from 10 groups of pairs where only one number of a pair is used for each combination. but I can't get exactly what i want, and there many repetitions of the same combinations and some numbers are not even used in combinations. Can I get a little help to fix this code, please? thanks.
Here is the vba:
VBA Code:
Sub Combs()
    Dim a As Long
    Dim b As Long
    Dim c As Long
    Dim d As Long
    Dim e As Long
    Dim f As Long
    Dim g As Long
    Dim h As Long
    Dim j As Long
    Dim k As Long
    Dim i As Long
    Dim grp
    grp = Range("B1:C10").Value
    Dim arr(1 To 1025, 1 To 10) As Long
    For a = 1 To 2
        For b = 1 To 2
            For c = 1 To 2
                For d = 1 To 2
                    For e = 1 To 2
                        For f = 1 To 2
                            For g = 1 To 2
                                For h = 1 To 2
                                    For j = 1 To 2
                                        For k = 1 To 2
                            i = i + 1
                            arr(i, 1) = grp(1, a)
                            arr(i, 2) = grp(2, b)
                            arr(i, 3) = grp(3, c)
                            arr(i, 4) = grp(4, d)
                            arr(i, 5) = grp(5, e)
                            arr(i, 6) = grp(6, f)
                            arr(i, 7) = grp(7, g)
                            arr(i, 8) = grp(8, h)
                            arr(i, 9) = grp(9, j)
                            arr(i, 10) = grp(10, k)
                                        Next k
                                    Next j
                                Next h
                            Next g
                        Next f
                    Next e
                Next d
            Next c
        Next b
    Next a
    Application.ScreenUpdating = False
    Range("H2").Resize(1025, 6).Value = arr
    Application.ScreenUpdating = True
End Sub

Here is a sample data: combs.xlsm

I posted this on another forum, but i couldn't get a working solution. here is the link: help with vba code

Thanks.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hi
Try to change to
VBA Code:
 arr(i, j) = grp(Int((10 - 1 + 1) * Rnd + 1), Col)
 
Upvote 0
Thanks, I did the change, the code works, but it doesn't give the correct answers. The rule is that only one number should be picked from any group to make the combination, and the list I created contains even repetitions.
 
Upvote 0

Forum statistics

Threads
1,214,792
Messages
6,121,612
Members
449,038
Latest member
apwr

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