61

I want to concatenate two strings with a linebreak between them.

st = "Line 1" + newline + "Line2"

How do I add a newline to VBA or Visual Basic 6?

6
  • 5
    You'll want to do this: st = "Line 1" + vbCrLf + "Line2" Commented Nov 21, 2014 at 17:54
  • 4
    Probably because this is very easy to search for. I would argue this Q/A isn't really helpful, as it's well-known/well-documented information. Commented Nov 21, 2014 at 17:57
  • Yes a simple Google search for "how do i create a new line in a string vba" returns the answers. Especially given you answered your own question as a "I'm going to throw this out there as a good searchable answer because it doesn't exist" but in reality it does. It does exist. Commented Nov 21, 2014 at 18:00
  • 1
    blog.stackoverflow.com/2011/07/… Commented Apr 19, 2016 at 16:21
  • 6
    I disagree with the "asked and answered" crowd. I come to stack overflow not only to find answers, but to find out the best answers. In this case, it seems that there are no escape sequences in VBA. Good to know. While this was an overly simple example ;), there are plenty where I go to stack overflow to look first. Hopefully my +1 keeps you excited about generating answers for people like myself. Commented Dec 14, 2016 at 21:10

5 Answers 5

104

Visual Basic has built-in constants for newlines:

vbCr = Chr$(13) = CR (carriage-return character) - used by Mac OS and Apple II family

vbLf = Chr$(10) = LF (line-feed character) - used by Linux and Mac OS X

vbCrLf = Chr$(13) & Chr$(10) = CRLF (carriage-return followed by line-feed) - used by Windows

vbNewLine = the same as vbCrLf

Sign up to request clarification or add additional context in comments.

5 Comments

While this is the correct answer, this question has been asked and answered many times.
@voters instead of downvoting a correct answer, try close voting the question if you think it's a duplicate.
Doesn't VBA use chr not char?
@Usagi Updated it to match VBA/VB6. Thank you for your input!
For a complete documentation, if you want a line jump, equal to Shift + Enter in Word, use Chr(11)
22

Use this code between two words:

& vbCrLf &

Using this, the next word displays on the next line.

1 Comment

vbNewLine is preferred because it will select the correct line terminator.
2

There are actually two ways of doing this:

  1. st = "Line 1" + vbCrLf + "Line 2"

  2. st = "Line 1" + vbNewLine + "Line 2"

These even work for message boxes (and all other places where strings are used).

Comments

-1

This is really simple one. You can user constant vbCrlf or vbNewLine

Code will be as follows:

st = "Line1" & vbCrlf & "Line2"

Or

st = "Line1" & vbNewLine & "Line2"

Comments

-2

Here’s a concise solution for adding a newline in VB6:

Dim myString As String
myString = "Line 1" & vbCrLf & "Line 2"
MsgBox myString

Just use vbCrLf to insert a new line. Alternatively, for constants:

Const NEWLINE As String = vbCrLf
Dim myString As String
myString = "Line 1" & NEWLINE & "Line 2"
MsgBox myString

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.