In this tutorial you will learn how to start a macro when a worksheet changes. This is a follow up tutorial from the creating a macro tutorial. In that tutorial, the goal was to have a macro that would sort records automatically when a new line was added.
In order to run this macro automatically when the worksheet changes, you need to add the macro code in the “worksheet change” function. Excel has many automated functions that run based on events like before saving the workbook, when the workbook changes, etc.
More details about these functions here in the post about VBA
To get to these functions, go to the Developer Tab on the ribbon and click on the Visual Basic. If you don’t have the developer tab, learn how to enable it here.
Once you reached Visual Basic, double click on the Sheet1 to open the Visual Basic code window.
Notice all the actions that Excel already has for a Worksheet. Click on Worksheet Change and Excel will create a blank function for the case when anything changes on a worksheet (for example when a new line is added)
The macro code can be found in the module 1 section. If you want more details about how the macro was recorded, see this post.
You have two choices. You can copy the macro code in the “Worksheet_Change” function
or call the macro by its function name.
So let’s see how this looks
If you want to start the macro on demand instead of automatically, you can add a button that would launch it