When we encounter a new language not to mention a programming language, it is understandable to be apprehensive about it. If you are trying to automate an Excel spreadsheet using VBA programming, it can seem especially daunting if you have never programmed before. The following tips and tricks will help you become more proficient and comfortable in your coding experience.
1. Using Recorder
One of the best tools you can use while creating your VBA is to use the recorder button to record a Macro. This will autogenerate the VBA code for you. While you may need to make some small changes to this code to better suit your needs, it should guide you in the right direction and approach.
The best way to facilitate code generation is to record a different macro for each step and combine it into your final programme. Which brings us to the next tip …
2. Following a modular approach
Try to break-up your solutions into multiple steps and tackle them separately. Create separate sub routines for tasks which are repeated.
Need to open a different file every time? – make a small sub-routine which accomplishes this task.
Modular approach will do wonders to the ease of programming. This approach also reduces coding errors and makes your programme easier to understand.
3. Forcing declaration of Variables
Compared to some other programming languages like C, C++, Java, etc., VBA takes a lax approach to variables, in that, it allows the use of a variable without declaring them beforehand.
This approach while easy to use at first, frequently leads to coding errors. To force yourself to declare a variable before using it in a module, use the following command at top of your module:
4. Disabling Excel Alerts
Some Excel actions require a confirmation on Excel in form of alerts – e.g., deleting a sheet with some data or formatting on it pops up an Excel confirmation alert. You can disable these alerts in VBA by using the following code before the coding area where you expect the alert/s:
Application.DisplayAlerts = False
Later, the alerts can be re-enabled with the command:
Application.DisplayAlerts = True
Hopefully, these tips will help you along your journey to VBA programming excellence. All the very best!