ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Code for optimization, ReDim Preserve not handling it well, HELP please! (https://www.excelbanter.com/excel-programming/344438-code-optimization-redim-preserve-not-handling-well-help-please.html)

[email protected]

Code for optimization, ReDim Preserve not handling it well, HELP please!
 
Hi, I have a piece of code (pasted below) which basically gives me all
possible combinations depending on the input of variable n and k.

The last sub called 'PrintIt' prints the results onto the sheet.
However I want the results put into an array and not onto a sheet. Just
commenting out the sub 'PrintIt' does not work because of the line 'Col
= Col + 1'.

This line makes sure that each of the results are printed on a new
line, if I remove this sub the results get printed into an array but
they keep overwriting each other. I tried to fix this using 'ReDim
Preserve' however because you can only change the second dimensio this
did not work.

My question: does anybody have an idea what the best way is to make
sure that the results that are now pasted onto a sheet are pasted into
an array (the results are fine btw, so I need the exact same results
printed). I am really stuck here and some input would really be
appreciated!

The code:

Option Explicit

Public n As Long, k As Long, m As Long
Public b(1 To 100, 1 To 1) As Variant
Public Col As Long

Sub Run()

'clear old values
Range(Cells(9, 2), Cells(65536, 256)).ClearContents

n = Cells(2, 3).Value
k = Cells(3, 3).Value
Col = 0

Call Comb(1, 0)

End Sub

Sub Comb(j As Long, m As Long)

Select Case j
Case Is n
PrintIt
Case Else
If k - m < n - j + 1 Then
b(j, 1) = 0
Call Comb(j + 1, m)
End If
If m < k Then
b(j, 1) = 1
Call Comb(j + 1, m + 1)
End If
End Select

End Sub

Sub PrintIt()

Range(Cells(9, 2 + Col), Cells(8 + UBound(b, 1), 2 + _
Col)).Value = b
Col = Col + 1

End Sub


Thanks! Sarju



All times are GMT +1. The time now is 12:29 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com