+ Reply to Thread
Results 1 to 18 of 18

Calling a recursive Sub in a 'for' loop in another Sub

  1. #1
    Registered User
    Join Date
    05-18-2012
    Location
    india
    MS-Off Ver
    Excel 2007
    Posts
    22

    Calling a recursive Sub in a 'for' loop in another Sub

    Hi,

    I have written a code of following format -

    Please Login or Register  to view this content.
    As you see, I have 2 subs Sub1 and Sub2. Sub1 has a 'for' loop inside which Sub2 is called. Sub2 is itself a recursive Sub which keeps on looping till p becomes Nothing. I observed that even before Sub2 finishes its first pass, the index i in Sub1 advances and starts advances to second pass. Can somebody advise me on how to remove this problem?

    Thanks a lot,
    excelworker_1

  2. #2
    Registered User
    Join Date
    06-11-2012
    Location
    Bangalore
    MS-Off Ver
    Excel 2007
    Posts
    86

    Re: Calling a recursive Sub in a 'for' loop in another Sub

    Dear,



    Sub aaa()

    'does some calculations
    Dim a As Long
    a = 1
    Dim b As Long
    b = 100
    For i = a To b
    Call bbb
    Next i

    End Sub

    Sub bbb()

    Dim p
    'define p

    If IsEmpty(p) Then
    'do some calculations
    Else
    Call aaa
    End If

    End Sub


    as you can see you don't have any loop in sub2.so it not repeat as many any loop.then no times it will in loop will depend on no of times for statement executes in sub 1.

    and you are calling sub 1 from sub2 only if your object is not nothing 1.e: it has some object assigned to it.

    the whole of this looping will depend on other parts of your code.which is missing.
    specially your object variable p.

  3. #3
    Registered User
    Join Date
    05-18-2012
    Location
    india
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Calling a recursive Sub in a 'for' loop in another Sub

    Hi Md Aejez,

    Thanks for the prompt reply. I did not exactly understand what you said. Here is a more detailed version of my code -
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Could you now please guide me?

    Thanks a lot,
    excelworker_1

  4. #4
    Registered User
    Join Date
    06-11-2012
    Location
    Bangalore
    MS-Off Ver
    Excel 2007
    Posts
    86

    Re: Calling a recursive Sub in a 'for' loop in another Sub

    Dear,

    you are calling the Sub2 from Sub2 so it is going under loop .

    in order to avoid the loop in sub2 remove the last 3rd line(call sub2).

  5. #5
    Registered User
    Join Date
    05-18-2012
    Location
    india
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Calling a recursive Sub in a 'for' loop in another Sub

    Hmm. But removing the loop will negate the purpose of why I'm doing this. I mean, I want to loop through Sub2 till p is set to Nothing and once that happens, I want to exit the Sub2. That is why I've written the code that way.

  6. #6
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Calling a recursive Sub in a 'for' loop in another Sub

    Hi

    Why don't you just loop through whatever it is that you are doing to set the unionrange and process each one on its merits?

    rylo
    Last edited by rylo; 06-15-2012 at 01:22 AM.

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Calling a recursive Sub in a 'for' loop in another Sub

    I agree with rylo -- recursion is always a choice, not a requirement. There is nothing that can be achieved by recursion that can't be achieved by iteration -- maybe a shade less elegantly, but probably faster in execution.
    Last edited by shg; 06-15-2012 at 01:24 AM.
    Entia non sunt multiplicanda sine necessitate

  8. #8
    Registered User
    Join Date
    05-18-2012
    Location
    india
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Calling a recursive Sub in a 'for' loop in another Sub

    True. But I tried the following iteration-type loop too -
    Please Login or Register  to view this content.
    This one doesnt work either. It goes in a perpetual loop and Excel gets stuck.

  9. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Calling a recursive Sub in a 'for' loop in another Sub

    That won't work at all -- unionrange is not initialized.

    If it were initialized, and the value was found, it would loop forever because the loop doesn't do anything to affect the contents of the range, so the same value would be found repetitively (unless you have so NOW() formula in there).

    Rather than beating around hypothetical code fragments, why not just explain what you're trying to do?
    Last edited by shg; 06-15-2012 at 01:36 AM.

  10. #10
    Registered User
    Join Date
    05-18-2012
    Location
    india
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Calling a recursive Sub in a 'for' loop in another Sub

    Sorry for the typo. I forgot to add initialization of unionrange1 in that. In my actual code, I have in fact initialized unionrange1 and assigned a value to it.

  11. #11
    Registered User
    Join Date
    06-11-2012
    Location
    Bangalore
    MS-Off Ver
    Excel 2007
    Posts
    86

    Re: Calling a recursive Sub in a 'for' loop in another Sub

    Sub Sub2()
    Dim p as Variant
    Set p = unionrange1.Find("#N/A Requesting Data...", LookIn:=xlValues)
    Dim n As Long
    if p.cells.count <> 0 then
    for y= 1 to p.cells.count
    unionrange1(y).Calculate
    DoEvents
    next y
    end if
    'then do some calculations
    Exit Sub





    Try this

  12. #12
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Calling a recursive Sub in a 'for' loop in another Sub

    In my actual code, I have in fact initialized unionrange1 and assigned a value to it.
    That doesn't diminish my second point, and reinforces the third.

  13. #13
    Registered User
    Join Date
    05-18-2012
    Location
    india
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Calling a recursive Sub in a 'for' loop in another Sub

    Ok I'm sorry. Here I'll try to explain my attempt a bit better -

    I have a folder with a main workbook and 200 other workbooks. In Sub1, the 'for loop' loops through each workbook, opens it, does some calculations, then calls Sub2 (which is used for pulling data from Bloomberg), then does some calculations, saves and closes that workbook and moves to next workbook.

    Thus for Sub2, I cannot do these 'some more calculations' until all data has been pulled from Bloomberg for the workbook in question. Hence by the code which I've written, I've tried to introduce a mechanism which ensures *all* data has been pulled (and saved in cells in unionrange1) and then proceed to do the calculations.

    Hope this puts my code and doubts in perspective now. Please help me. I've spent a lot of time in past 3-4 days trying to finish this, but have failed.

    Thanks,
    excelworker_1

  14. #14
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Calling a recursive Sub in a 'for' loop in another Sub

    How does the data come in from Bloomberg? What's the code to do that?

  15. #15
    Registered User
    Join Date
    05-18-2012
    Location
    india
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Calling a recursive Sub in a 'for' loop in another Sub

    Just above "Call Sub2" in the for loop in Sub1, I have "Call FetchBloombergData" where FetchBloombergData is a Sub. The code is as follows -

    Sub FetchBloombergData()

    Worksheets("DataSheet").Cells(2, 31).Formula = "=bds(" & Chr(34) & Worksheets("DataSheet").Cells(2, 1) & " IS Equity" & _
    Chr(34) & ", " & Chr(34) & "dvd_hist" & Chr(34) & ")"
    End Sub

    Basically I've 'hard-wired' Excel 's Bloomberg formula into VBA code. I have checked if there is any error in this code, and there isn't. It does manage to pull data successfully from Bloomberg.

  16. #16
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Calling a recursive Sub in a 'for' loop in another Sub

    For context see this thread.
    Remember what the dormouse said
    Feed your head

  17. #17
    Registered User
    Join Date
    05-18-2012
    Location
    india
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Calling a recursive Sub in a 'for' loop in another Sub

    Thanks for the link romperstomper. Hope it will help others understand this problem better. I tried all suggestions made by you (and also some stuff on my own), but have not been able to figure this out.

  18. #18
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Calling a recursive Sub in a 'for' loop in another Sub

    Why wouldn't you have just continued in that thread instead of starting a new one?? It shouldn't be necessary to have 15 cat-and-mouse posts to drag out what you're trying to do -- again.
    Last edited by shg; 06-15-2012 at 10:59 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1