+ Reply to Thread
Results 1 to 17 of 17

Sub handling in VBA

  1. #1
    Registered User
    Join Date
    04-06-2010
    Location
    Rochester, NY
    MS-Off Ver
    Excel 2003
    Posts
    8

    Sub handling in VBA

    Does VBA create a Sub Main() the way VB does if you don't include it in your code? I ask because I've been using a procedure called MainProcedure to call all my other subs from, and the program seems to run fine, but since I have no Main shouldn't the program run all my procedures twice? Once when called from MainProcedure, and again when going top-to-bottom through each sub as entered?
    Last edited by scudder12; 04-10-2010 at 03:15 AM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Sub handling in VBA

    Quote Originally Posted by scudder12 View Post
    I've been using a procedure called MainProcedure to call all my other subs from, and the program seems to run fine, but since I have no Main shouldn't the program run all my procedures twice?
    No, VBA appears to run the code in the order it is encountered. Subroutines inside other subs will complete then return to the same point in the original sub and proceed logically and in order until the code ends or aborts.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    04-06-2010
    Location
    Rochester, NY
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: Sub handling in VBA

    Quote Originally Posted by JBeaucaire View Post
    No, VBA appears to run the code in the order it is encountered. Subroutines inside other subs will complete then return to the same point in the original sub and proceed logically and in order until the code ends or aborts.
    So, if my code looks something like this:

    Please Login or Register  to view this content.
    Then the Main() will call the two subs in order and the end statement will terminate the program, preventing it from running sub1 and sub2 again, correct?

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Sub handling in VBA

    Quote Originally Posted by scudder12 View Post
    So, if my code looks something like this:

    Please Login or Register  to view this content.
    Then the Main() will call the two subs in order and the end statement will terminate the program, preventing it from running sub1 and sub2 again, correct?
    That is correct. Listing the related subs in the same module is completely safe.

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

    Re: Sub handling in VBA

    The End statement is unnecessary (and should be avoided on its own). The main sub will simply stop at end sub anyway.
    Remember what the dormouse said
    Feed your head

  6. #6
    Registered User
    Join Date
    04-06-2010
    Location
    Wellington, New Zealand
    MS-Off Ver
    Excel 2007
    Posts
    42

    Re: Sub handling in VBA

    Quote Originally Posted by romperstomper View Post
    The End statement is unnecessary (and should be avoided on its own). The main sub will simply stop at end sub anyway.
    Indeed, good advice. In the following example SubProc2 will never run because of the End statement in SubProc1. However, from an error handling perspective there may be times when you want to use End in called procedures to prevent further processing.

    Please Login or Register  to view this content.
    regards,
    Graham

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

    Re: Sub handling in VBA

    Quote Originally Posted by parrynz View Post
    IndHowever, from an error handling perspective there may be times when you want to use End in called procedures to prevent further processing.
    I would tend to disagree with that. Either use a Function (or passed variable) that indicates success, or don't error trap in the called sub and allow the calling sub to do it. End is too brutal for my liking as it kills your variables.

  8. #8
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    1,187

    Re: Sub handling in VBA

    The way I think about it is : if I were designing a (simple) spreadsheet program I would need a form with a grid control (in lieu of full spreadsheet functionality) and some buttons.

    Main displays my form with the grid control and the user doesn't need access to that. But I will allow them to write macros to tie into events related to the form eg buttonClick, formClose.

    So in Excel main starts excel provides the UI etc. Whereas VBA just provides user defined code for methods/events. You can have macros that run on Worksheet_Open, Cell_Change, Before_Save etc When these terminate they don't terminate the program they just return to "waiting for user input". The oddity appears to be when you directly run a sub. But this is still just running an event created by user input.

    The END only occurs when you close down Excel.

    I agree with RomperStomper you should never need to use END (on its own). If you think you do it is nearly always poor coding elsewhere.


    click on the * Add Reputation if this was useful or entertaining.

  9. #9
    Registered User
    Join Date
    04-06-2010
    Location
    Rochester, NY
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: Sub handling in VBA

    Ok, just so I'm perfectly clear: As I understand it, as soon as the runtime hits an "End Sub" statement the macro terminates, regardless of whether or not there are other subroutines within the same module?

    Thus if my module looks like this:

    Please Login or Register  to view this content.
    the message box will never come up because once the End Sub statement for Main() is reached the macro will terminate. Seemed to work out that way when I tested it, just want to make sure I'm understanding correctly. I seem to recall C++ and VB needing an explicit end statement to prevent it from just running top-to-bottom through all the subroutines in a given module, but I could be mis-remembering.

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

    Re: Sub handling in VBA

    That's correct. VB behaves the same way, incidentally, as I believe does C++.

  11. #11
    Registered User
    Join Date
    04-06-2010
    Location
    Wellington, New Zealand
    MS-Off Ver
    Excel 2007
    Posts
    42

    Re: Sub handling in VBA

    Quote Originally Posted by romperstomper View Post
    I would tend to disagree with that. Either use a Function (or passed variable) that indicates success, or don't error trap in the called sub and allow the calling sub to do it. End is too brutal for my liking as it kills your variables.
    Yes I agree. My intention was to demonstrate what happens when you use it as the Op has a VB background. I use error handling within my main procedures myself not End but nevertheless the option is still available.

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

    Re: Sub handling in VBA

    Quote Originally Posted by parrynz View Post
    Yes I agree. My intention was to demonstrate what happens when you use it as the Op has a VB background. I use error handling within my main procedures myself not End but nevertheless the option is still available.
    A bit like closing Excel by killing its process though.

    Are you on holiday from MrExcel?

  13. #13
    Registered User
    Join Date
    04-06-2010
    Location
    Wellington, New Zealand
    MS-Off Ver
    Excel 2007
    Posts
    42

    Re: Sub handling in VBA

    Quote Originally Posted by romperstomper View Post
    A bit like closing Excel by killing its process though.

    Are you on holiday from MrExcel?
    Just a change. I created a FIFA World Cup spreadsheet which I posted in the Development forum so I thought it would be a tad rude to just post that and not contribute to the site. Even though I did not give the most precise advice first up! :-)

    regards,
    Graham

  14. #14
    Registered User
    Join Date
    04-06-2010
    Location
    Rochester, NY
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: Sub handling in VBA

    Quote Originally Posted by romperstomper View Post
    That's correct. VB behaves the same way, incidentally, as I believe does C++.
    Well, that's what I get for doing no programming for almost a decade and then deciding "I'm sure it'll come back to me once I get into it"

    Thanks all for the help, much appreciated!

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

    Re: Sub handling in VBA

    Quote Originally Posted by parrynz View Post
    I created a FIFA World Cup spreadsheet which I posted in the Development forum so I thought it would be a tad rude to just post that and not contribute to the site.
    I saw (and downloaded) that in the Lounge yesterday - along with Richard's impressive optimism... Looks very good, so thanks!

  16. #16
    Registered User
    Join Date
    04-06-2010
    Location
    Wellington, New Zealand
    MS-Off Ver
    Excel 2007
    Posts
    42

    Smile Re: Sub handling in VBA

    Quote Originally Posted by romperstomper View Post
    I saw (and downloaded) that in the Lounge yesterday - along with Richard's impressive optimism... Looks very good, so thanks!
    My pleasure!

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

    Re: Sub handling in VBA

    I thought the anthems were a nice touch too.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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