Tom’s Tutorials For Excel: Message Box “Carriage Return”

Tom’s Tutorials For Excel: Message Box “Carriage Return”

To start a new line in your Message Box prompt, insert a Visual Basic Carriage Return Line Feed character, coded as vbCrLf.

Example macros:

Sub MessageBoxNewLinesA()
MsgBox "This is sentence 1." & vbCrLf & "This is sentence 2." & vbCrLf & "This is sentence 3."
End Sub

You can use the underscore character for easier readability of a lengthy code line:

Sub MessageBoxNewLinesB()
MsgBox _
"This is sentence 1." & vbCrLf & _
"This is sentence 2." & vbCrLf & _
"This is sentence 3."
End Sub
Share Button
Posted in Tom's Tutorials for Excel
Tags: , , , , , , , , , , ,
3 comments on “Tom’s Tutorials For Excel: Message Box “Carriage Return”
  1. Rich says:

    For some reason the following combination DOES NOT work!!!

    Dim Value1, Value2 as Integer

    MsgBox “Line 1 = ” & Value1 & vbCrLf & “Line 2 = ” &Value2

    ***************

    The following works but requires two separate message boxes

    MsgBox “Line 1 = ” & Value1
    MsgBox “Line 2 = ” &Value2

    • Tom Urtis says:

      There are at least two flaws in your code. One flaw is your incorrect syntax for variable declaration:
      Dim Value1, Value2 as Integer

      In that line you posted, Value1 is declared as a Variant type because it is not defined. Value2 is declared as an Integer type because it is defined.

      You need to write it as

      Dim Value1 as Integer, Value2 as Integer
      MsgBox “Line 1 = ” & Value1 & vbCrLf & “Line 2 = ” & Value2

      Of course, as it is, all you will get is a single message box that shows:

      Line 1 = 0
      Line 2 = 0

      And that is because you do not show in your example how the Value1 and Value2 variables are defined. If you write your code like this, for example…

      Dim Value1 as Integer, Value2 as Integer
      Value1 = 5
      Value2 = 9
      MsgBox “Line 1 = ” & Value1 & vbCrLf & “Line 2 = ” & Value2

      you will get this message box result:

      Line 1 = 5
      Line 2 = 9

      Finally, instead of saying that something you tried “DOES NOT work!!”, say what result you get that explains what “DOES NOT work” means to you. Having answered Excel questions for over 20 years, I’ve seen people say that and end up explaining everything from a run time error to an electrical malfunction in their office.

  2. Rick Rothstein says:

    One comment about your use of vbCrLf… that is not needed for MessageBoxes, TextBoxes, Cell values and maybe some other things not coming to mind at the moment. In VBA, vbLf is sufficient. When writing files out to a hard disk… then vbCrLf should be used to separate text lines for a PC, but in olden days (and I assume still to this day), a Mac uses vbCr for its text line separators. There is a predefined constant that works correctly for disk files no matter which computer is being used… vbNewLine (it automatically expands to vbCrLf on a PC and vbCr, or whatever is currently being used, on a Mac).

Leave a Reply

Your email address will not be published. Required fields are marked *

*