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

- 16,398
- 7
- 76
- 175

- 9,660
- 22
- 90
- 120
-
1Are you asking about VB.NET or VB 6? – Cody Gray - on strike Mar 01 '11 at 08:42
-
See also *[End-of-line identifier in VB.NET?](http://stackoverflow.com/questions/1399268)*. – Peter Mortensen Apr 09 '14 at 00:18
-
1@PeterMortensen who decided this question should be changed to VB.Net? – user692942 Feb 19 '22 at 22:22
17 Answers
- for VB:
vbCrLf
orvbNewLine
- for VB.NET:
Environment.NewLine
orvbCrLf
orConstants.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

- 23,794
- 27
- 122
- 225

- 6,751
- 3
- 28
- 30
-
12Do 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. – Cody Gray - on strike Mar 01 '11 at 08:55
-
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. – Fun Mun Pieng Mar 02 '11 at 05:00
-
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. – Cody Gray - on strike Mar 02 '11 at 05:39
-
2Down 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. – Samuel Nov 27 '12 at 18:16
-
1I just declared a public string called `n`... just for convenience. (`Public n As String = Environment.NewLine`) – Galacticai Apr 13 '21 at 21:26
Try using vbcrlf
for a newline
msgbox "This is how" & vbcrlf & "to get a new line"

- 23,794
- 27
- 122
- 225

- 8,390
- 41
- 129
- 238
-
1The 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. – Tim Morton Aug 14 '20 at 21:46
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)

- 239,200
- 50
- 490
- 574

- 175,020
- 35
- 237
- 263
-
-
1Theoretically, 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 - on strike Mar 01 '11 at 08:42
-
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 - on strike Mar 01 '11 at 08:46
Use the Environment.NewLine
property

- 239,200
- 50
- 490
- 574

- 3,249
- 2
- 25
- 42
-
This is the correct solution for VB.NET. Skip the `vbCrLf`/`vbNewLine` nonsense unless you're using VB 6. – Cody Gray - on strike Mar 01 '11 at 08:42
An alternative to Environment.NewLine is to use :
Regex.Unescape("\n\tHello World\n")
from System.Text.RegularExpressions
This allows you to escape Text without Concatenating strings as you can in C#, C, java

- 318
- 5
- 10
Use the command "vbNewLine"
Example
Hello & vbNewLine & "World"
will show up as Hello on one line and World on another

- 41
- 1
- 3
You can use Environment.NewLine OR vbCrLF OR vbNewLine
MsgBox($"Hi!{Environment.NewLine}I'M HERE")

- 21
- 4
You can use carriage return character (Chr(13)), a linefeed character (Chr(10)) also like
MsgBox "Message Name: " & objSymbol1.Name & Chr(13) & "Value of BIT-1: " & (myMessage1.Data(1)) & Chr(13) & "MessageCount: " & ReceiveMessages.Count

- 33
- 4
Module MyHelpers
<Extension()>
Public Function UnEscape(ByVal aString As String) As String
Return Regex.Unescape(aString)
End Function
End Module
Usage:
console.writeline("Ciao!\n".unEscape)

- 11
- 2
On my side I created a sub MyMsgBox replacing \n in the prompt by ControlChars.NewLine

- 11
- 1
A lot of the stuff above didn't work for me. What did end up working is
Chr(13)

- 2,851
- 2
- 17
- 22
-
-
@LenardBartha likely not https://stackoverflow.com/a/38792168/2727437 – Marcucciboy2 Oct 22 '20 at 14:48
-
Which is equivalent to the `vbCr` [built in constant](https://learn.microsoft.com/en-us/office/vba/Language/Reference/user-interface-help/miscellaneous-constants). – user692942 Feb 21 '22 at 13:38
msgbox("your text here" & Environment.NewLine & "more text") is the easist way. no point in making your code harder or more ocmplicated than you need it to be...

- 31
- 2
- 7
-
1Your 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](http://stackoverflow.com/help/privileges/comment) you will be able to comment on any post. – Scott Solmer Nov 18 '14 at 17:07