+ Reply to Thread
Results 1 to 5 of 5

Creating a message box when a cell value meets a criteria runs multiple times

Hybrid View

  1. #1
    Registered User
    Join Date
    08-23-2006
    Location
    Darlington, England
    MS-Off Ver
    Microsoft 365
    Posts
    33

    Creating a message box when a cell value meets a criteria runs multiple times

    Hi

    My workbook has a bunch of power queries pulling in data into some tables.

    I also have a hidden sheet that counts a particular condition in a cell.

    Within the hidden sheet's code I have the following...
    Sub Worksheet_Calculate()
    
        If Range("C2") > 0 Then
            MsgBox "Test Message"
        End If
    
    End Sub
    The workbook refreshes every 10 minutes and when the cell is above 0 the alert pops up.
    Unfortunutely it pops up 4 times so the sheet must be calculating multiple times on refresh causing the code to run multiple times.

    I'm looking for a solution to make this code run only once.

    Is there an altenative I can use to make the message box pop up once?

  2. #2
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,082

    Re: Creating a message box when a cell value meets a criteria runs multiple times

    Does this work for you?

    Sub Worksheet_Calculate()
        Static blnRun As Boolean
        If Range("C2") > 0 Then
            If blnRun = False Then
                blnRun = True
                MsgBox "Test Message"
            Else
                blnRun = False
            End If
        End If
    End Sub

  3. #3
    Registered User
    Join Date
    08-23-2006
    Location
    Darlington, England
    MS-Off Ver
    Microsoft 365
    Posts
    33

    Re: Creating a message box when a cell value meets a criteria runs multiple times

    Quote Originally Posted by ByteMarks View Post
    Does this work for you?

    Sub Worksheet_Calculate()
        Static blnRun As Boolean
        If Range("C2") > 0 Then
            If blnRun = False Then
                blnRun = True
                MsgBox "Test Message"
            Else
                blnRun = False
            End If
        End If
    End Sub
    Hi, thanks for the suggestion.

    You code seems to have reduced the alert to popping up 2 sometimes 3 times but didn't resolve the issue.

  4. #4
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,082

    Re: Creating a message box when a cell value meets a criteria runs multiple times

    If it was always 4 times originally, then this might work.


    Sub Worksheet_Calculate()
        Static CalcTimes As Long
        If Range("C2") > 0 Then
            If CalcTimes = 0 Then
                MsgBox "Test Message"
            End If
        End If
        CalcTimes = CalcTimes + 1
        If CalcTimes >= 4 Then CalcTimes = 0
    End Sub

  5. #5
    Registered User
    Join Date
    08-23-2006
    Location
    Darlington, England
    MS-Off Ver
    Microsoft 365
    Posts
    33

    Re: Creating a message box when a cell value meets a criteria runs multiple times

    Quote Originally Posted by ByteMarks View Post
    If it was always 4 times originally, then this might work.


    Sub Worksheet_Calculate()
        Static CalcTimes As Long
        If Range("C2") > 0 Then
            If CalcTimes = 0 Then
                MsgBox "Test Message"
            End If
        End If
        CalcTimes = CalcTimes + 1
        If CalcTimes >= 4 Then CalcTimes = 0
    End Sub
    Thanks ByteMarks that worked for me.
    I searched what the Static variable does and it allows a variable to retain it's value after the code has finished which allows the count to take place everytime the worksheet is calculated.
    Totally makes sense.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Copying multiple rows if a certain cell in those row meets a criteria
    By jpringle1 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 01-08-2019, 10:11 AM
  2. need help pulling a cell that meets multiple Criteria
    By jaredhulon323 in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 11-18-2015, 06:25 PM
  3. [SOLVED] Counting quantity in cell if it meets multiple criteria
    By dgreene2010 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 10-23-2013, 05:42 PM
  4. Macro for replacing cell if it meets multiple criteria
    By Batrac in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-04-2012, 11:02 AM
  5. Replies: 7
    Last Post: 07-19-2012, 10:22 AM
  6. highlight selection cell that meets criteria and multiple cells if not
    By rona_ele in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-30-2011, 09:17 AM
  7. Average of dates and times if it meets a specific criteria
    By Shadoweski in forum Excel General
    Replies: 1
    Last Post: 09-28-2010, 10:26 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1