Excel VBA Macro Exercise
This is a tutorial for anyone wanting to learn how to write their first excel vba macro. These macros are very useful when we need to do a lot of manual work on large data tables and these manual actions can be automated by using a macro to execute every operation that we usually did with the mouse and keyboard and took us a few hours to complete.
Open an Excel file, right click the current Sheet name and select View Code.
Microsoft Visual Basic for Applications (VBA) opens and you are ready for your first macro.
Let’s say that we have the following range of numbers and we need a macro to verify each number if it’s greater that 50, then write in the next right cell a message (“Good number”) and also change the background of the cell into some color (Red).
The code is in the picture, but I will let you download the excel file at the end of this article.
Make sure that you save the excel file as .xlsm or in the old excel format .xls, as the .xlsx extension doesn’t save the macro code behind the excel workbook.
Run the excel vba macro in the worksheet by selecting Macros from Developer tab and click on Run. If you don’t have Developer tab, you must go to Excel Options, select Customize Ribbon and check the box beside Developer in the table to the right.
The result should look like this:
You can also insert a button to run the vba macro with one click. The button you need is in the upper left of the list bellow. A dialog box with all the available macros will open and you can select your macro from that list to be attached to the new button.
I hope that this tutorial was helpful and you are on the right path to becoming a great VBA programmer. Please leave a comment bellow for me to evaluate the quality of this article.
You can find here a great resource for your VBA study.