
Tips.Net > ExcelTips Home > Graphics > Placing Textbox Text Into a Worksheet
Summary: Want to get rid of your text boxes and move their text into the worksheet? It’s going to take a macro-based approach, discussed here. (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, Excel 2003, and Excel 2007.)
Excel allows you to place all sorts of graphic objects on your worksheet. One type of graphic object actually contains text—a textbox. If you have quite a few textboxes in a worksheet, you may be wondering if there is a way to extract the text from each textbox and place it in the worksheet itself.
There is no command to do this; you must instead use a macro. The following macro steps through each textbox in a worksheet and makes the desired extraction:
Sub ExtractText()
Dim shp As Shape
Dim sLoc As String
For Each shp In ActiveSheet.Shapes
With shp
If Left(.Name, 8) = "Text Box" Then
sLoc = .TopLeftCell.Address
Do Until Range(sLoc) = ""
sLoc = Range(sLoc).Offset(1, 0).Address
Loop
Range(sLoc) =.TextFrame.Characters.Text
.Delete
End If
End With
Next
End Sub
Since Excel stores all graphic shapes in the Shapes collection, you can step through the collection and make a determination as to which shapes you want to work with. In this case, the first eight characters of the shape's name is checked. Only if the name begins with "Text Box" does the macro consider the shape to be a text box from which text can be extracted.
Rather than check for the "Text Box" wording in the name, the macro could also check to see what type of shape is being considered. If you prefer to do this, then simply replace the test line (If Left...) with the following test line:
If shp.Type = msoTextBox Then
The sLoc variable is used to store the location of the textbox, which is contained in the .TopLeftCell property. A Do loop is then used to make sure that the cell pointed to by the address is empty. (This prevents any existing contents of the cell from being overwritten.) If it is not empty, then the address is "incremented" to the next cell in the column.
With the address of an empty cell determined, the text of the textbox is stored in the cell. The .Delete method is then used to get rid of the actual text box.
Tip #2388 applies to Microsoft Excel versions: 97 2000 2002 2003 2007
Step Up and Take Control! Subscribers to ExcelTips know just how valuable a resource it is. ExcelTips Premium provides twice the number of exceptional, easy-to-understand tips every week in an ad-free newsletter, as well as substantial discounts on ExcelTips archives and e-books.
Check out ExcelTips Premium today!
Thousands of ExcelTips, available for immediate download. Have all the Microsoft Excel info you need, right at your fingertips. (more information...)
Ask an Excel Question
Make a Comment
ExcelTips FAQ
ExcelTips Premium
Bugs and Pests Tips
ExcelTips
Family Tips
Health Tips
Home Tips
Organizing Tips
WordTips
Advertise on the
ExcelTips Site