You are here:

Excel/removing specific text boxes

Advertisement


Question
QUESTION: We have an older spreadsheet that has thousands of un-used text boxes. Is there a way to delete multiple empty text boxes at once? There are some that we use and many that are just 'out there', in order to clean up the file I'd like to only delete the blanks...

ANSWER: You use a macro for this.  For example, this one will look on the active sheet for all the shapes that are named as "Text Box" and delete the ones without any character in it.

Sub DeleteTextBoxes()
   Dim myTextBox As Shape
   
   For Each myTextBox In ActiveSheet.Shapes
       If Left(myTextBox.Name, 8) = "Text Box" Then
         If myTextBox.TextFrame.Characters.Count = 0 Then
         myTextBox.Delete
         End If
       End If
   Next
End Sub

I strongly recommend to try it first on a backup copy.

Hope this helps,
Miguel

---------- FOLLOW-UP ----------

QUESTION: This works great when there are less than 5-8k empty textboxes on a tab, but we have a few that have literally 38-40k on a single tab...and when the macro trys to work, it freezes up and I have to shut down excel...it says(Not Responding)any ideas for this type of situation?

Answer
Ok, we may try to simplify one of the IF statements with a different comparison, using the ShapeType instead of the name:

Sub DeleteTextBoxes()
  Dim myTextBox As Shape
  
  For Each myTextBox In ActiveSheet.Shapes
      If myTextBox.AutoShapeType = 1 Then
         If myTextBox.TextFrame.Characters.Count = 0 Then
         Debug.Print myTextBox.AutoShapeType
         End If
      End If
  Next
End Sub

Additionally, for the tabs with a huge amount of text boxes, you can use a For...Next loop instead of the For Each one, tweaking it to get only a fraction of the shapes.  The following sample takes the 10th part of the shapes for the checks, allowing you to save the progress between runs.  You can change that fraction at will.

Sub DeleteTextBoxesFor()
   For i = 1 To (ActiveSheet.Shapes.Count / 10)
       With ActiveSheet.Shapes(i)
         If .AutoShapeType = 1 Then
         If .TextFrame.Characters.Count = 0 Then
         Debug.Print .AutoShapeType
         End If
         End If
       End With
   Next
End Sub

Hope this helps,
Miguel
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Miguel Zapico

Expertise

I am recycling to Excel 2010, so I am taking an extended leave while I work on keeping myself an expert on this matter.

Experience

I have worked with Excel for the past 12 years, in various environments.

Organizations
NYPC (New York PC users group)

Organizations
NYPC (New York PC users group)

Education/Credentials
MCSE in Windows NT

©2012 About.com, a part of The New York Times Company. All rights reserved.