Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 191
  • Last Modified:

Where to write code for VBA?

Hello,

Just as a clarification for best practice, can someone tell me........?

Say, I open Excel.  I have Book1.  I go into VB Editor, and I see in the project tree that I have VBAProject(Book1).  And if I open another new Worksheet, I get VBAProject(Book2).  Then under VBAProject, the tree breaks into "Microsoft Excel Objects", which then further breaks down into "Sheet1", "Sheet2"......"This Workbook".

My question is, I have an option of inserting a "MODULE", "CLASS MODULE" and "USER FORM".  When I write code, what should be put in where??????? for the sake of better code organization etc........????

I used to put everything into "This Workbook", but then it becomes a whole page of procedures.........

I'm only using Excel as an example, but I do alot of other VBA stuff for Engineering Applications.  Can someone please clarify the ussage of modules, objects, etc.....???

Regards,
Kenny
0
KC_78
Asked:
KC_78
  • 2
1 Solution
 
jmwheelerCommented:
Well in general you should write code the "closest" to where it is going to be used.  If you have a procedure that is only used by Sheet 1 then write it in Sheet1 if you have a procedure that is used for multiple sheets you could use ThisWorkbook or create a module.  You might consider putting subs under ThisWorkbook and functions in a module as functions should only return values and not make changes to Workbook or Sheet.  Hope this helps some.
0
 
leonstrykerCommented:
Place it the Module most of the time.  The only code you should place in This Workbook is behind the events.

Leon
0
 
doubleglazing2Commented:
I'd put procedures into modules and rename the modules (open 'properties' wiondow from 'view' menue).

I also found that code that is specific to certain sheets (that are reused and not deleted) is better if i put them into the 'Sheet1', 'Sheet2', code windows. That's because it cuts down on the coding and makes it easier to refer to the sheets e.g. the "Me.Name = "123"" code would work in any of the 'Sheets' code windows. BUT when you put them in the Modules, you have to write "Worksheets("SheetX").Delete" (X is a number).

I can't rememebr right now, but there are other little shortcutes in coding that let you refer to the object's code window you're programming in, that you can't do in modules.

It also depends on the size of code. It it's massive, then you better organise yourslef. Otherwiese its best to keep it in one window where yo ucan see all of it - saves having to move back and forth between different code windos!

Codes bits that work together a lot (i.e. maybe on calls the other one like 10 times ) are best put in one page because then you can out in breaks and see exactly how your code is running instead of it shifting off to a different code window everytime it calls another piece of code.
This also allows you to change coding more easily e.g. a variable passed from one procedure to another needs drastic change done to it - can be done more easily.

The way i dot it is like this: "Lazy Coding": the more easier it is to change the code i've written# - the better the layout is of the code. This is beacuse future changes are more easily made, coding progress is more easily observed, you know where ecerything is - so its eaier to get to the code you wanna look at.

Hop this helps.
0
 
doubleglazing2Commented:
Typos:
Should read:

"Codes bits that work together a lot (i.e. maybe on calls the other one like 10 times ) are best put in one page because then you can see exactly how your code is running instead of it shifting off to a different code window everytime it calls another piece of code.
This also allows you to change coding more easily e.g. a variable passed from one procedure to another needs drastic change done to it - can be done more easily."

Should read:

"The way i do it is like this: "Lazy Coding": the easier it is to change the code i've written - the better the layout of the code is . This is beacuse future changes are more easily made, coding progress is more easily observed, you know where everything is - so its easier to get to the code you wanna look at."

Apologies.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now