Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Recursive Code


Hi all,

I am re-visiting my initial query (lost in traffic) hoping this time
someone will kindly oblige me. In my desperation to -understand- the
mechanics of this code credited to Tom Ogilvy , I threw in MsgBoxes in
strategic places so to trap and monitor the changing values of the
respective variables and not least the worksheet output. The more
trappings I did, the more I became mystified. Assuming activecell is
A1, and using n = 9 and m = 3, the code populates Column A with all the
84 possible combinations - selecting 3 items at a time from 1,2,3 ...
9.

Sub Combinations()
Dim n As Integer, m As Integer
numcomb = 0
'n = InputBox("Number of items?", , 10)
'm = InputBox("Taken how many at a time?", , 3)
n=9
m=3

com n, m, 1, "'"
End Sub

'Generate combinations of integers k..n taken m at a time, recursively
Sub com(ByVal n%, ByVal m%, ByVal k%, ByVal s as String)
If m n - k + 1 Then Exit Sub
If m = 0 Then
ActiveCell = s
ActiveCell.Offset(1, 0).Select
MSGBOX M & \" \" & K & \" \" & S
Exit Sub
End If
com n, m - 1, k + 1, s & k & " "
MSGBOX M & \" \" & K & \" \" & S
com n, m, k + 1, s
MSGBOX M & \" \" & K & \" \" & S
End Sub


Myles


--
Myles
------------------------------------------------------------------------
Myles's Profile: http://www.excelforum.com/member.php...o&userid=28746
View this thread: http://www.excelforum.com/showthread...hreadid=571143

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Recursive Code

Hi Myles,

A Recursive procedure calls itself passing same set of arguments to process.
Typically something like this -

'code
If not done enough then
'code
call myself again
End if

or in your example

If done enough then
'code
exit Sub
End if
'code
call myself again

Each new instance of the procedure starts afresh with it's own set of any
declared variables, ie values don't persist into the next level unless
declared as Static.

Need to ensure eventually there will be no need to 'call myself again' or
you'll run out of stack space.

A simplistic and contrived example, pads a string a single "A" until the
required length is built -

Sub testRecursive()
Dim s As String
s = "w"
recursive s, 5
MsgBox s
End Sub

Sub recursive(sIn As String, nLen As Long, _
Optional ByVal nLevel As Long)
Dim sp As String

nLevel = nLevel + 1
sp = Space(nLevel)
Debug.Print sp & "Enter" & nLevel, sIn

If Len(sIn) < nLen Then
sIn = sIn & "A"
recursive sIn, nLen, nLevel
End If

Debug.Print sp & "Exit" & nLevel, sIn
End Sub

Press ctrl-g to see the debug results in the intermediate window.

In the example you posted you could pass the range (eg activecell) and an
offset counter to increment as arguments which would avoid the need to
select the next cell each time.

Regards,
Peter T


"Myles" wrote in
message ...

Hi all,

I am re-visiting my initial query (lost in traffic) hoping this time
someone will kindly oblige me. In my desperation to -understand- the
mechanics of this code credited to Tom Ogilvy , I threw in MsgBoxes in
strategic places so to trap and monitor the changing values of the
respective variables and not least the worksheet output. The more
trappings I did, the more I became mystified. Assuming activecell is
A1, and using n = 9 and m = 3, the code populates Column A with all the
84 possible combinations - selecting 3 items at a time from 1,2,3 ...
9.

Sub Combinations()
Dim n As Integer, m As Integer
numcomb = 0
'n = InputBox("Number of items?", , 10)
'm = InputBox("Taken how many at a time?", , 3)
n=9
m=3

com n, m, 1, "'"
End Sub

'Generate combinations of integers k..n taken m at a time, recursively
Sub com(ByVal n%, ByVal m%, ByVal k%, ByVal s as String)
If m n - k + 1 Then Exit Sub
If m = 0 Then
ActiveCell = s
ActiveCell.Offset(1, 0).Select
MSGBOX M & \" \" & K & \" \" & S
Exit Sub
End If
com n, m - 1, k + 1, s & k & " "
MSGBOX M & \" \" & K & \" \" & S
com n, m, k + 1, s
MSGBOX M & \" \" & K & \" \" & S
End Sub


Myles


--
Myles
------------------------------------------------------------------------
Myles's Profile:

http://www.excelforum.com/member.php...o&userid=28746
View this thread: http://www.excelforum.com/showthread...hreadid=571143



Reply
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
Recursive Code: How does it work? Myles[_67_] Excel Programming 0 August 8th 06 03:28 PM
Help with Recursive Call? mark Excel Programming 9 May 25th 06 08:44 PM
Recursive Functio help BigBobbo Excel Worksheet Functions 1 May 10th 06 07:23 PM
recursive sums Joe Excel Worksheet Functions 6 July 17th 05 09:45 AM
Recursive Subs? ExcelMonkey[_74_] Excel Programming 5 February 5th 04 02:54 AM


All times are GMT +1. The time now is 02:00 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"