XL2000: How to Use Visual Basic Constants in Text Strings
This article was previously published under Q211774
This article has been archived. It is offered "as is" and will no longer be updated.
Microsoft Visual Basic for Applications includes a number of intrinsic constants that you can use in macro code to insert certain formatting characters, such as tabs, line feeds, and carriage returns. This article contains information about using these constants and about issues that may occur when you use them.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs.
If you have limited programming experience, you may want to contact a Microsoft Certified Partner or Microsoft Advisory Services. For more information, visit these Microsoft Web sites:
Microsoft Certified Partners - https://partner.microsoft.com/global/30000104
Microsoft Advisory Services - http://support.microsoft.com/gp/advisoryservice
For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMSThe constants referred to in the "Summary" section are listed in the following table.
Constant Definition ------------------------------------------------------------------- vbBack A backspace character [Chr(8)] vbCr A carriage return [Chr(13)] vbCrLf A carriage return and line feed [Chr(13) + Chr(10)] vbLf A linefeed [Chr(10)] vbNewLine A platform-specific new line character, either [Chr(13) + Chr(10)] or [Chr(13)] vbNullChar A null character of value 0 [Chr(0)] vbNullString A string of value 0 [no Chr code]; note that this is not the same as "" vbTab A tab character [Chr(9)]You can use these constants anywhere in Visual Basic code that you wantthem to appear. For example, you can use them to display a multi-linemessage in a message box, as in the following example:
Make sure that you do not enclose the constants in quotation marks; ifyou do, the constants appear in the text string instead of as the characters that they represent.
MsgBox "Hello" & vbCr & "World!"
When you use these constants, you may notice the following issues.
Text Boxes and CellsIn Excel 2000, if you use these constants when you insert text into a text box or into a cell, a square character may appear in the text box or cell. This behavior occurs if you use any of the following constants:
- If you run the following line of codethe cell displays the following value:
ActiveCell.Value = "AAA" & vbNewLine & "BBB"AAA[square character]You can remove the square character by manually editing the cell.
- If you use the vbNullChar constant, text following the constant may be cut off. For example, if you run the following line of codethe cell displays only "AAA."
ActiveCell.Value = "AAA" & vbNullChar & "BBB"
- The vbLf and vbNullString constants work correctly when you use them with text boxes and cells. For example, if you run the following line of codethe cell displays the following value:
ActiveCell.Value = "AAA" & vbLf & "BBB"AAAIf you run the following line of code
BBBthe cell displays the following value
ActiveCell.Value = "AAA" & vbNullString & "BBB"AAABBBbecause vbNullString creates a string of value 0 (the string has no length so nothing appears in the cell).
Message Boxes (MsgBox)All of the constants listed in this article work correctly when used in aMsgBox, with the exception of the following:
- If you use the vbBack constant, a square character appears in the message box. For example, if you run the following line of codethe message box displays the following message:
MsgBox "AAA" & vbBack & "BBB"AAA[square character]BBB
- If you use the vbNullChar constant, text following the constant is cut off.
square rectangle box XL2000
Article ID: 211774 - Last Review: 12/05/2015 11:46:04 - Revision: 3.5
Microsoft Excel 2000 Standard Edition
- kbnosurvey kbarchive kbcode kbhowto kbinfo kbprogramming KB211774