How to organize my VBA code

nelson41412

New Member
Joined
Jul 8, 2022
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hello,

I'm entirely self taught and I like to say that I'm starting to get "dangerous" with my abilities. However, I don't have a mentor and no one at my company does similar work in Microsoft VBA's so I might be committing some coding crimes. I'm looking for tips on how to organize code, how to organize modules, what should be global vs. private, when does it make sense to create a new sub and call programs vs just a big sub, etc. Any references or suggestions would be very helpful!

For an idea of my level of capability, I'm working on a one click/point of entry solution that will create a set of data for an entry based on existing formulas, add that set of data to an excel database, then run a mail merge in a word file for a particular 'address', then save the printed file to a set location.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Organize is sort of a loose term I think, and what works for you might not be attractive to someone else. You posted this in "other applications" so not sure if you had any other app specifically in mind. The majority of my vba is in Access. I tend to group procedures into modules and use module names that describe the intent (e.g. mdlUserFunctions, mdlDbFunctions and the like) although sometimes the purpose of procedure would fit in more than one place.

Public variables (aka Global) should be kept to a minimum IMO. Suggest research variable scope in vba, esp. where it covers Static variables. A key factor should be developing with reusability in mind, even to the point of creating code libraries if you might end up with lots of code that can be used over several projects. F'rinstance, a procedure that can accept a form object as a parameter can be much more powerful than one that only accepts a form property such as its name. Not only that, it becomes reusable as you could pass any form to it. Same might go for objects such as listboxes or other controls. Another example of a reusable procedure might be the case of calling one of the msoFileDialog pickers. It might make for a long list of parameters in the procedure as there are several properties that you might want to employ so the balance of one procedure vs repeating the same code every time might be driven by factors such as usage and complexity desired. Keep in mind that only a function can return a value, so if in the dialog case you want to know if .Show was true or not, you have to use a function. You must also use functions if you specify a custom procedure as an event property (again, basing that on Access).
HTH some. FWIW, I am self taught also.
 
Upvote 0
Solution

Forum statistics

Threads
1,215,086
Messages
6,123,035
Members
449,092
Latest member
ikke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top