Creación de Cuadros de Mensaje Personalizados Con La Función MsgBox de VBA
Creación de Cuadros de Mensaje Personalizados Con La Función MsgBox de VBA
Creación de Cuadros de Mensaje Personalizados Con La Función MsgBox de VBA
EN ESTE ARTÍCULO
Sin embargo, puede hacer mucho más que mostrar un simple cuadro de mensaje OK. Veamos
rápidamente un ejemplo complicado antes de entrar en detalles...
En la siguiente sección te mostraremos todas las opciones disponibles para ti a la hora de crear
cuadros de mensaje. A continuación, le presentaremos la sintaxis de la función MsgBox y, por
último, repasaremos otros ejemplos de cuadros de mensaje.
This is a screenshot of the “MessageBox Builder” from our Premium VBA Add-in: AutoMacro.
The MessageBox Builder allows you to quickly design your desired messagebox and insert the
code into your code module. It also contains many other code builders, an extensive VBA code
library, and an assortment of coding tools. It’s a must-have for any VBA developer.
buttons – Choose which buttons to display. If omitted, ‘OKonly’. Here you can also specify what
icon to show and the default button.
title – The title at the top of the message box. If omitted, the name of the current application is
displayed (ex. Microsoft Excel).
helpfile – Specify help file that can be accessed when user clicks on the ‘Help’ button. If
specified, then you must also add context (below)
context – Numeric expression representing the Help context number assigned to the
appropriate Help topic.
You can probably ignore the helpfile and context arguments. I’ve never seen them used.
Msgbox "Prompt",,"Title"
Another example:
Sub MsgBoxPromptTitle()
MsgBox "Step 1 Complete. Click OK to run step 2.",, "Step 1 of 5"
End Sub
MessageBox LineBreaks
You can also add line breaks to your message box prompts with ‘vbNewLine’.
Sub MsgBoxPromptTitle_NewLine()
MsgBox "Step 1 Complete." & vbNewLine & "Click OK to Run Step 2.", , "Step 1 o
End Sub
Notice we use the & symbol to join text together. You can learn more about using & with text
and other options for inserting linebreaks in our article on joining text.
MsgBox Icons
VBA gives you the ability to add one of four pre-built icons to your message boxes:
Icon Constant Icon
vbInformation
vbCritical
vbQuestion
vbExclamation
Sub MsgBoxQuestionIcon()
MsgBox "Question Example", vbQuestion
End Sub
This will generate the default ‘OK’ message box with the Question icon:
Notice how when you type, the VBA Editor will show you the options available to you:
This is helpful because you don’t need to remember the exact syntax or names of icons or
buttons.
Below we will talk about generating message boxes with different button layouts. If you do
choose a different message box type, you will need to append the icon type after the buttons
using a “+”:
Sub MsgBoxQuestionIcon()
MsgBox "Do you want to continue?", vbOKCancel + vbQuestion
End Sub
MsgBox Variables
So far we have worked primarily with the default ‘OK’ message box. The OK message box only
has one option: Pressing ‘OK’ allows the code to continue. However, you can also specify other
button groupings: OK / Cancel, Yes / No, etc.
In which case you will want to perform different actions based on which button is pressed. Let’s
look at an example.
Sub MsgBoxVariable()
End Sub
The MsgBox function returns an integer value (between 1-7) so we define the variable as an
integer type. However, instead of referring to the integer number, you can refer to a constant
(ex. vbOK, vbCancel, etc.). Look at this table to see all of the options:
Button Constant Value
OK vbOK 1
Cancel vbCancel 2
Abort vbAbort 3
Retry vbRetry 4
Ignore vbIgnore 5
Yes vbYes 6
No vbNo 7
Sub MsgBox_OKOnly()
End Sub
Sub MsgBox_OKCancel()
End Sub
Sub MsgBox_YesNo()
End Sub
Sub MsgBox_YesNoCancel()
End Sub
Sub MsgBox_AbortRetryIgnore()
End Sub
End Sub
Sub Msgbox_BeforeRunning()
End Sub
'Some Code
End Sub
Free Download
Learn More
Company
AI
ABOUT
FORMULA GENERATOR
EDITORIAL TEAM
VBA GENERATOR
CONTACT US
TABLE GENERATOR
REVIEWS
HELP CHAT
940 REVIEWS
VBA Excel
Disclaimer
Privacy Policy
Cookie Policy
Notice at Collection
Copyright © 2024. Microsoft® And Excel® are trademarks of the Microsoft group of
companies. Microsoft does not endorse, promote, or warrant the accuracy of information
found on AutomateExcel.com.