Microsoft training courses | Wise Owl - home page

Phone (01457) 858877 or email

USING MSGBOX TO DISPLAY MESSAGES IN VBA MACROS

Part one of a five-part series of blogs

The MsgBox command in Visual Basic for Applications allows you to display basic messages, but you can also make it prettier, and even use it to ask questions or display multiple-line messages! Read on to find out more about this versatile VBA command.

  1. Using MsgBox to Display Messages in VBA Macros (this article)
  2. Displaying Message Boxes in VBA macros
  3. Joining Bits of a Message Together
  4. Customising your Message Box in VBA macros
  5. Using MsgBox to Ask Questions in VBA macros

This blog is part of our Excel macros online tutorial series.  Our main business is running training courses in Excel, courses in VBA and training in many other Microsoft applications.

Posted by Andy Brown on 28 September 2011 | 2 comments

Using MsgBox to Display Messages in VBA Macros

Whether you're writing Visual Basic for Applications code in Excel, Access, PowerPoint, Word or any other application, you can use the MsgBox command to display a pop-up message on screen. 

Don't confuse message boxes with input boxes, the subject of a separate blog.  Message boxes display a message on screen; input boxes ask you to type something in.

Message boxes thus displayed can be simple:

Simple message box

A simple message box, without any customisation at all.

 

Or customised:

A customised MsgBox message box

In this message box, we've changed the titles and buttons and added a question mark symbol.

 

If you want to customise your message boxes more than this, you're out of luck - you can only add so many bells and whistles.  In such a case you're best off creating a user form, which you can format as much as you like.

Without further ado, then, let's look at what you can do to create a message box ... beginning with what it is and the basic syntax.

USING MSGBOX TO DISPLAY MESSAGES IN VBA MACROS

Part one of a five-part series of blogs

The MsgBox command in Visual Basic for Applications allows you to display basic messages, but you can also make it prettier, and even use it to ask questions or display multiple-line messages! Read on to find out more about this versatile VBA command.

  1. Using MsgBox to Display Messages in VBA Macros (this article)
  2. Displaying Message Boxes in VBA macros
  3. Joining Bits of a Message Together
  4. Customising your Message Box in VBA macros
  5. Using MsgBox to Ask Questions in VBA macros

This blog is part of our Excel macros online tutorial series.  Our main business is running training courses in Excel, courses in VBA and training in many other Microsoft applications.

Comments on this blog

This blog has 2 comments:

Comment added by Beginner on 02 October 2011 at 21:45 GMT

Hi,

I have to say "GOOD JOB", and I like Your writting style. You present the ideas in a lovely manner.

 
Comment added by avicbz on 19 February 2013 at 10:15 GMT
Hi Andy,

In your macro "Displaying worksheet names", how about initializing msg variable within the loop, as given below?
When i do this, the result shows me only one name "Sheet3". Could you please help in understanding this. Many Thanks.

Example : 

Sub ShowSheetName()
Dim i As Integer
Dim msg As String
For i = 1 To Sheets.Count
msg = "This file has below mentioned sheets: " & vbCrLf & vbCrLf & Sheets(i).Name
Next i
MsgBox msg
End Sub
Reply from Andy Brown (blog author)
If you intitalise the variable within the loop, it will be overwritten each time you go through the loop.  That's why you're just getting the name of the last sheet.

All content copyright Wise Owl Business Solutions Ltd 2013. All rights reserved.