How to Copy Text to TextBoxes Using the Characters Method
This article was previously published under Q148815
This article has been archived. It is offered "as is" and will no longer be updated.
In a Microsoft Excel worksheet, you can use TextBox objects to add textthat is not limited by the boundary of a cell. You can also use text boxeson dialog sheets and chart sheets when specially formatted text isrequired.
The text string that is copied to or from a TextBox object has a 255-character restriction when you use the Characters method in Visual Basicfor Applications. In other words, although a text box is capable of holdingapproximately 2,000 characters, the text must be added in strings that areno more that 255 characters in length. You can use the Sub procedures inthe "More Information" section of this article to work around thisrestriction.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers 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 requirements. The first Sub procedure (that is, TextBox_To_TextBox) demonstrates howto copy text from one TextBox DrawingObject into another text box. Thesecond Sub procedure Cell_Text_To_TextBox, demonstrates a method that youcan use to copy the value of a range of cells into a text box.
The following Sub procedure copies the text from one text box on the activesheet to another text box on the active sheet.
Sample Visual Basic Procedures
The following Sub procedure copies the values from a range of cells thatyou specify into a text box on the active sheet.
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
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
For additional information about getting help with Visual Basic forApplications, please see the following article in the Microsoft KnowledgeBase:
163435 VBA: Programming Resources for Visual Basic for Applications
5.00c "characters method" "chr function" "textbox control" textboxes "text boxes" XL
Article ID: 148815 - Last Review: 12/04/2015 14:24:43 - Revision: 3.2
Microsoft Excel 97 Standard Edition
- kbnosurvey kbarchive kbdtacode kbhowto KB148815