Solved

Where to write code for VBA?

Posted on 2004-04-06
4
183 Views
Last Modified: 2010-05-02
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
Comment
Question by:KC_78
  • 2
4 Comments
 
LVL 11

Expert Comment

by:jmwheeler
ID: 10767611
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
 
LVL 29

Expert Comment

by:leonstryker
ID: 10768377
Place it the Module most of the time.  The only code you should place in This Workbook is behind the events.

Leon
0
 
LVL 1

Expert Comment

by:doubleglazing2
ID: 10790615
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
 
LVL 1

Accepted Solution

by:
doubleglazing2 earned 100 total points
ID: 10791638
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Visual Basic Excel Formatting error 4 128
Updates not working for MS Windows 7 12 183
VB6 Compile Compatibility Issue 4 116
VB6 - Scroll Mouse wheel on Picturebox 13 79
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question