Why are my 3 message boxes repeating twice?

Ironman

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

My 'Daily Tracking' worksheet has the following code:


Private Sub Worksheet_Change(ByVal Target As Range)

If Intersect(Target, Range("EntRng")) Is Nothing Or Target.Count > 1 Then Exit Sub

' PROCEDURE 1
With Worksheets("Training Log")
MsgBox "Lifetime Mileage: " & Format$(.Range("E5").Value, "#,##0") & " " & vbNewLine & vbNewLine & _
"Year to Date Mileage: " & Format$(.Range("C5").Value, "#,##0") & " ", vbInformation, "Mileage Totals "
End With

' PROCEDURE 2
With Worksheets("Daily Tracking")
uValue = Sheets("Training Log").Range("MlsYTDLessLastYr")
Select Case uValue
Case Is < 0
utext = "less"
Case Is = 0
utext = "equal"
Case Is > 0
utext = "more"
End Select
MsgBox "You have now run " & uValue & " miles " & _
utext & " than this time last year ", vbInformation, "Mileage Compared To This Time Last Year"
End With

' PROCEDURE 3
If Range("CurYTD").Value > Range("CurGoal").Value Then
MsgBox ("Congratulations!" & vbNewLine & "You have now run more miles this year than" & vbNewLine & vbNewLine & _
"- The whole of " & Range("PreYear").Value & vbNewLine & _
"- " & Range("counter").Value & " of the " & Year(Now) - 1981 & " years you've been running" & vbNewLine & _
vbNewLine & "New rank for " & Year(Now) & " is " & Range("CurYTD").Offset(1, 0).Value & " out of " & Year(Now) - 1981), _
vbInformation, "Another Year End Mileage Total Exceeded "

Range("counter").Value = Range("Counter").Value + 1
Else
MsgBox (CLng(Range("CurGoal").Value - Range("CurYTD").Value) & _
" miles to go until you reach rank " & (Range("CurYTD").Offset(1, 0).Value) - 1 & " " & vbNewLine & vbNewLine & _
" (Year end mileage for " & Range("PreYear").Value & ")"), _
vbInformation, "Year To Date Mileage"
End If
End Sub


The 3 procedures named above run consecutively no problem, but after Procedure 3 has run, all 3 run one more time in sequence.

What do I need to add or amend so that the 3 procedures only run once?

Thanks
 
Last edited:

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Do you get the same problem with the code below?
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Intersect(Target, Range("EntRng")) Is Nothing Or Target.Count > 1 Then Exit Sub
    Application.EnableEvents = False
    ' PROCEDURE 1
    With Worksheets("Training Log")
        MsgBox "Lifetime Mileage: " & Format$(.Range("E5").Value, "#,##0") & " " & vbNewLine & vbNewLine & _
               "Year to Date Mileage: " & Format$(.Range("C5").Value, "#,##0") & " ", vbInformation, "Mileage Totals "
    End With

    ' PROCEDURE 2
    With Worksheets("Daily Tracking")
        uValue = Sheets("Training Log").Range("MlsYTDLessLastYr")
        Select Case uValue
            Case Is < 0
                utext = "less"
            Case Is = 0
                utext = "equal"
            Case Is > 0
                utext = "more"
        End Select
        MsgBox "You have now run " & uValue & " miles " & _
               utext & " than this time last year ", vbInformation, "Mileage Compared To This Time Last Year"
    End With

    If Range("CurYTD").Value > Range("CurGoal").Value Then
        MsgBox ("Congratulations!" & vbNewLine & "You have now run more miles this year than" & vbNewLine & vbNewLine & _
                "- The whole of " & Range("PreYear").Value & vbNewLine & _
                "- " & Range("counter").Value & " of the " & Year(Now) - 1981 & " years you've been running" & vbNewLine & _
                vbNewLine & "New rank for " & Year(Now) & " is " & Range("CurYTD").Offset(1, 0).Value & " out of " & Year(Now) - 1981), _
                vbInformation, "Another Year End Mileage Total Exceeded "

        ' PROCEDURE 3
        Range("counter").Value = Range("Counter").Value + 1
    Else
        MsgBox (CLng(Range("CurGoal").Value - Range("CurYTD").Value) & _
                " miles to go until you reach rank " & (Range("CurYTD").Offset(1, 0).Value) - 1 & " " & vbNewLine & vbNewLine & _
                " (Year end mileage for " & Range("PreYear").Value & ")"), _
                vbInformation, "Year To Date Mileage"
    End If
    Application.EnableEvents = True
End Sub
 
Upvote 0
Hi Mark, many thanks.

Yes, inserting

Code:
Application.EnableEvents = False

at the start and

Code:
Application.EnableEvents = True

at the end as you suggested made no difference.

(I've just noticed your footer re Code Tags :biggrin:)
 
Last edited:
Upvote 0
And just to make sure, if you change
Code:
    If Intersect(Target, Range("EntRng")) Is Nothing Or Target.Count > 1 Then Exit Sub
    Application.EnableEvents = False
to
Code:
    Application.EnableEvents = False
    If Intersect(Target, Range("EntRng")) Is Nothing Or Target.Count > 1 Then Exit Sub
still the same?
 
Upvote 0
Not much that I can suggest without having the workbook to test on other than to suggest to put a break point at the start of the code (or the word Stop on a separate line) and then step through with F8 to try and see when the second run is called.

http://www.cpearson.com/excel/debuggingvba.aspx
 
Last edited:
Upvote 0
Here's the workbook link, if it's of any assistance?

Code:
https://www.dropbox.com/s/d0a7aj233gn35wp/Exercise%20Log.xlsm?dl=0

Many thanks
 
Last edited:
Upvote 0
This line
Code:
Range("counter").Value = Range("Counter").Value + 1
is triggering a second change event.
you need to disable events before and re-enable them after

Code:
Application.EnableEvents = False
Range("counter").Value = Range("Counter").Value + 1
Application.EnableEvents = True
 
Upvote 0
Thanks Mike - I just tried that and it made no difference.
 
Upvote 0

Forum statistics

Threads
1,213,496
Messages
6,113,995
Members
448,539
Latest member
alex78

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