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.
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon 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!)
So, if my code looks something like this:
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?Sub Main() Call Sub1() Call Sub2() End End Sub Sub Sub1() ... End Sub Sub Sub2() ... End Sub
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon 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!)
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.
regards,Sub MainProcedure() Call SubProc1 Call SubProc2 End Sub Sub SubProc1() MsgBox "SubProc1 procedure ran" End 'this will prevent any further processing. The process will not be passed back to the MainProcedure End Sub Sub SubProc2() MsgBox "SubProc2 procedure ran" End Sub
Graham
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.
Hope this was useful or entertaining.
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:
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.Sub Main() End Sub Sub Secondary() MsgBox("Secondary Sub") End Sub
That's correct. VB behaves the same way, incidentally, as I believe does C++.
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
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks