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: , , , , , , , , , , ,
2 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.

Leave a Reply

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

*