Forum Discussion
Martyn Rowlands
Jun 25, 2018Copper Contributor
Best practice for VBA/Macros in Excel
Hi Everyone, I am just starting a project to review complex spreadsheets that contain a lot of VBA and Macros. Is there some best practice around on this? A lot of what people are using is old, ...
RalphB57
Sep 20, 2019Copper Contributor
Hi,
The best practice is to maintain the mindset that VBA is a powerful object oriented programming language, not just a macro environment.
- Choose meaningful variable, resource and method names.
- Declare variables and resources at the start of any methods.
- Release/close any resources at the end of your methods.
- Always use Option Explicit.
- Indent your code.
- Comment your code.
- And always think before you start coding.
Refactoring someone else's code is always a frustrating journey but the satisfaction you get when everything works the way you need it to is really rewarding. Enjoy 👍
The best practice is to maintain the mindset that VBA is a powerful object oriented programming language, not just a macro environment.
- Choose meaningful variable, resource and method names.
- Declare variables and resources at the start of any methods.
- Release/close any resources at the end of your methods.
- Always use Option Explicit.
- Indent your code.
- Comment your code.
- And always think before you start coding.
Refactoring someone else's code is always a frustrating journey but the satisfaction you get when everything works the way you need it to is really rewarding. Enjoy 👍