Avatar of Davisro
DavisroFlag for United States of America

asked on 

Excel 2010 VBA disabling Enter key on message box

I've noticed that the following code will disable the Enter keys in Excel, but not in a message box. So if I presented a user with a message box and a choice of Yes or No, I can't prevent them from hitting Enter to choose the default button. Why is that?

Application.OnKey "~", "" 
Application.OnKey "{Enter}", ""
Visual Basic ClassicMicrosoft ExcelVBA

Avatar of undefined
Last Comment
Martin Liss
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Do you want to make "No" the default?
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

If so

MsgBox "Your message", vbYesNo + vbDefaultButton2
Avatar of Davisro
Davisro
Flag of United States of America image

ASKER

I did that. What I don't understand is why Application.OnKey doesn't apply to message boxes. So it will disable the Enter key in the application, but not in the message box
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Most likely because a MsgBox is modal and so other code can't run while the MsgBox is open. A userform that looks like a MsgBox could be substituted and probably the OnKey code could be used there. Are you interested in something like that?
ASKER CERTIFIED SOLUTION
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2015, Experts-Exchange Top Expert Visual Basic Classic 2012 to 2014
Visual Basic Classic
Visual Basic Classic

Visual Basic is Microsoft’s event-driven programming language and integrated development environment (IDE) for its Component Object Model (COM) programming model. It is relatively easy to learn and use because of its graphical development features and BASIC heritage. It has been replaced with VB.NET, and is very similar to VBA (Visual Basic for Applications), the programming language for the Microsoft Office product line.

165K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo