Solved

Delete text boxes that are located on charts

Posted on 2011-09-18
3
284 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need to combine three formulas into one 5 38
Increment default InPutBox value 14 26
I NEED KEEP THE CONDITIONAL  FORMATTING 24 28
Help to find the duplicates 8 19
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

749 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