Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

XL2000: How to Copy Text to Text Boxes Using the Characters Method


View products that this article applies to.

This article was previously published under Q213802

↑ Back to the top


Summary

In a Microsoft Excel worksheet, you can use text box objects to add text that is not limited by the boundary of a cell. You can also use text boxes on dialog sheets and chart sheets when specially formatted text is required.

The text string that is copied to or from a text box object has a 255-character restriction when you use the Characters method in Visual Basic for Applications. In other words, although a text box is capable of holding approximately 2,000 characters, the text must be added in strings that are no more that 255 characters in length.

↑ Back to the top


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 first example Sub procedure (that is, TextBox_To_TextBox) demonstrates how to copy text from one text box drawing object into another text box. The second example Sub procedure, Cell_Text_To_TextBox, demonstrates a method that you can use to copy the value of a range of cells into a text box.

Example 1

To copy text from one text box drawing object into another text box, use the following code:
Sub TextBox_To_TextBox()

   ' Dimension the variables.
   Dim x As Integer
   Dim txtBox1 As TextBox, txtBox2 As TextBox
   Dim theText As String

   ' Set txtBox1 and txtBox2 equal to the active sheet's TextBox
   ' objects. Replace the ordinal number with your TextBox names
   ' in quotes. For example: ActiveSheet.DrawingObjects("Text 1")
   Set txtBox1 = ActiveSheet.DrawingObjects(1)
   Set txtBox2 = ActiveSheet.DrawingObjects(2)

   ' Create a For-Next construct that loops until there is no more
   ' text in txtBox1.
   For x = 1 To txtBox1.Characters.Count Step 250

      ' Place the first text box text into a variable called theText.
      theText = txtBox1.Characters(start:=x, Length:=250).Text

      ' Place the value of theText variable into second text box.
      txtBox2.Characters(start:=x, Length:=250).Text = theText
   Next

End Sub
				

Example 2

To copy the value of a range of cells into a text box, use the following code:
Sub Cell_Text_To_TextBox()

   ' Dimension the variables.
   Dim txtBox1 As TextBox
   Dim theRange As Range, cell As Range
   Dim startPos As Integer

   ' Set txtBox1 equal to the active sheet's TextBox object. You can
   ' replace the ordinal number with your text box name in quotes.
   ' For example: ActiveSheet.DrawingObjects("Text 1")
   Set txtBox1 = ActiveSheet.DrawingObjects(1)

   ' Set a range on the active sheet equal to the range object text
   ' that you are interested in copying to the text box.
   Set theRange = ActiveSheet.Range("A1:A10")

   'Set the starting position for the text.
   startPos = 1

  ' Create a For-Each construct to loop through the cells in the range.
   For Each cell In theRange

      ' Populate the textbox with the cell values using the Characters
      ' method.
      ' Note: Chr(10) can be used to add a new line in the textbox for
      ' each cell.
      txtBox1.Characters(start:=startPos, _
         length:=Len(cell.Value)).Text = cell.Value & Chr(10)

      ' Update the startPos variable to keep track of where the next
      ' string of text will begin in the textbox.
      startPos = startPos + Len(cell.Value) + 1
    Next cell
End Sub
				

↑ Back to the top


References

For more information about how to use the sample code in this article, click the article number below to view the article in the Microsoft Knowledge Base:
212536� OFF2000: How to Run Sample Code from Knowledge Base Articles

↑ Back to the top


Keywords: KB213802, kbhowto, kbdtacode

↑ Back to the top

Article Info
Article ID : 213802
Revision : 8
Created on : 11/23/2006
Published on : 11/23/2006
Exists online : False
Views : 274