Solved

Delete text boxes that are located on charts

Posted on 2011-09-18
3
281 Views
Last Modified: 2012-05-12
Dear Experts:

Below macro places a text box (contents are named ranges) into each and every chart of the active worksheet.

The macro runs just fine.

There is one problem: I got no idea how to DELETE ONLY these text boxes that were created by running below macro and leave all other possible text boxes (data labels) or ordinary' text boxes located outside the chart alone.

I have attached a sample file with below code integrated for your convenience.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas

 SampleFileTextBoxes.xlsm
Sub TextBoxes_Add_ToCharts()

'Add text box
'You can assign the result of the AddTextbox method to a Shape object variable and manipulate its Top, Left,
'Height and Width properties 
'1. Value = Left property
'2. Value = Top property
'3. Value = Width property
'4. Value = Height property


Dim chtTemp As Chart
Dim objTemp As Object
Dim x As Integer

x = 1
Do While x <= ActiveSheet.ChartObjects.Count
Set chtTemp = ActiveSheet.ChartObjects(x).Chart
Set objTemp = chtTemp.Shapes.AddTextBox(msoTextOrientationHorizontal, 285, 171, 135, 20)

objTemp.Select
Selection.Formula = "='" & chtTemp.Parent.Parent.Name & "'!Blue_Range_TextBox_" & Format(x, "00")
x = x + 1
Loop

ActiveCell.Select

End Sub

Open in new window

0
Comment
Question by:AndreasHermle
  • 2
3 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 36556717
I modified your original procedure to add a fixed name to those textboxes, which then allows us to identify them for a sub that would try to delete them (also supplied here):


Sub TextBoxes_Add_ToCharts()

'Add text box
'You can assign the result of the AddTextbox method to a Shape object variable and manipulate its Top, Left,
'Height and Width properties (relative to those properties for myChtObj).
'myChtObj.Chart.Shapes.AddTextbox(msoTextOrientationHorizontal, 30.75, 11.25, 57.75, 21#)
'1. Value = Left property
'2. Value = Top property
'3. Value = Width property
'4. Value = Height property


Dim chtTemp As Chart
Dim objTemp As Object
Dim x As Integer

x = 1
Do While x <= ActiveSheet.ChartObjects.Count
Set chtTemp = ActiveSheet.ChartObjects(x).Chart
Set objTemp = chtTemp.Shapes.AddTextBox(msoTextOrientationHorizontal, 285, 171, 135, 20)
With objTemp
    .Select
    Selection.Formula = "='" & chtTemp.Parent.Parent.Name & "'!Blue_Range_TextBox_" & Format(x, "00")
    .Name = "TextBoxes_Add_ToCharts"
End With
x = x + 1
Loop

ActiveCell.Select

End Sub


Sub KillTextBoxes()
    
    Dim cht As ChartObject
    Dim shap As Shape
    Dim Counter As Long
    
    For Each cht In ActiveSheet.ChartObjects
        For Each shap In cht.Chart.Shapes
            If shap.Name = "TextBoxes_Add_ToCharts" Then
                shap.Delete
                Counter = Counter + 1
            End If
        Next
    Next
    
    If Counter Then
        msgbox "I killed " & Counter & " TextBoxes", vbInformation, "Yahtzee!"
    Else
        msgbox "No matching TextBoxes found", vbExclamation, "Wasting my time"
    End If
    
End Sub

Open in new window

0
 

Author Closing Comment

by:AndreasHermle
ID: 36556898
Hi Patrick,

you know that you are one of the best here, don't you!

Great job. Exactly what I wanted. Thank you very much for your professional help. I really, really appreciate it. This forum is just great.

Regards, Andreas
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 36557012
Glad to help, Andreas!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now