Excel VBA Worksheet Change Event Based on Multiple Cells

Looking for ways to use VBA Worksheet Change event based on multiple cells in Excel? Then, this is the right place for you.

To automate a task in Excel, you need to use the VBA Worksheet Change event. This post will give you clear instructions and useful examples to help you master this powerful feature, regardless of whether you need to track changes in continuous or non-continuous ranges. We’ll give you a clear, step-by-step tutorial in this blog post on how to use the VBA Worksheet Change Event Multiple Cells to monitor and react to changes in multiple cells.


How to Use VBA Worksheet Change Event Based on Multiple Cells in Excel: 2 Useful Examples

In the section that follows, we will examine two easy methods for managing the Worksheet_Change event based on multiple cells. Your VBA code will be more robust and flexible thanks to these methods, which give you the freedom to initiate particular actions when changes take place within a predetermined range.

A Sample Dataset


1. VBA Worksheet Change Event Based on the Continuous Multiple Cells

In some circumstances, you might need to keep an eye on changes in a continuous range of cells rather than just a single cell. This blog post will walk you through an easy way to set up and modify the Worksheet_Change event handler for multiple cells in a continuous range, even if you have little to no experience with VBA.

Steps:

  • In Excel, you can open the VBA Editor by pressing “Alt + F11” or by going to the “Developer” tab and selecting the “Visual Basic” button.
  • Locate and choose the worksheet where you want to track changes in the VBA Editor.
  • Enter the Worksheet Change Event code in the worksheet’s code module. Keep in mind that you must enter the event in the worksheet’s code module; you cannot modify it in a regular module. To do that, you can right-click on the sheet name and click on View Code.
  • The worksheet’s code module should now contain the following VBA code:

Code:

Private Sub Worksheet_Change(ByVal Target As Range)
    ' Specify the cells you want to monitor
    Dim monitoredRange As Range
    Set monitoredRange = Range("D5:D14") ' Modify this range as needed
    ' Check if the changed cells are within the monitored range
    If Not Intersect(Target, monitoredRange) Is Nothing Then
        ' Perform actions based on the changed cells
        ' Add your code here
        MsgBox "One of the monitored cell has been changed!"
    End If
End Sub

Entering the VBA Code in Sheet’s Module

Note
It is important to know the Target parameter when working with events in Excel VBA. In the Worksheet_Change event, it represents the cell or cells that trigger the event. In other words, the Target parameter defines the range that has been changed.

VBA Explanation:

Private Sub Worksheet_Change(ByVal Target As Range)
  • Beginning with this line, we initiated the Worksheet_Change event procedure. It specifies that the code will execute each time the worksheet is changed.
Dim monitoredRange As Range
  • This line declares a Range-type variable with the name monitoredRange. The range of cells you want to keep an eye on changes will be stored in this variable.
    Set monitoredRange = Range("D5:D14") ' Modify this range as needed
  • The monitoredRange variable is set to the set of cells that you want to track in this line. It is set to the cells D5 to D14 in this illustration. This range can be changed to meet your unique needs.
    If Not Intersect(Target, monitoredRange) Is Nothing Then
  • This line determines whether the monitoredRange and the changed cells (Target) cross. The overlapping region between two ranges is represented by the range object that the intersect function returns. The changed cells are in the monitored range if the Intersect function returns a non-null value.
        MsgBox "One of the monitored cells has been changed!"
  • This line indicates the presence of a change within the monitored range by displaying a message box with the text “One of the monitored cells has changed!”
    End If
  • The If statement block ends with this line.
End Sub
  • The “Sub” block ends with this line.
  • In the following image, you can see the final result of changing or deleting cell values. After any kind of changes, Microsoft Excel will show this message “One of the monitored cells has been changed!”

A Message Box After Changing a Cell


2. Worksheet Change Event Based on the Non-Continuous Multiple Cells

With simple steps for users with limited VBA knowledge to implement and modify as necessary, this blog post will walk you through an easy way to handle the Worksheet_Change event for multiple non-continuous cells.

Steps:

  • By pressing “Alt + F11” or by going to the “Developer” tab and choosing the “Visual Basic” button in Excel, you can launch the VBA Editor.
  • Find and select the worksheet in the VBA Editor where you want to keep track of changes.
  • Enter the Worksheet Change Event code in the worksheet’s code module. You must enter the event in the worksheet’s code module; you cannot change it in a regular module, so keep that in mind.
  • The following VBA code ought to now be present in the worksheet’s code module:

Code:

Private Sub Worksheet_Change(ByVal Target As Range)
    ' Specify the cells you want to monitor
    Dim monitoredRange2 As Range
    Set monitoredRange2 = Range("D5:D7,D10:D14") ' Modify this range as needed
    ' Check if the changed cells are within the monitored range
    If Not Intersect(Target, monitoredRange2) Is Nothing Then
        ' Perform actions based on the changed cells
        ' Add your code here
        MsgBox "One of the monitored cells has changed!"
    End If
End Sub

Entering the VBA Code with worksheet change event

  • After changing the D7 cell, one of the cells in the range of D4:D7, Microsoft Excel will show a message

A Message Box After Changing a Cell

  • However, the cell out of the ranges D4:D7 and D10:D14 has been changed, as in the following image. In response to that changes, Microsoft Excel.

A Has Been Changed Outside the Range of Change Events


How to Solve If VBA Worksheet Change Event is Not Working in Excel

There are times when an event might not go as planned. In this blog post, we will examine typical causes for the Worksheet Change event’s potential malfunction and offer workable solutions to VBA Worksheet Change event not working problem. A sample VBA code will also be provided, with the highlighted error serving to prevent the event from triggering.

Solution:

  • You can start the VBA Editor by pressing “Alt + F11” or by going to the “Developer” tab and selecting the “Visual Basic” button in Excel.
  • In the VBA Editor, choose the worksheet where you want to track changes.
  • In the worksheet’s code module, type the Worksheet Change Event code. Keep in mind that you cannot change the event in a regular module. You must enter it in the worksheet’s code module.
  • Now, the worksheet’s code module should contain the following VBA code:
Private Sub Worksheet_Change(ByVal Target As Range)
    ' Incorrect code: missing "Then" keyword
    If Target.Column = 1 ' Incorrect syntax
        Target.Offset(0, 1).Value = "Value Changed"
    End If
End Sub

Error in the VBA Code

  • Here you need to correct any kind of error showing in the red line in the previous image. By adding “Then” you can solve the issue and fix the error.

Correcting the VBA Code with worksheet change event


Frequently Asked Questions (FAQs)

  • How can I take various actions depending on the changed cells in the monitored range?

To perform particular actions based on the modified cells, you can add different code blocks or call different procedures inside the If statement block. Make each of the statement’s codes specific to the desired outcomes.

  • Is it possible to use a single worksheet change event to track changes across multiple worksheets?

No, a specific worksheet is the only one that the Worksheet Change event refers to. You must add the Worksheet Change event code to each relevant worksheet’s code window to track changes across multiple worksheets.

  • Why doesn’t my Worksheet Change event work properly?

There are a number of potential causes for the Worksheet Change event to fail, including an incorrect event name, disabled macros, incorrect macro security settings, and errors in the event code. Make sure you have named the event code correctly, turned on those macros, and the code is free from errors.


Download Practice Workbook

You can download the practice workbook from the following download button.


Conclusion

In this article, we looked at how to use the VBA Worksheet Change event to track changes in a number of cells in an Excel worksheet. Despite how straightforward it is, some common mistakes can prevent the event from running smoothly. We aimed to assist even those with limited VBA knowledge in successfully implementing and troubleshooting the Worksheet Change event by addressing these concerns and offering useful solutions.

Get FREE Advanced Excel Exercises with Solutions!
Al Ikram Amit
Al Ikram Amit

Al Ikram Amit, holding a BSc in Naval Architecture & Engineering from Bangladesh University of Engineering and Technology, serves as a key Excel & VBA Content Developer at ExcelDemy. Driven by a profound passion for research and innovation, he actively immerses himself in Excel. In his role, Amit not only skillfully addresses complex challenges but also exhibits enthusiasm and expertise in gracefully navigating tough situations, emphasizing his steadfast commitment to consistently delivering exceptional, high-quality content that adds significant... Read Full Bio

2 Comments
  1. I like your layout, and explanations.
    I have been having trouble with two critical details, and after a while I was able to figure out my problems. 1. I’ve gone 30 years programming in vba, and never use the Project Explorer, and when you said to put the code in a worksheet’s module, I was putting it in a regular module, and for days nothing was working! YOU MAY WANT TO MENTION “OPEN THE PROJECT EXPLORER, AND CLICK ON THE WORKSHEET” SO IT GOES THERE!
    2. I’ve looked through dozens of web pages showing usage of the “Worksheet_Change” event, and nobody does a decent job of saying what “Target” is. I think it might be better named as “Trigger”. You and the other guys don’t explain what is in the “Target” data item, when the process comes into this routine. I’m gleaning from multiple examples that maybe it’s a cell that changed, triggering the event called Worksheet_Change. I’ve even seen some paragraphs on this event talking about “Target” and “Destination”, when nothing is moving, as far as I can see. What confusion!
    3. I see a line in your code that concatenates multiple selected values into the cell, with commas between them, and I CAN NEVER GET THAT PART TO WORK!!! Ugh!

    • Reply Lutfor Rahman Shimanto
      Lutfor Rahman Shimanto Apr 3, 2024 at 12:49 PM

      Hello Irish Jim

      Thanks for your compliments. Your appreciation and suggestions mean a lot to us. You are right; it would be great to mention right-clicking and clicking on View Code when inserting code in the sheet module. Also, it is important to mention the Target parameter when discussing events in Excel.

      Thank you once again for visiting our blog and providing beneficial suggestions. Based on your suggestion, we have updated the sections. Hopefully, other visitors will have a better reading experience.

      Regards
      ExcelDemy

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo