modify a macro by a bit?

excelNewbie22

Well-known Member
Joined
Aug 4, 2021
Messages
534
Office Version
  1. 365
Platform
  1. Windows
hi!
johnnyL helped me in this thread macro edit for generate 5 out of 6
can anyone help me modify it again?
to do exactly the same, just instead of 5 of 6, 4 out of 6?

i tried myself, but no luck
here's what i tried: (error in line 9)

VBA Code:
Sub excelNewbie22V2()
'
    Dim InputNumbersRow             As Long
    Dim LastRow                     As Long, StartRow                       As Long
    Dim SourceArray()               As Long, OutputArray(1 To 6, 1 To 15)    As Long
    Dim OutputRow                   As Long
    Dim SourceArrayColumn           As Long, SourceArrayRow                 As Long
    Dim OutputColumn                As String
    Dim InputNumbersArray           As Variant
'
    StartRow = 2                                                                       ' <--- Set this to the starting row of numbers to use
    OutputColumn = "G"                                                                  ' <--- Set this to the Column letter to display results to
    LastRow = Range("A" & Rows.Count).End(xlUp).Row                                     ' Get last row # of numbers to use
    SourceArray = GetCombinations(6, 4)                                                 ' Load SourceArray with all non repeating 4 out of 6 combinations
'
    InputNumbersArray = Range("A" & StartRow & ":F" & LastRow)                          ' Save numbers to use to InputNumbersArray
'
    Range("A" & StartRow & ":F" & LastRow).ClearContents                                ' Erase the numbers to use range
'
    OutputRow = -4                                                                      ' Initialize OutputRow
'
    For InputNumbersRow = LBound(InputNumbersArray, 1) To UBound(InputNumbersArray, 1)  ' Loop through the rows of numbers to use
        OutputRow = OutputRow + 15                                                       '   Increment the OutputRow
'
        Range("A" & OutputRow).Resize(1, 6) = Array(InputNumbersArray(InputNumbersRow, 1), _
                InputNumbersArray(InputNumbersRow, 2), InputNumbersArray(InputNumbersRow, 3), _
                InputNumbersArray(InputNumbersRow, 4), InputNumbersArray(InputNumbersRow, 5), _
                InputNumbersArray(InputNumbersRow, 6))                                 '   Display row of numbers to use to the sheet
'
        OutputRow = OutputRow + 1                                                       '   Increment the OutputRow
'
        For SourceArrayRow = 1 To 6                                                     '   Loop through rows of the SourceArray
            For SourceArrayColumn = 1 To 15                                              '       Loop through columns of the SourceArray
                Select Case SourceArray(SourceArrayRow, SourceArrayColumn)
                    Case 1: OutputArray(SourceArrayRow, SourceArrayColumn) = _
                            InputNumbersArray(InputNumbersRow, 1)                       '               Perform replacement array values ...
                    Case 2: OutputArray(SourceArrayRow, SourceArrayColumn) = _
                            InputNumbersArray(InputNumbersRow, 2)
                    Case 3: OutputArray(SourceArrayRow, SourceArrayColumn) = _
                            InputNumbersArray(InputNumbersRow, 3)
                    Case 4: OutputArray(SourceArrayRow, SourceArrayColumn) = _
                            InputNumbersArray(InputNumbersRow, 4)
                    Case 5: OutputArray(SourceArrayRow, SourceArrayColumn) = _
                            InputNumbersArray(InputNumbersRow, 5)
                    Case 6: OutputArray(SourceArrayRow, SourceArrayColumn) = _
                            InputNumbersArray(InputNumbersRow, 6)
                    Case 7: OutputArray(SourceArrayRow, SourceArrayColumn) = _
                            InputNumbersArray(InputNumbersRow, 7)
                    Case 8: OutputArray(SourceArrayRow, SourceArrayColumn) = _
                            InputNumbersArray(InputNumbersRow, 8)
                    Case 9: OutputArray(SourceArrayRow, SourceArrayColumn) = _
                            InputNumbersArray(InputNumbersRow, 9)
                    Case 10: OutputArray(SourceArrayRow, SourceArrayColumn) = _
                            InputNumbersArray(InputNumbersRow, 10)
                    Case 11: OutputArray(SourceArrayRow, SourceArrayColumn) = _
                            InputNumbersArray(InputNumbersRow, 11)
                    Case 12: OutputArray(SourceArrayRow, SourceArrayColumn) = _
                            InputNumbersArray(InputNumbersRow, 12)
                    Case 13: OutputArray(SourceArrayRow, SourceArrayColumn) = _
                            InputNumbersArray(InputNumbersRow, 13)
                    Case 14: OutputArray(SourceArrayRow, SourceArrayColumn) = _
                            InputNumbersArray(InputNumbersRow, 14)
                    Case 15: OutputArray(SourceArrayRow, SourceArrayColumn) = _
                            InputNumbersArray(InputNumbersRow, 15)
                End Select
            Next                                                                        '       Loop back
        Next                                                                            '   Loop back
'
        Range(OutputColumn & OutputRow).Resize(UBound(OutputArray), 5).Value = OutputArray  '   Display results to sheet
    Next                                                                                ' Loop back
End Sub


Function GetCombinations(lNumber As Long, lNoChosen As Long) As Long()

    Dim lOutput() As Long, lCombinations As Long
    Dim i As Long, j As Long, k As Long
   
    lCombinations = WorksheetFunction.Combin(lNumber, lNoChosen)
    ReDim lOutput(1 To lCombinations, 1 To lNoChosen)
   
    For i = 1 To lNoChosen
        lOutput(1, i) = i
    Next i
   
    For i = 2 To lCombinations
        For j = 1 To lNoChosen
            lOutput(i, j) = lOutput(i - 1, j)
        Next j
        For j = lNoChosen To 1 Step -1
            lOutput(i, j) = lOutput(i, j) + 1
            If lOutput(i, j) <= lNumber - (lNoChosen - j) Then Exit For
        Next j
        For k = j + 1 To lNoChosen
            lOutput(i, k) = lOutput(i, k - 1) + 1
        Next k
    Next i
   
    GetCombinations = lOutput
End Function




and here's the original:
VBA Code:
Sub excelNewbie22V2()
'
    Dim InputNumbersRow             As Long
    Dim LastRow                     As Long, StartRow                       As Long
    Dim SourceArray()               As Long, OutputArray(1 To 6, 1 To 5)    As Long
    Dim OutputRow                   As Long
    Dim SourceArrayColumn           As Long, SourceArrayRow                 As Long
    Dim OutputColumn                As String
    Dim InputNumbersArray           As Variant
'
    StartRow = 2                                                                        ' <--- Set this to the starting row of numbers to use
    OutputColumn = "G"                                                                  ' <--- Set this to the Column letter to display results to
    LastRow = Range("A" & Rows.Count).End(xlUp).Row                                     ' Get last row # of numbers to use
    SourceArray = GetCombinations(6, 5)                                                 ' Load SourceArray with all non repeating 5 out of 6 combinations
'
    InputNumbersArray = Range("A" & StartRow & ":F" & LastRow)                          ' Save numbers to use to InputNumbersArray
'
    Range("A" & StartRow & ":F" & LastRow).ClearContents                                ' Erase the numbers to use range
'
    OutputRow = -4                                                                      ' Initialize OutputRow
'
    For InputNumbersRow = LBound(InputNumbersArray, 1) To UBound(InputNumbersArray, 1)  ' Loop through the rows of numbers to use
        OutputRow = OutputRow + 6                                                       '   Increment the OutputRow
'
        Range("A" & OutputRow).Resize(1, 6) = Array(InputNumbersArray(InputNumbersRow, 1), _
                InputNumbersArray(InputNumbersRow, 2), InputNumbersArray(InputNumbersRow, 3), _
                InputNumbersArray(InputNumbersRow, 4), InputNumbersArray(InputNumbersRow, 5), _
                InputNumbersArray(InputNumbersRow, 6))                                  '   Display row of numbers to use to the sheet
'
        OutputRow = OutputRow + 1                                                       '   Increment the OutputRow
'
        For SourceArrayRow = 1 To 6                                                     '   Loop through rows of the SourceArray
            For SourceArrayColumn = 1 To 5                                              '       Loop through columns of the SourceArray
                Select Case SourceArray(SourceArrayRow, SourceArrayColumn)
                    Case 1: OutputArray(SourceArrayRow, SourceArrayColumn) = _
                            InputNumbersArray(InputNumbersRow, 1)                       '               Perform replacement array values ...
                    Case 2: OutputArray(SourceArrayRow, SourceArrayColumn) = _
                            InputNumbersArray(InputNumbersRow, 2)
                    Case 3: OutputArray(SourceArrayRow, SourceArrayColumn) = _
                            InputNumbersArray(InputNumbersRow, 3)
                    Case 4: OutputArray(SourceArrayRow, SourceArrayColumn) = _
                            InputNumbersArray(InputNumbersRow, 4)
                    Case 5: OutputArray(SourceArrayRow, SourceArrayColumn) = _
                            InputNumbersArray(InputNumbersRow, 5)
                    Case 6: OutputArray(SourceArrayRow, SourceArrayColumn) = _
                            InputNumbersArray(InputNumbersRow, 6)
                End Select
            Next                                                                        '       Loop back
        Next                                                                            '   Loop back
'
        Range(OutputColumn & OutputRow).Resize(UBound(OutputArray), 5).Value = OutputArray  '   Display results to sheet
    Next                                                                                ' Loop back
End Sub


Function GetCombinations(lNumber As Long, lNoChosen As Long) As Long()

    Dim lOutput() As Long, lCombinations As Long
    Dim i As Long, j As Long, k As Long
   
    lCombinations = WorksheetFunction.Combin(lNumber, lNoChosen)
    ReDim lOutput(1 To lCombinations, 1 To lNoChosen)
   
    For i = 1 To lNoChosen
        lOutput(1, i) = i
    Next i
   
    For i = 2 To lCombinations
        For j = 1 To lNoChosen
            lOutput(i, j) = lOutput(i - 1, j)
        Next j
        For j = lNoChosen To 1 Step -1
            lOutput(i, j) = lOutput(i, j) + 1
            If lOutput(i, j) <= lNumber - (lNoChosen - j) Then Exit For
        Next j
        For k = j + 1 To lNoChosen
            lOutput(i, k) = lOutput(i, k - 1) + 1
        Next k
    Next i
   
    GetCombinations = lOutput
End Function
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Is this what you were looking for a sample output?

Book1
ABCDEFGHIJK
1
2123456
31234
41235
51236
61245
71246
81256
91345
101346
111356
121456
132345
142346
152356
162456
173456
18789101112
1978910
2078911
2178912
22781011
23781012
24781112
25791011
26791012
27791112
287101112
29891011
30891012
31891112
328101112
339101112
3441112141522
354111214
364111215
374111222
384111415
394111422
404111522
414121415
424121422
434121522
444141522
4511121415
4611121422
4711121522
4811141522
4912141522
50
Sheet1
 
Upvote 0
Solution
wow!
johnnyL
even thought i didn't need it anymore, you helped,
thanks a lot!
 
Upvote 0
Here you go:

VBA Code:
Sub excelNewbie22V4Of6()
'
    Dim AmountOfNumbersChosen   As Long, MaxAmountOfNumbers     As Long
    Dim InputNumbersRow         As Long, OutputRow              As Long
    Dim StartRow                As Long, LastRow                As Long
    Dim SourceArrayColumn       As Long, SourceArrayRow         As Long
    Dim SourceArray()           As Long, OutputArray()          As Long
    Dim OutputColumn            As String
    Dim InputNumbersArray       As Variant
'
    AmountOfNumbersChosen = 4                                                           ' <--- Set this to the AmountOfNumbersChosen
    MaxAmountOfNumbers = 6                                                              ' <--- Set this to the MaxAmountOfNumbers
    StartRow = 2                                                                        ' <--- Set this to the starting row of numbers to use
    OutputColumn = "G"                                                                  ' <--- Set this to the Column letter to display results to
'
    SourceArray = GetCombinations(MaxAmountOfNumbers, AmountOfNumbersChosen)            ' Load SourceArray with all non repeating 4 out of 6 combinations
'
    ReDim OutputArray(1 To UBound(SourceArray, 1), 1 To UBound(SourceArray, 2))         ' Set the number of rows and columns of OutputArray
'
    LastRow = Range("A" & Rows.Count).End(xlUp).Row                                     ' Get last row # of numbers to use
'
    InputNumbersArray = Range("A" & StartRow & ":F" & LastRow)                          ' Load numbers to use to InputNumbersArray
'
    Range("A" & StartRow & ":F" & LastRow).ClearContents                                ' Erase the numbers to use range
'
    OutputRow = StartRow - UBound(SourceArray, 1)                                       ' Initialize OutputRow
'
    For InputNumbersRow = LBound(InputNumbersArray, 1) To UBound(InputNumbersArray, 1)  ' Loop through the rows of numbers to use
        OutputRow = OutputRow + UBound(SourceArray, 1)                                  '   Increment the OutputRow
'
        Range("A" & OutputRow).Resize(1, MaxAmountOfNumbers) = Array(InputNumbersArray(InputNumbersRow, 1), _
                InputNumbersArray(InputNumbersRow, 2), InputNumbersArray(InputNumbersRow, 3), _
                InputNumbersArray(InputNumbersRow, 4), InputNumbersArray(InputNumbersRow, 5), _
                InputNumbersArray(InputNumbersRow, 6))                                  '   Display row of numbers to use to the sheet
'
        OutputRow = OutputRow + 1                                                       '   Increment the OutputRow
'
        For SourceArrayRow = 1 To UBound(SourceArray, 1)                                '   Loop through rows of the SourceArray
            For SourceArrayColumn = 1 To UBound(SourceArray, 2)                         '       Loop through columns of the SourceArray
                Select Case SourceArray(SourceArrayRow, SourceArrayColumn)
                    Case 1: OutputArray(SourceArrayRow, SourceArrayColumn) = _
                            InputNumbersArray(InputNumbersRow, 1)                       '               Perform replacement array values ...
                    Case 2: OutputArray(SourceArrayRow, SourceArrayColumn) = _
                            InputNumbersArray(InputNumbersRow, 2)
                    Case 3: OutputArray(SourceArrayRow, SourceArrayColumn) = _
                            InputNumbersArray(InputNumbersRow, 3)
                    Case 4: OutputArray(SourceArrayRow, SourceArrayColumn) = _
                            InputNumbersArray(InputNumbersRow, 4)
                    Case 5: OutputArray(SourceArrayRow, SourceArrayColumn) = _
                            InputNumbersArray(InputNumbersRow, 5)
                    Case 6: OutputArray(SourceArrayRow, SourceArrayColumn) = _
                            InputNumbersArray(InputNumbersRow, 6)
                End Select
            Next                                                                        '       Loop back
        Next                                                                            '   Loop back
'
        Range(OutputColumn & OutputRow).Resize(UBound(SourceArray, 1), _
                UBound(SourceArray, 2)).Value = OutputArray                             '   Display results to sheet
    Next                                                                                ' Loop back
End Sub


Function GetCombinations(lNumber As Long, lNoChosen As Long) As Long()

    Dim lOutput() As Long, lCombinations As Long
    Dim i As Long, j As Long, k As Long
  
    lCombinations = WorksheetFunction.Combin(lNumber, lNoChosen)
    ReDim lOutput(1 To lCombinations, 1 To lNoChosen)
  
    For i = 1 To lNoChosen
        lOutput(1, i) = i
    Next i
  
    For i = 2 To lCombinations
        For j = 1 To lNoChosen
            lOutput(i, j) = lOutput(i - 1, j)
        Next j
        For j = lNoChosen To 1 Step -1
            lOutput(i, j) = lOutput(i, j) + 1
            If lOutput(i, j) <= lNumber - (lNoChosen - j) Then Exit For
        Next j
        For k = j + 1 To lNoChosen
            lOutput(i, k) = lOutput(i, k - 1) + 1
        Next k
    Next i
  
    GetCombinations = lOutput
End Function

That will make it easier for you to to modify, if you need to.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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