If you are here, its probably you want to learn how to run a subroutine every time a change is made in a specific sheet. This article will teach exactly that. So let's get started.
The Syntax for the Worksheet Change Event Handler is:
Private Sub worksheet_change(ByVal Target As Range)
'your code
'
'
End Sub
This code must be written in a worksheet object in order to work, not in a normal module.
Private Sub worksheet_change(ByVal Target As Range) Msgbox "You updated something in this sheet" End Sub
If you have this code on sheet1 and you write or delete something in the sheet1, the massage will pop that "You updated something in this sheet".
It won't work in normal modules. But you can call subroutines from any module. We will do the same in this session.
Scenario:
So, let's say we have a macro that changes the color of cells based on some condition. Just like Conditional Formating of Excel. The only difference is that the color of cells changes when we run this VBA code using a button or run command, not when the values change. We want the macro to run whenever a change is made in the sheet. Here VBA Events will help. Now to do this we will use Worksheet Change Event.
Follow these steps:
Private Sub Worksheet_Change(ByVal Target As Range) End Sub
worksheet_change(Target as Range) is a preserved subroutine that runs when a change is made on the code containing sheet.
When you will write this code, you will see the object changing to the worksheet. See the upper-left drop-down. In the upper-right drop-down, the event is "change".
If you use the drop-down and select the worksheet in the left drop-down, all the available events will be shown in the right dropdown. Whichever drop-down you choose, the code for that event will be written automatically. You just need to fill it with your custom subroutines.
Private Sub worksheet_change(ByVal Target As Range)
Call FormatUsingVBA
End Sub
Now, whenever we will make a change in sheet2 the macro FromatUsingVBA will run.
So yeah guys, this how can make run a VBA script when a sheet changes its content, using Worksheet Change Event handler. I hope it was helpful. If you have any queries regarding this event handler, mention it in the comments section.
Related Articles:
Simplest VBA Code to Highlight Current Row and Column Using | Use this small VBA snippet to highlight the current row and column of the sheet.
The Worksheet Events in Excel VBA | The worksheet event are really useful when you want your macros run when a specified event occurs on the sheet.
Run Macro If Any Change Made on Sheet in Specified Range | To run your macro code when the value in a specified range changes, use this VBA code. It detects any change made in the specified range and will fire the event.
Popular Articles:
50 Excel Shortcuts to Increase Your Productivity | Get faster at your task. These 50 shortcuts will make you work even faster on Excel.
The VLOOKUP Function in Excel | This is one of the most used and popular functions of excel that is used to lookup value from different ranges and sheets.
COUNTIF in Excel 2016 | Count values with conditions using this amazing function. You don't need filter your data to count specific value. Countif function is essential to prepare your dashboard.
How to Use the SUMIF Function in Excel | This is another dashboard essential function. This helps you sum up values on specific conditions.
The applications/code on this site are distributed as is and without warranties or liability. In no event shall the owner of the copyrights, or the authors of the applications/code be liable for any loss of profit, any problems or any damage resulting from the use or evaluation of the applications/code.