Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50158

6 Number Combination

$
0
0
Hi,
I copied this VBA code from a search to help with 6/49 lotto. It is suppose to do the following:
It allows an input (in Column B)of an x set of numbers (e.g. 1,2,3,4,7,0,42,34,12, etc.)
and it should give you all possible 6 number combinations (e.g. 3,7,12,34,1,2) and so forth.
When i run it, it gives mismatch errors.
Can anyone help me figure it out. Or is it better to start from scratch?
Thanks



Code:

Sub comb()
    choice = [a1]:    arraycount = 0:    outputpointer = 0
    Dim fromarray() As Long, outputarray() As Long
   
    Sheets("Sheet2").UsedRange.Cells.ClearContents ' ### clear the output area
   
    ' first, find out what the values are that combinations are to be made from
    For Each c In ActiveSheet.Range("B1", ActiveSheet.Range("B1").End(xlDown))
        If Not IsEmpty(c) Then
            arraycount = arraycount + 1
            ReDim Preserve fromarray(arraycount)
            fromarray(arraycount) = c.Value
        End If
    Next
   
    numcomb = Application.WorksheetFunction.Combin(arraycount, choice)
    ReDim outputarray(numcomb, choice) ' storage for the results
   
    startpoint = 0
    Call combinations(startpoint, fromarray, arraycount, choice, outputarray)
   
    ' ##### now output results to Sheet2 ########
    For irow = 1 To numcomb
        For icol = 1 To choice
            Sheets("Sheet2").Cells(irow, icol).Value = outputarray(irow, icol)
        Next
    Next
    MsgBox "Done"
End Sub
Sub combinations(startpoint, source() As Long, size, depth As Long, output() As Long)
    ' do calcs for current level then do recursion
    Dim nextlevel As Long
    nextlevel = depth - 1
    For iloop = startpoint + 1 To size
        outputsinglerow(choice - depth + 1) = source(iloop)
        If depth > 1 Then
            Call combinations(iloop, source, size, nextlevel, output)
        Else
            outputpointer = outputpointer + 1
            If outputpointer > numcomb Then Exit For
            For jloop = 1 To choice
                output(outputpointer, jloop) = outputsinglerow(jloop)
            Next
        End If
    Next
End Sub


Viewing all articles
Browse latest Browse all 50158

Trending Articles