I have a bunch of VBA code that I'd like to throw in a macro.
Basically I call a bunch of subs that update the page.
It looks something like this:
Call Update1("Work", strConn)
Call Update2("Work", strConn)
Call Update3("Work", strConn)
where Update1, Upd开发者_Python百科ate2, and update3 are all in the same sheet with the form
Public Sub Update1(strPlace, strConn)
SQL code..
End Sub
How would I throw it all in a macro? Could I copy and paste all the code (including the calls?) That hasn't work. Neither has taking only the Subs. Not sure what's next.
All you should need is to insert a module as a container for all this code. Unless each of the UPDATE subs are specific to a single sheet, then they should probably be in the module as well.
All of your "call" statements will need to be in their own SUB myMain () ... END SUB
Once you've done this, you can either run it from the Tools, Macro list, or hook it into other events (a custom toolbar button or something else)
Edit: to be more precise, this is what your module should look like:
Sub myMainMacro()
'... initialize stuff
Call Update1("Work", strConn)
Call Update2("Work", strConn)
Call Update3("Work", strConn)
'... do more stuff
End Sub
Sub Update1(strPlace, strConn)
SQL code..
End Sub
Sub Update2(strPlace, strConn)
SQL code..
End Sub
'... other UPDATE SUBs here
Function CalculateValue(myInput as integer) as integer
'... do calculations here
CalculateValue = answer
End Function
All code (except for global variable declarations, which we haven't talked about) needs to be inside a SUB or a FUNCTION (a function returns a value, a SUB does not). Since they are all in the same module, the keyword PUBLIC isn't necessary.
精彩评论