57

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gerhard Powell
  • 5,965
  • 5
  • 48
  • 59
  • 4
    You'll want to do this: [`st = "Line 1" + vbCrLf + "Line2"`](http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.constants.vbcrlf%28v=vs.110%29.aspx) – admdrew Nov 21 '14 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. – admdrew Nov 21 '14 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. – Chrismas007 Nov 21 '14 at 18:00
  • 4700-20K+ google hits for this on this site alone (depending on how you word it). I am quite certain MSDN would have the answer as well. – Ňɏssa Pøngjǣrdenlarp Nov 21 '14 at 18:00
  • 1
    http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/ – Gerhard Powell Apr 19 '16 at 16:21
  • 5
    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 Dec 14 '16 at 21:10

3 Answers3

99

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gerhard Powell
  • 5,965
  • 5
  • 48
  • 59
23

Use this code between two words:

& vbCrLf &

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gajanan Chitare
  • 349
  • 1
  • 3
  • 4
3

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131