Displaying Messages When Automatic Data Changes

Written by Allen Wyatt (last updated February 27, 2021)
This tip applies to Excel 97, 2000, 2002, and 2003


Joydip has an Excel worksheet that is constantly and automatically updated with live commodity market data. He wants to display a message box containing a particular message whenever the data in a specified cell/range changes to meet some predefined criteria. Data validation won't work, because the validation feature isn't triggered when data changes automatically.

The best way to check the data and display the desired message box is to use a macro that is triggered by the Worksheet_Change event. This event is triggered any time the contents of a cell are changed. It is not, however, triggered by a change in what is displayed in a cell. For instance, if a new piece of commodity data is placed into a cell, then the event is triggered. However, if a formula is recalculated and a new result of that formula displayed, the event is not triggered. Why? Because the formula itself didn't change; it was only the result of the formula (what is displayed) that was changed.

Once the Worksheet_Change event is triggered, the macro can do anything you want it to do, including displaying your message. For this example, let's assume that the range to check is A1:C5 (this is where the commodity data is being inserted) and that the criteria you want to trigger the message is that the average of the range is 5. If the contents of any cell in the range is changed and the average of the values in the range is 5, then a message is displayed.

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    Dim rng As Range
    Set rng = Range("A1:C5")
    If Not Intersect(Target, rng) Is Nothing Then
        If Application.WorksheetFunction. _
          Average(rng) = 5 Then
            MsgBox "The average of " & _
              rng.Address & " = 5"
        End If
    End If
    Set rng = Nothing
End Sub

It is important that this macro be placed in the sheet object for the worksheet you want to monitor. When you display the VBA Editor, right-click on the desired worksheet in the Project Explorer area, then choose View Code from the resulting Context menu. This code window is where you place the macro.

The macro, again, is triggered anytime there is a change anywhere on the worksheet. The macro then uses the Intersect function to determine if the change occurred within the desired A1:C5 range. If it did, then the average of the range is checked, and the message displayed if the result is 5.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2906) applies to Microsoft Excel 97, 2000, 2002, and 2003.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Adding Page Numbers in Headers or Footers

While Word has a default format for page numbers, you can design and specify how you want them to appear in your ...

Discover More

Repeating Rows for a Table Footer

Word allows you to specify rows that should be repeated at the top of a table when that table extends beyond the bottom ...

Discover More

Moving Captions with Pictures

Put a caption with a picture and you'd probably like the two elements to behave like they belong together. If you are ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (menu)

Out of Memory Errors when Accessing the VBA Editor

It can be frustrating when you get error messages doing something that you previously did with no errors. If you get an ...

Discover More

Unhiding or Listing All Objects

An Excel workbook can contain quite a few different objects. Sometimes those objects can be hidden so that they are not ...

Discover More

Selecting a Specific Cell in a Macro

Need to use a macro to select a specific cell in a different workbook? It's not as straightforward of a proposition as ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 1 + 1?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.