ActiveX Control Return Keypress MsgBox Loop

tomgreen1000

New Member
Joined
Jun 22, 2007
Messages
8
A simplified version of my problem. Consider a single command button on a blank worksheet. I want to be able execute the command button Click event with both the Return key and a mouse click. I want to be able to clear the message box with both the Return key and a mouse click. If I try to clear the message box with the Return key, it also executes the command button Click event again. So using the Return key to clear the message box puts you into a loop. Any ideas are greatly appreciated.

Thanks, Tom

In ThisWorkbook
Code:
Private Sub Workbook_Open()
Sheet1.Button1.Activate
End Sub
In Sheet1
Code:
Private Sub Button1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Select Case KeyCode
    'Return is only key press that we care about
    Case vbKeyReturn
    Call Button1_Click
End Select
End Sub
Private Sub Button1_Click()
MsgBox "I am stuck in a loop when I use the Return key to clear this message box" & vbNewLine & "but everything is fine if I use a mouse click to clear this message box"
End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

Joe Was

MrExcel MVP
Joined
Feb 19, 2002
Messages
7,539
You can control which button in a MsgBox is the default, so if the user presses the ENTER(Return) key the button you picked, as the default, is activated!

In this case I made the CANCEL button the Default.


Sub myBox()

MsgBox "Test Msg!", vbDefaultButton3 + vbInformation + vbYesNoCancel, "Test!"
End Sub
 

Forum statistics

Threads
1,181,416
Messages
5,929,790
Members
436,693
Latest member
BroTr

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
Top