VBA - Message Box pop up twice

Beatrice

Board Regular
Joined
Sep 17, 2019
Messages
85
Office Version
  1. 2019
Platform
  1. Windows
Hi experts, I need some help as the simple code for a pop up message always appear twice.
I want to process 3 checks and each of them might pop up once only
here is my code, could you please point out what did I do wrong?
Thanks in advance for your help.

VBA Code:
Private Sub Worksheet_Calculate()
'popup message for part not found
Application.EnableEvents = True
Application.EnableEvents = False

If Range("h117").Value = 1 Then
MsgBox "Item 1 Not Found", vbExclamation, "Error"
End If

If Range("h145").Value = 1 Then
MsgBox "Item 2 Not Found", vbExclamation, "Error"
End If

If Range("h145").Value = 1 Then
MsgBox "Item 3 Not Found", vbExclamation, "Error"
End If

Application.EnableEvents = True
End Sub
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
i'll guess the popups are for different values not found? if you only need to return the first error it gets to (if any) then you'll need to nest your ifs -
VBA Code:
Private Sub Worksheet_Calculate()
    'popup message for part not found
    Application.EnableEvents = True
    Application.EnableEvents = False
    
    If Range("h117").Value = 1 Then
        MsgBox "Item 1 Not Found", vbExclamation, "Error"
    ElseIf Range("h145").Value = 1 Then
        MsgBox "Item 2 Not Found", vbExclamation, "Error"
    ElseIf Range("h145").Value = 1 Then
        MsgBox "Item 3 Not Found", vbExclamation, "Error"
    End If
    
    Application.EnableEvents = True
End Sub

with how it's set up for you now, if two of you conditions succeed you will get 2 popups.
 
Upvote 0
i'll guess the popups are for different values not found? if you only need to return the first error it gets to (if any) then you'll need to nest your ifs -
VBA Code:
Private Sub Worksheet_Calculate()
    'popup message for part not found
    Application.EnableEvents = True
    Application.EnableEvents = False
   
    If Range("h117").Value = 1 Then
        MsgBox "Item 1 Not Found", vbExclamation, "Error"
    ElseIf Range("h131").Value = 1 Then
        MsgBox "Item 2 Not Found", vbExclamation, "Error"
    ElseIf Range("h145").Value = 1 Then
        MsgBox "Item 3 Not Found", vbExclamation, "Error"
    End If
   
    Application.EnableEvents = True
End Sub

with how it's set up for you now, if two of you conditions succeed you will get 2 popups.
thanks for helping, but it still pop up twice with these code.
H117/ H131 / H145 represent 3 different check, they need to be process separately
thank you
 
Upvote 0
Well In that case the code is fine. I guess excel is recalculating after ok is hit the first time. As this sub triggers on the recalculate event. Try putting -
VBA Code:
Application.calculation = xlmanual

At the beginning and -
VBA Code:
Application.calculation = xlautomatic

At the end.
It's weird that it doesn't get stuck in a loop doing this. But hey it's excel.
 
Upvote 0

Forum statistics

Threads
1,215,029
Messages
6,122,760
Members
449,095
Latest member
m_smith_solihull

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