Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Easy explanation of modules?

Status
Not open for further replies.

Maine

Technical User
Feb 4, 2001
41
US
I have one spreadsheet file. The file has 8 worksheets. Each worksheet has two buttons on it with macros attached to them - the buttons on each sheet do the same thing. Is there an easy explanation of modules somewhere? How many modules should I have and why? As I add new buttons etc. do they go into their own module?
 
Thanks very much. I have printed out most of the information on this website. I haven't seen the answer to my question in it yet though. If I have 8 worksheets with macros etc. on each would I have 8 modules or would I have more?
 
The number of modules, macro routines, and sheets are all independent from each other.

An Excel file can have 8 sheets, 2 macro routines, and one module.

Cutting and pasting code from one module to another is very easy and often desirable. You can then delete the unused modules and the macros will still work perfectly well.

It is common to have more than one module because someone has turned the Record New Macro feature on, which seems to always record in an entirely new module. Normally I test the new macro, and when it's OK, paste into the desired module and delete the old.
 
It sounds like you have 2 macros that need to run on each of 8 worksheets. You could insert a module into the VBA project. Then write the 2 main macros using Public Sub. You can use the ActiveSheet object so that the macro will perform the actions on the active sheet rather than having to explicitly specify the sheet name. Then, on each of your sheets, you can insert the two buttons. Behind the buttons, just use a Call statment to your main macros. This will prevent duplicate code.

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top