VBA delete freeform positioned over chartobject

fboehlandt

Active Member
Joined
Sep 9, 2008
Messages
334
Hello everyone,
I have a strange problem with which I am making no headway. I would like to delete a freeform from a particular spreadsheet. The code below does exactly that whilst retaining all other shape types:

Code:
Sub dshape()
Dim wb As Workbook
Dim ws As Worksheet
Set wb = ThisWorkbook
Set ws = wb.Sheets("Response_Q2")
Dim Sh As Shape
With ws
    For Each Sh In .Shapes
        If Sh.Type = msoFreeform Then: Sh.Delete
'       alternatively If Not (Sh.Type = msoChart Or Sh.Type = msoFormControl) Then: Sh.Delete
    Next Sh
End With
End Sub

However, if the freeform is positioned on top of a chart (i.e. the chart was selected, the freeform pasted into the chart area/plot area), the code does not recognize the freeform. I assume it is to do with the freeform now being regarded as 'part' of the chart? Is the chartobject rather than spreadsheet the parent object of the freeform? If so, how is the code above to be altered to accomodate such? I am really at a loss here and would greatly appreciate any help...
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hi, :)

Is the chartobject rather than spreadsheet the parent object of the freeform?

Yes. The chart also has "shapes" when it was activated while you insert the freeform.

Try it like this:

Code:
Option Explicit
Sub dshape()
    Dim wb As Workbook
    Dim ws As Worksheet
    Set wb = ThisWorkbook
    Set ws = wb.Sheets("Response_Q2")
    Dim Sh As Shape
    With ws
        For Each Sh In .Shapes
            If Sh.Type = msoChart Then
                If Sh.Chart.Shapes.Count > 0 Then
                    If Sh.Chart.Shapes.Item(1).Type = msoFreeform Then
                        Sh.Chart.Shapes.Item(1).Delete
                    End If
                End If
            ElseIf Sh.Type = msoFreeform Then
                Sh.Delete
            End If
    '       alternatively If Not (Sh.Type = msoChart Or Sh.Type = msoFormControl) Then: Sh.Delete
        Next Sh
    End With
End Sub
 
Upvote 0
Thank you!! This has kept me busy for hours. I just didn't know how to reference the freeform within the chart

Code:
Sh.Chart.Shapes.Item(1).Type

In addition, Excel 2007 does not allow for recording changes to shapes anymore. A seemingly simple problem that, unfortunately, cost me a lot of time. Anyway, thanks for your help again...
 
Upvote 0

Forum statistics

Threads
1,223,970
Messages
6,175,703
Members
452,667
Latest member
vanessavalentino83

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top