Hello, Friends,
The following vba is used to combine "n" numbers (written in column A), picked "p" by "p" (written in cell "B1").
Public NumRows As Long
Public NumCols As Long
Public j As Long
Public ColStep As Long
Public StartCol As Long
Sub Combinations()
Dim rRng As Range, p As Integer
Dim vElements, lRow As Long, vresult As Variant
StartCol = 3 'Starts in Column "C"
ColStep = 14 'Column Step
j = StartCol
NumRows = ActiveSheet.Rows.Count
NumCols = ActiveSheet.Columns.Count
Set rRng = Range("A1", Range("A1").End(xlDown)) ' The set of numbers
p = Range("B1").Value ' How many are picked
vElements = Application.Index(Application.Transpose(rRng), 1, 0)
ReDim vresult(1 To p)
Call CombinationsNP(vElements, p, vresult, 1, 1)
End Sub
Sub CombinationsNP(vElements As Variant, p As Integer, vresult As Variant, iElement As Integer, iIndex As Integer)
Dim i As Integer
For i = iElement To UBound(vElements)
vresult(iIndex) = vElements(i)
If iIndex = p Then
Cells(Application.Rows.Count, j).End(xlUp).Offset(1, 0).Resize(, p) = vresult
Cells(Application.Rows.Count, j).End(xlUp).Select
If (ActiveCell.Address = Cells(NumRows - 1, j).Address) Then
j = j + ColStep
If ((j + p) > NumCols) Then
Sheets.Add After:=Sheets(ActiveSheet.Name)
j = StartCol
End If
Cells(1, j).Select
Else
Cells(Application.Rows.Count, j).End(xlUp).Select
End If
Else
Call CombinationsNP(vElements, p, vresult, i + 1, iIndex + 1)
Cells(Application.Rows.Count, j).End(xlUp).Select
End If
Next i
End Sub
The Windows Task Manager shows that the memory usage is only 260MB !!!!
So, I would like to ask your help to improve this code in order to INCREASE THE MEMORY USAGE.
Thanks,
John
Bookmarks