MsgBox Issues

richExcelforAJ

New Member
Joined
Jul 12, 2022
Messages
4
Office Version
  1. 365
Platform
  1. Windows
The code below presents a yes/no MsgBox.

First Item:
When the message box appears, I click the 'No' button and the the MsgBox quickly disappears and redisplays. I click 'No' a second time and the click is accepted producing a VbMsgBoxResult of VbNo (7). Why are 2 clicks required?
Note: This does not happen with Yes, which works perfectly. Additionally, I only have the code for the click event on this button. As I tried to get this to work, I cycled through answer types of integer, string, and VbMsgBoxResult but changing types did not help.

Second Item (not that important)
When displayed the MsgBox defaults to selection of Yes, which I would like to change to default to the No button being selected. Can the default selection when the MsgBox appears be changed?


---------------------------------------------------------------------------------------------------------------
Private Sub btnDeleteAlert_Click()

Dim answeDelete, sPrompt, sMsgTitle As String
Dim nMsgAnswer As VbMsgBoxResult

sMsgTitle = "Delete " & frmPlans.frameActivityAlerts.txtBoxAlertText
sPrompt = "This action cannot be undone. Are you sure you want to continue?"

nMsgAnswer = MsgBox(sPrompt, vbYesNo, sMsgTitle)
If nMsgAnswer = vbNo Then GoTo endSub

deleteOK:
Rem sPlanFirstAlertRow sPlanLastAlertRow sSelectedAlertRow
Call getFirstLastSelectedAlertRows
endSub:
End Sub

Picture1.png
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
My guess is that the call to getFirstLastSelectedAlertRows also contains a msgbox function so you're seeing both. The fact that your message in the code and the message in your pic is not the same supports this. The latter is missing the ?
Tips:
- put a break point at the start of the sub & step through the code (F8) and follow the execution.
- post vba code in vba code tags (vba button on posting toolbar) to maintain indentation and improve readability. Note that in your multi-line declaration, only sMsgTitle is a string and the others are variants because they are not explicitly declared.

Yes, you can default to a different button. Research msgbox function to see all of the parameters. Numeric values for vb constants can be summed; e.g you can write
Msgbox "Message goes here vbYesNo+vbDefaultButton2

or you can add their values (4 and 256) and write
"Message goes here, 260
 
Upvote 0
My guess is that the call to getFirstLastSelectedAlertRows also contains a msgbox function so you're seeing both. The fact that your message in the code and the message in your pic is not the same supports this. The latter is missing the ?
Tips:
- put a break point at the start of the sub & step through the code (F8) and follow the execution.
- post vba code in vba code tags (vba button on posting toolbar) to maintain indentation and improve readability. Note that in your multi-line declaration, only sMsgTitle is a string and the others are variants because they are not explicitly declared.

Yes, you can default to a different button. Research msgbox function to see all of the parameters. Numeric values for vb constants can be summed; e.g you can write
Msgbox "Message goes here vbYesNo+vbDefaultButton2

or you can add their values (4 and 256) and write
"Message goes here, 260
Thanks so much. After posting I created a macro that did nothing but present a Yes/No MsgBox and it worked perfectly. On a whim, I moved the code out of the btnDeleteAlert_Click() event routine to the code module and everything worked. This seems like a bug to me... but I am probably wrong.

By the way, I must have truncated the 'as string' at some point but it was there when I was testing. Thank you for spotting that. I will will change back to explicitly declaring the variables.
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,893
Members
449,097
Latest member
dbomb1414

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