XL2000: How to Use Visual Basic Constants in Text Strings

Article translations Article translations
Article ID: 211774 - View products that this article applies to.
This article was previously published under Q211774
Expand all | Collapse all

On This Page

SUMMARY

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.

MORE INFORMATION

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;CNTACTMS The 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 want them to appear. For example, you can use them to display a multi-line message in a message box, as in the following example:
MsgBox "Hello" & vbCr & "World!"
				
Make sure that you do not enclose the constants in quotation marks; if you do, the constants appear in the text string instead of as the characters that they represent.

When you use these constants, you may notice the following issues.

Text Boxes and Cells

In 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:
  • vbBack
  • vbCr
  • vbCrLf
  • vbNewLine
  • vbTab
Additionally, the following behaviors can occur under the following conditions:
  • If you run the following line of code
    ActiveCell.Value = "AAA" & vbNewLine & "BBB"
    						
    the cell displays the following value:
    AAA[square character]
    BBB
    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 code
    ActiveCell.Value = "AAA" & vbNullChar & "BBB"
    						
    the cell displays only "AAA."
  • 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 code
    ActiveCell.Value = "AAA" & vbLf & "BBB"
    						
    the cell displays the following value:
    AAA
    BBB
    If you run the following line of code
    ActiveCell.Value = "AAA" & vbNullString & "BBB"
    						
    the cell displays the following value
    AAABBB
    because 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 a MsgBox, 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 code
    MsgBox "AAA" & vbBack & "BBB"
    						
    the message box displays the following message:
    AAA[square character]BBB
  • If you use the vbNullChar constant, text following the constant is cut off.

Properties

Article ID: 211774 - Last Review: November 23, 2006 - Revision: 3.5
APPLIES TO
  • Microsoft Excel 2000 Standard Edition
Keywords: 
kbcode kbhowto kbinfo kbprogramming KB211774

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com