Msgbox Close with vba

  • Thread starter Thread starter art
  • Start date Start date
A

art

Hello:

I have a code that triggers a msgbox to open and ask the user yes or no.
However, there is certain times that the msgbox triggers but I don't need it.
How can I put in the code to close the msgbox. Please understand the the code
that triggers the msgbox is not in the same sub as the code that I want to
cancel the msgbox.

Thanks.

Art
 
Can you not adapt the code that triggers the msgbox with the
'Application.Displayalerts=FALSE' statement to not show when it is not
required.

Perhaps an IF or Case section of code ?

Corey....
 
Why do you have subroutines triggering the MessageBox when it is not needed?
What is it about your set up that would dictate the MessageBox should be
dismissed? Why can't you incorporate the logic that dictates no MessageBox
into the subroutines that trigger it?
 
Put an If...Then statement in to only fire the message box when it is needed.

If this condition is met Then
'activate the message box
Else
'ignore this and go on with the code
End If
 
You all guys are great, but nobody answered my question. I'll explain why I
need it. The code that triggers the msgbox is "beforePrint" event. Now the
msgbox is also triggered when Print Preview is pressed. So I have a activex
command button that opens the print preview. So I want the msgbox not to open
when the printpreview is pressed. Any ideas?

Code BeforePrint:

If Worksheets("Order Form").Range("C3") = "You have not selected a customer
yet" Then
msg = "You have not yet selected a customer."
msg = msg & vbNewLine
msg = msg & "Are you sure you want to print?"
Ans = MsgBox(msg, vbYesNo + vbQuestion + vbDefaultButton2, "Print
Order")
If Ans = vbNo Then
Cancel = True
End If
End If
End Sub

Please help.
 
Hi

With that information given, add this line before the line that opens
Print Preview:

Application.EnableEvents=false

Just remember to set it = true before end sub.

Hopes it helps

Regards,
Per
 
Another possibility is to create a global "flag" variable and use it to
control whether the MessageBox is displayed or not. A way to implement
something like this would be to insert a Module (Insert/Module from the VB
editor), Dim a Boolean variable in it and then set the variable in those
subroutines/event procedures that you didn't want to trigger the MessageBox
and test for that set variable in the subroutine/event procedures that
contained the code to display the MessageBox. For example, in the Module
that you Insert, put this...

Public HideMsgBox As Boolean

In a subroutine that you didn't want to trigger the MessageBox, put this at
the top of the code...

HideMsgBox = True

And in the subroutine/event procedure that displays the MessageBox, encase
the MsgBox command inside an If..Then block like this...

If Not HideMsgBox Then
'
' Put your MessageBox(es) here
'
End If
 
I forgot one thing... inside the If...Then block, as the last statement, you
have to reset the global "flag" variable. So, the code for the
MessageBox(es) should have been this...

If Not HideMsgBox Then
'
' Put your MessageBox(es) here
'
HideMsgBox = False
End If

If you don't do this, the "flag" will remain set and the MessageBox(es) will
not be displayed again even when you wanted them to be.
 
When the user uses File, Print the before print event runs before the dialog
appears. So there is no way to know at that point whether the user will
pick Print or Print Preview.

If you have your own "Print" and "Print Preview" buttons running macros
you'd add your msgbox to the former only. But you'd still have to disable
all built-in ways to print to insure the user used your buttons.

--
Jim
|I tried that already, it still pops up the msgbox.
|
| "Per Jessen" wrote:
|
| > Hi
| >
| > With that information given, add this line before the line that opens
| > Print Preview:
| >
| > Application.EnableEvents=false
| >
| > Just remember to set it = true before end sub.
| >
| > Hopes it helps
| >
| > Regards,
| > Per
| >
| > > You all guys are great, but nobody answered my question. I'll explain
why I
| > > need it. The code that triggers the msgbox is "beforePrint" event. Now
the
| > > msgbox is also triggered when Print Preview is pressed. So I have a
activex
| > > command button that opens the print preview. So I want the msgbox not
to open
| > > when the printpreview is pressed. Any ideas?
| > >
| > > Code BeforePrint:
| > >
| > > If Worksheets("Order Form").Range("C3") = "You have not selected a
customer
| > > yet" Then
| > > msg = "You have not yet selected a customer."
| > > msg = msg & vbNewLine
| > > msg = msg & "Are you sure you want to print?"
| > > Ans = MsgBox(msg, vbYesNo + vbQuestion + vbDefaultButton2,
"Print
| > > Order")
| > > If Ans = vbNo Then
| > > Cancel = True
| > > End If
| > > End If
| > > End Sub
| > >
| > > Please help.
| > >
| > >
| > >
| > > "JLGWhiz" wrote:
| > > > Put an If...Then statement in to only fire the message box when it
is needed.
| > >
| > > > If this condition is met Then
| > > > 'activate the message box
| > > > Else
| > > > 'ignore this and go on with the code
| > > > End If
| > >
| > > > "art" wrote:
| > >
| > > > > Hello:
| > >
| > > > > I have a code that triggers a msgbox to open and ask the user yes
or no.
| > > > > However, there is certain times that the msgbox triggers but I
don't need it.
| > > > > How can I put in the code to close the msgbox. Please understand
the the code
| > > > > that triggers the msgbox is not in the same sub as the code that I
want to
| > > > > cancel the msgbox.
| > >
| > > > > Thanks.
| > >
| > > > > Art- Skjul tekst i anførselstegn -
| > >
| > > - Vis tekst i anførselstegn -
| >
| >
 
Hi Avi,

I understand that you have created a msg box from VBA coding and now you
want to remove that msg box..

To remove the msg box, please open the document in which you have created
that msg box now, press ALT + F11 to enter in VBA module, now on the left
hand panel, you would have three options?

1. Sheet1
2. Sheet2
3. Sheet3

Select the sheet1 and right click and then select view code, now you will
see the vba coding, then where you will see the coding of that msg.. Please
delete that one and then click on save..

Then check, has that msg box disappear or not..?

Regards,
Arun Sharma
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top