How to use \n new line in VB msgbox() ...?

426,970

Solution 1

  • for VB: vbCrLf or vbNewLine
  • for VB.NET: Environment.NewLine or vbCrLf or Constants.vbCrLf

Info on VB.NET new line: http://msdn.microsoft.com/en-us/library/system.environment.newline.aspx

The info for Environment.NewLine came from Cody Gray and J Vermeire

Solution 2

Try using vbcrlf for a newline

msgbox "This is how" & vbcrlf & "to get a new line"

Solution 3

These are the character sequences to create a new line:

  • vbCr is the carriage return (return to line beginning),

  • vbLf is the line feed (go to next line)

  • vbCrLf is the carriage return / line feed (similar to pressing Enter)

I prefer vbNewLine as it is system independent (vbCrLf may not be a true new line on some systems)

Solution 4

Use the Environment.NewLine property

Solution 5

Add a vbNewLine as:

"text1" & vbNewLine & "text2"
Share:
426,970

Related videos on Youtube

Wasim A.
Author by

Wasim A.

[email protected] | Skype:wasimxe | Whatsapp: +923455407008

Updated on February 20, 2022

Comments

  • Wasim A.
    Wasim A. over 2 years

    What is the alternative to \n (for new line) in a MsgBox()?

    • Cody Gray
      Cody Gray over 13 years
      Are you asking about VB.NET or VB 6?
    • Peter Mortensen
      Peter Mortensen about 10 years
    • user692942
      user692942 over 2 years
      @PeterMortensen who decided this question should be changed to VB.Net?
  • Pranay Rana
    Pranay Rana over 13 years
    @Andrew - during my college days when i do programming in vb
  • Cody Gray
    Cody Gray over 13 years
    Theoretically, I suppose you're right. But in every implementation that I've seen, vbNewLine is simply defined as vbCrLf. There isn't any difference.
  • Cody Gray
    Cody Gray over 13 years
    This is the correct solution for VB.NET. Skip the vbCrLf/vbNewLine nonsense unless you're using VB 6.
  • Cody Gray
    Cody Gray over 13 years
    I wouldn't change anything. I agree with you the way it is. I think vbNewLine more clearly expresses your intent. It's obviously been designed to insert a new line. I was just providing auxiliary commentary. "Under the hood", they do the same thing. Implementation details like that shouldn't leak into your code.
  • Cody Gray
    Cody Gray over 13 years
    Do not use vbCrLf for VB.NET code! That's only there for compatibility with ported VB 6 applications. You should be using Environment.NewLine instead, as suggested in J. Vermeire's answer.
  • Fun Mun Pieng
    Fun Mun Pieng over 13 years
    I agree with you that Environment.NewLine should be used, but because it is cross platform. It returns \r\n for Window, but only \n for unix/linux based platforms.
  • Cody Gray
    Cody Gray over 13 years
    Yes, that's a very good reason to use it. Considering that alternative implementations of the CLR (like Mono) support VB.NET, it's a good idea to write code with an eye towards platform independence. Beyond that, adopting standard .NET Framework idioms, rather than holdovers for backwards compatibility purposes, is always a good idea. The biggest mistake any VB.NET programmer will make is assuming it's the same language as VB 6. It's emphatically not! The true object-orientation is only scratching the surface of the differences. Pretending otherwise is doing yourself an injustice.
  • Samuel
    Samuel over 11 years
    Down vote because it doesn't work for me. Calling EditPoint.Insert(vbNewLine) in a VB macro inserts \r\n. vbLf is the correct answer to the asked question.
  • Scott Solmer
    Scott Solmer over 9 years
    Your answer (Environment.NewLine) was already given 3 years ago and is the highest voted answer. This is more like commentary. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post.
  • Lenard Bartha
    Lenard Bartha about 4 years
    Shouldn't it be Chr(10)?
  • Tim Morton
    Tim Morton almost 4 years
    The example is appreciated because I don't work with VB and didn't know the concatenation format. They are evidently not interpreted within double quotes.
  • Marcucciboy2
    Marcucciboy2 over 3 years
    @LenardBartha likely not stackoverflow.com/a/38792168/2727437
  • XEROling
    XEROling about 3 years
    I just declared a public string called n... just for convenience. (Public n As String = Environment.NewLine)
  • user692942
    user692942 over 2 years
    Which is equivalent to the vbCr built in constant.