VBA code to show Message Box popup if the formula in the target cell exceeds a certain value

234,947

Solution 1

You could add the following VBA code to your sheet:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Range("A1") > 0.5 Then
        MsgBox "Discount too high"
    End If
End Sub

Every time a cell is changed on the sheet, it will check the value of cell A1.

Notes:

  • if A1 also depends on data located in other spreadsheets, the macro will not be called if you change that data.
  • the macro will be called will be called every time something changes on your sheet. If it has lots of formula (as in 1000s) it could be slow.

Widor uses a different approach (Worksheet_Calculate instead of Worksheet_Change):

  • Pros: his method will work if A1's value is linked to cells located in other sheets.
  • Cons: if you have many links on your sheet that reference other sheets, his method will run a bit slower.

Conclusion: use Worksheet_Change if A1 only depends on data located on the same sheet, use Worksheet_Calculate if not.

Solution 2

Essentially you want to add code to the Calculate event of the relevant Worksheet.

In the Project window of the VBA editor, double-click the sheet you want to add code to and from the drop-downs at the top of the editor window, choose 'Worksheet' and 'Calculate' on the left and right respectively.

Alternatively, copy the code below into the editor of the sheet you want to use:

Private Sub Worksheet_Calculate()

If Sheets("MySheet").Range("A1").Value > 0.5 Then
    MsgBox "Over 50%!", vbOKOnly
End If

End Sub

This way, every time the worksheet recalculates it will check to see if the value is > 0.5 or 50%.

Solution 3

I don't think a message box is the best way to go with this as you would need the VB code running in a loop to check the cell contents, or unless you plan to run the macro manually. In this case I think it would be better to add conditional formatting to the cell to change the background to red (for example) if the value exceeds the upper limit.

Share:
234,947
Hatt
Author by

Hatt

Data science and analysis - just a general fan of deeper analysis and visualization within the hospitality industry.

Updated on February 17, 2020

Comments

  • Hatt
    Hatt over 4 years

    I am trying to write a simple macro to display a pop-up (vbOKOnly) if the value in a cell exceeds a certain value.

    I basically have a worksheet with products and discounts. I have a formula in one cell, say A1, that shows the discount as a percent (50% or .5) effective discount of all the entries.

    What I'm looking for is code to display a message box if the value of cell A1 exceeds say 50%, because the input of another cell pushed the discount over 50%.

    Thanks!

  • Matt Donnan
    Matt Donnan over 12 years
    @assylias I was thinking along the lines of a localised script to focus in on said cell, if using the method you have provided then true no loop is required, but as you say the more data on the spreadsheet the longer it will take to run.
  • Siddharth Rout
    Siddharth Rout over 12 years
    Yes you are right. I deleted my post. Seems like I misread the question :) +1 for getting it right :)
  • assylias
    assylias over 12 years
    @SiddharthRout to be honest I first replied with data validation, until I read the 3rd line of the question ;)
  • Hatt
    Hatt over 12 years
    Hey, thanks for all the comments and answers everyone. I've learned VBA (and available subs and functions) almost exclusively through use/need. I do plan on keeping all data/references on the same sheet. Thanks again for all the help!
  • ejbytes
    ejbytes almost 8 years
    @assylias Found this while searching the subject. Actually Matt is correct. While a loop isn't used in the syntax in VBa, a Worksheet_Change, in effect is a constant "looping" call. In other words, it's an internal call to "Anything happen yet?" over and over, every millisecond.