LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default 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

 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel code optimization deepika :excel help[_2_] Excel Discussion (Misc queries) 5 January 29th 08 01:02 PM
VBA syntax help: ReDim Preserve an array Dave Excel Discussion (Misc queries) 4 September 8th 07 07:37 PM
Redim Preserve doesn't work Witek[_2_] Excel Programming 3 November 1st 04 11:34 PM
ReDim, Preserve and Multidimensional arrays Andy Westlake[_2_] Excel Programming 3 October 19th 04 07:04 PM
Pivot Table - Code Optimization kaon[_35_] Excel Programming 3 August 20th 04 02:24 AM


All times are GMT +1. The time now is 04:51 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"