54
votes

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?

3
You'll want to do this: st = "Line 1" + vbCrLf + "Line2" - admdrew
I don't know why people downvote. I did not find the solution on Stack Overflow, and I am trying to help other people that also search for a solution. - Gerhard Powell
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. - admdrew
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. - Gerard ONeill

3 Answers

94
votes

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

21
votes

Use this code between two words:

& vbCrLf &

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

3
votes

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).