• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

Message box should appear only once, when spreadsheet is opened|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / Message box should appear only once, when spreadsheet is opened|VBA & Macros|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosMessage box should appear only once…
sp_PrintTopic sp_TopicIcon
Message box should appear only once, when spreadsheet is opened
Avatar
Gayatri Kulkarni

Active Member
Members

Dashboards

Power Pivot

Power BI
Level 0
Forum Posts: 5
Member Since:
August 2, 2016
sp_UserOfflineSmall Offline
1
June 29, 2019 - 1:56 am
sp_Permalink sp_Print

Hi, I have a worksheet that triggers a message whenever the value in cell K4 or in L4 is greater than the value in cell O2. However, the message box pops up each time I change anything in the spreadsheet, which is frustrating. How can I get the message to pop -up only once, when the first change in the spreadsheet occurs on that day? 

I am a newbie at VBA - any help is greatly welcome!

Here is my code:

Private Sub Worksheet_Change(ByVal Target As Range)
If Range("k4").Value > Range("o2").Value Then
MsgBox "Sudden spike in usage this month!!", vbExclamation, Title:="Warning:"

ElseIf Range("l4").Value > Range("o2").Value Then
MsgBox "Sudden spike in usage next month!!", vbExclamation, Title:="Warning:"

End If
End Sub

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1807
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
2
July 1, 2019 - 1:38 pm
sp_Permalink sp_Print

Hi Gayatri,

Use Intersect to set the range to be monitored, this way the code will be triggered only when you change K4 or L4:

Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target,Range("K4,L4")) is nothing then

If Range("k4").Value > Range("o2").Value Then
MsgBox "Sudden spike in usage this month!!", vbExclamation, Title:="Warning:"

ElseIf Range("l4").Value > Range("o2").Value Then
MsgBox "Sudden spike in usage next month!!", vbExclamation, Title:="Warning:"

End If

end if
End Sub

Note that if K4 or L4 have formulas, and you don't actually make changes to those cells manually, the Change event will not be fired, a formula change is not a monitored event, in this case you need another logic.

Avatar
Gayatri Kulkarni

Active Member
Members

Dashboards

Power Pivot

Power BI
Level 0
Forum Posts: 5
Member Since:
August 2, 2016
sp_UserOfflineSmall Offline
3
July 3, 2019 - 2:04 am
sp_Permalink sp_Print

Thanks so much for replying, Catalin - truly grateful! However, the code did not work, because K4 and L4 have formulas, and the values are formula-driven, and are not manually changed. How can the code be modified to reflect this logic?

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1807
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
4
July 3, 2019 - 3:47 pm
sp_Permalink sp_Print

Hi Gayatri,

In this case, instead of monitoring K4 and L4, put in code the ranges that you are actually editing manually that can cause changes in K4 and L4 formulas.

If you edit F2:F200 or example, and these changes might change K4 and L4 results, add this range in code:

If Not Intersect(Target,Range("F2:F200")) is nothing then

Avatar
Gayatri Kulkarni

Active Member
Members

Dashboards

Power Pivot

Power BI
Level 0
Forum Posts: 5
Member Since:
August 2, 2016
sp_UserOfflineSmall Offline
5
July 4, 2019 - 5:55 am
sp_Permalink sp_Print

None of the values are changed manually - they are formula driven. 

I tried changing the event to calculate, ie as below:

Private Sub Worksheet_Calculate()

but now the macro does not work: I get the error message:

Procedure declaration does not match description of event or procedure having same name

Here is my code:

Private Sub Worksheet_Calculate()
If Not Intersect(Target, Range("M:M")) Is Nothing Then
If Range("k4").Value > Range("o2").Value Then
MsgBox "Sudden spike in usage this month!!", vbExclamation, Title:="Warning:"
ElseIf Range("l4").Value > Range("o2").Value Then
MsgBox "Sudden spike in usage next month!!", vbExclamation, Title:="Warning:"
End If
End If
End Sub

k4 and L4 are formulas that can change based on the values in column M

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1807
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
6
July 4, 2019 - 5:59 pm
sp_Permalink sp_Print sp_EditHistory

Hi Gayatri,

The worksheet_Change event provides an argument named Target:

Private Sub Worksheet_Change(ByVal Target As Range)

The calculate event does not provide any argument, so using the Target as an argument in Calculate is wrong:

Private Sub Worksheet_Calculate()
If Not Intersect(Target, Range("M:M")) Is Nothing Then

You have to rely only on worksheet ranges.

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: s Ramchandran, Shanna Henseler, Stacy Culpepper
Guest(s) 11
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 870
Purfleet: 412
Frans Visser: 346
David_Ng: 306
lea cohen: 219
A.Maurizio: 202
Jessica Stewart: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
John Chisholm
vexokeb sdfg
John Jack
Malcolm Toy
Ray-Yu Yang
George Shihadeh
Naomi Rumble
Uwe von Gostomski
Jonathan Jones
drsven
Forum Stats:
Groups: 3
Forums: 24
Topics: 6212
Posts: 27237

 

Member Stats:
Guest Posters: 49
Members: 31889
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.