Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
547 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
When you have a long message to display, you might want to break it into multiple lines. This page describes how to add multiple lines of text to a single message box.
We probably don't need quite so many lines for this message.
You don't need any files for this section.
You can click here to download a workbook containing the sample code.
You can insert a new line character into a message box prompt by using the vbNewLine constant. Using any workbook, insert a new module and create a subroutine called UsingMultipleLines. Add an instruction to display a message box as shown below:
Concatenate the vbNewLine constant between the lines of your message.
Running the code shown above will produce a message box with two lines of text:
The exact text that appears depends on the date and name of the workbook in which your code is stored.
You may see other people using an older technique to achieve the same result. You can use vbCrLf in place of vbNewLine to create a new line. Create a new subroutine or edit the existing one to demonstrate this:
This code produces the same result as the easier-to-understand vbNewLine.
vbCrLf stands for Visual Basic Carriage-Return Line-Feed.
Yet another technique you may see involves concatenating the individual characters for a carriage-return and line-feed:
The result of running this code is the same as the previous two examples.
If you're curious about the history of new line characters (and who isn't?!), Wikipedia has articles on Control Characters and Teleprinters. Enjoy!
To practise using variables and new lines with a message box:
Sub UsingVariablesAndNewLines()
'declare a variable
Dim MsgText As String
'assign a string to the variable
MsgText = "Hello " & Environ("UserName")
End Sub
Sub UsingVariablesAndNewLines()
'declare a variable
Dim MsgText As String
'assign a string to the variable
MsgText = "Hello " & Environ("UserName")
'display the variable's value
MsgBox MsgText
End Sub
Sub UsingVariablesAndNewLines()
'declare a variable
Dim MsgText As String
'assign a string to the variable
MsgText = "Hello " & Environ("UserName")
MsgText = MsgText & vbNewLine
MsgText = MsgText & "Welcome to " & ThisWorkbook.Name
'display the variable's value
MsgBox MsgText
End Sub
The exact message will depend on your user name and the file name.
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.