Message Box in VBA Excel | debugvba.com

input box vba excel

Objective

To create message box using VBA Excel.

Approach

In this VBA Excel automation, we shall see how we can use MsgBox function to create a message box. Although there are multiple options available in message box function, but we shall go through only those options which are used frequently. You can go through all the other options by using the link provided in reference section.

vbOkCancel

We can use Ok and Cancel option in message box. It will ask us to execute the code next or cancel (or exit the code). I have just added a value in cell A1 to demonstrate how this functionality works.

Sub MsgBoxOkCancel()
Dim UserInput As Variant

UserInput = MsgBox(Prompt:="Would you like to proceed", Buttons:=vbOKCancel, Title:="Error Message")

If UserInput = vbOK Then
    Range("A1").Value = 10
Else
    Range("A1").Value = 20
End If

End Sub

 

input box vba

 

vbOK

This option is used when we only want to display an information message to end user, but we do not require any input from user. We only need to click on “ok” button, once we click on “ok” button code will execute further.

Sub MsgBoxvbInformation()
Dim UserInput As Variant

MsgBox Prompt:="It is just a information message", Buttons:=vbInformation, Title:="Info Message"

End Sub

 

input box vba

 

Reference

https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/msgbox-function

Post you may like

Format excel cells and border using VBA