Excel VBA MsgBox

The MsgBox function will be used to display a message box to wait user to click a button, and then returns an integer indicating which button the user clicked.

Syntax

MsgBox(prompt [,buttons] [,title] [helpfile,context])

More detailed about arguments of Msgbox, pls visit Microsoft official site.( https://support.office.com/en-US/article/MsgBox-Function-E23511C8-4157-4E09-93A6-BA9F1749F4C0)

Example

Example1 : show multiple lines of string using MsgBox

1# open visual Basic Editor, then insert a module and name as :myMsgBoxdemo1.

excel msgbox demon1

2# enter into the below vba code in code window.

Sub multipleLineString()
 Dim msgstr As String
 msgstr = "Excelhow.net" & Chr(13) & Chr(10)
 msgstr = msgstr & "learning ms excel" & vbCrLf
 msgstr = msgstr & "online free"
 MsgBox msgstr, , "welcome"
End Sub

excel msgbox demon2
3# back to workbook, then run above macro.

show multiple lines of string using MsgBox1

Sample2: show multiple style MsgBox dialog box

1# open visual Basic Editor, then insert a module and name as :myMsgBoxdemo2.

2# enter the below VBA codes in code window.

Sub myMsgBoxdemo2()
 MsgBox "vbOKCancel + vbCritical", vbOKCancel + vbCritical, "style1"
 MsgBox "vbAbortRetryIgnore + vbQuestion", vbAbortRetryIgnore + vbQuestion, "style2"
 MsgBox "vbYesNo + vbInformation", vbYesNo + vbInformation, "style3"
 MsgBox "vbYesNoCancel + vbExclamation", vbYesNoCancel + vbExclamation, "style4"
End Sub

excel msgbox demon3

3# back to the current workbook, run the above vba macro.

excel msgbox demon4

show multiple style MsgBox dialog box1

MsbBox Function return values as bellows:

Constant Value Description
vbOK 1 OK
vbCancel 2 Cancel
vbAbort 3 Abort
vbRetry 4 Retry
vbIgnore 5 Ignore
vbYes 6 Yes
vbNo 7 No

 

 

Related Posts

VBA Macro For VLOOKUP From Another Sheet

In the previous post, you should know that how to fix or remove the #N/A error when using VLOOKUP formula to lookup value from another sheet. And this post will show you how to use VBA code to vlookup data ...

How To Remove Special Characters in Excel

This post will show you how to remove special characters from text strings in Excel. And I am going to introduce two ways to remove special characters (@&)#%$) from a string in Excel. Removing Special Characters using Power Query If ...

How To Insert Comments in Protected Worksheet in Excel

This post will show you how to allow comments in a protected worksheet in Excel. You can easily to insert comments into cells in a normal worksheet in Excel, but if want to insert a comment in a worksheet that ...

How To Convert Text to Upper Cases(Using VBA) in Excel

This post will show you how to switch from lower case to upper case in Excel. and I am going to show you two different ways of converting text to upper cases using formula or VBA macro in Excel 2013,Excel ...

How To Hide Every Other Row in Excel (Using VBA)

This post will show you how to hide alternate rows or columns in Excel or how to hide every third, fourth, fifth row or column in Excel. If you want to hide every other row in your current worksheet, how ...

Sidebar