Display Message Box only once when value exceeded

Ironman

Well-known Member
Joined
Jan 31, 2004
Messages
1,069
Office Version
  1. 365
Platform
  1. Windows
Hi

I've created a simple message box when Col B in the bottom row of the expanding range below exceeds Col B in the row above it. The two cells are named ranges (I've placed them in cells so it's clear what they do - they're not actually in those cells) and it looks like this
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Range ("thisyr") > Range ("lastyr") then msgbox "Last year's total exceeded!"
End Sub

Book1.xlsm
AB
1YEARTOTAL
219845
3198612
4198710
519881
619896
719902
819913
919931
1019948
1119965
1219981
1320007
1420011
1520026
16200312
17200424
1820059
1920062
20200722
2120083
22201927
23202011
24202132
25
26TOTAL210
27
28Last Yr11
29This Yr32
Sheet1
Cell Formulas
RangeFormula
B26B26=SUM(B2:B25)
B28B28=LastYr
B29B29=ThisYr
Named Ranges
NameRefers ToCells
LastYr=OFFSET(Sheet1!$B$24,-1,0)B26
ThisYr=OFFSET(Sheet1!$B$25,-1,0)B28, B26


What I'm after is a bit more code so the message box only pops up once.

Many thanks!
 
Thanks a lot for your suggestion Johnny, that's the only way I knew how to do it as well, by creating a '1' in a cell after the event and then clearing it on exit (although the next time the workbook is opened and the sheet changes it will then reappear and trigger the message box again).
 
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Why are you contemplating clearing the flag on exit? Leave it there. Bury the flag somewhere out of the way and leave it there. You could put it on a veryhidden sheet if you want to.
 
Upvote 0
Because if I do that, it will never activate again because the code will only run if the cell is blank.
 
Upvote 0
One of us is confused.

What I am suggesting as an approach is replacing:

VBA Code:
    If Range("thisyr") > Range("lastyr") Then MsgBox "Last year's total exceeded!"

with something like:

VBA Code:
    If HelperCell <= CurrentYear And Range("thisyr") > Range("lastyr") Then
        MsgBox "Last year's total exceeded!"
'
        HelperCell = CurrentYear + 1
    End If

If you don't think that will work, please explain how it would fail to work.

HelperCell would be an address somewhere and CurrentYear would be a variable that is calculated.
 
Upvote 0
OK, what I've been using is this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

'**UNCOMMENT JAN 1 ANNUALLY**
'04.11.2021 Msgbox activated if this year's total exceeds last year's
'If Range("IronManLogThisYr").Value > Range("IronManLogLastYrTot").Value Then
'If Sheets("Training Log").Range("H3") = "" Then
'MsgBox "Congratulations!" & vbNewLine & vbNewLine & "You've now run more Iron Man runs than last year!", vbInformation, "Last year's total beaten"
'Sheets("Training Log").Range("H3") = "1"
'End If
'End If
'End Sub
There are other subs in the Change Event so to avoid it activating every time another cell changes, it has to be commented out once it's activated until Jan 1 in the next year.
 
Upvote 0
This is the code I tested and the msgbox only popped up one time:

VBA Code:
    Dim HelperCell      As Range
    Dim wsHelperSheet   As Worksheet
'
    Set wsHelperSheet = Sheets("Sheet1")                    ' <--- Set this to desired Helper sheet
    Set HelperCell = wsHelperSheet.Range("A1")              ' <--- Set this to desired cell address to store the flag
'
    CurrentYear = Year(Date)                                ' Get current year
'
    If HelperCell <= CurrentYear Then                       ' If flag < or = to CurrentYear then ...
        HelperCell = CurrentYear + 1                        '   Increment flag so msgbox will not be executed until the next year
        MsgBox "Helper cell Updated"                        '   Display message to user
    End If

The msgbox will not have a chance to popup until next year when the code satisfies the 'HelperCell <= CurrentYear' portion of the code.
 
Upvote 0
Hey that's brilliant Johnny, thanks ever so much.

Could I just ask you to tell me where your code fits into the code I posted (#16) please? I will of course delete the references I made to H3 and use your code to reference that instead.

Thanks again!
 
Upvote 0
You mean this? :

VBA Code:
    Dim HelperCell      As Range
    Dim wsHelperSheet   As Worksheet
'
    Set wsHelperSheet = Sheets("Training Log")                                                      ' <--- Set this to desired Helper sheet
    Set HelperCell = wsHelperSheet.Range("H3")                                                      ' <--- Set this to desired cell address to store the flag
'
    CurrentYear = Year(Date)                                                                                        ' Get current year
'
    If HelperCell <= CurrentYear And Range("IronManLogThisYr").Value > Range("IronManLogLastYrTot").Value Then      ' If flag < or = to CurrentYear and
'                                                                                                                   '   ThisYr total> LastYr total then ...
        HelperCell = CurrentYear + 1                                                    '   Increment flag so msgbox will not be executed until the next year
        MsgBox "Congratulations!" & vbNewLine & vbNewLine & "You've now run more Iron Man runs than last year!", _
            vbInformation, "Last year's total beaten"                                                               '   Display message to user
    End If
 
Upvote 0
Solution
Exactly that - brilliant, that works perfectly, thank you so much for that Johnny!
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,429
Members
448,961
Latest member
nzskater

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top