Solved

Delete text boxes that are located on charts

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Macro to change number to Month in Excel? 10 46
Date Formatting on Userform Print 5 27
Need help with Clear Macro 4 26
COPYING ROW W/ CHECKBOX TO SEPARATE SHEET 3 25
A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

837 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