Error Trying To Remove Shapes

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,616
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Is anyone able to shed some light on why I might be getting an "Application-defined or object defined error with the line highlighted in red in the code below?
The odd thing is this had worked flawlessly, but today, it's causing grief. I don't recall changing anything that might have led to this.

Rich (BB code):
Private Sub Workbook_Open()
    'Stop
    ini1
    
    svcCnt = 0
    svcRid = 0
    mbevents = True
    
    'Stop
    With ws_front
        .Activate
        ActiveWindow.ScrollRow = 1
        ActiveWindow.ScrollColumn = 1
        .Unprotect
        With ws_front.Range("J7:BM2206")
            .Clear
            For Each shp In .Parent.Shapes
                If Not Intersect(shp.TopLeftCell, .Cells) Is Nothing Then shp.Delete
            Next shp
        End With
        mbevents = False
        .Range("A1") = "Enter Date"
        .Range("A2") = Format(0, "00000") 'date serial
        .Range("D2") = Format(0, "000") 'record
        .Range("E2:F2").Locked = True
        .Range("E2") = svcRid
        .Range("G2") = svcCnt
        .Range("A3:A5") = ""
        .Range("A19") = "" 'sunset offset
        .Pictures("hidden1").Visible = False
        .Pictures("hidden2").Visible = False
        .Pictures("hidden3").Visible = False
        .Range("P3,R3,Y2,AA2,AG2,AI2,Y3,AA3,Y4,AA4, AG3,AI3") = 0
        ws_staff.Range("D4:R33").Clear
        ws_lists.Range("D2:D101").Clear
        
        mbevents = True
        .Protect
    End With
    
End Sub

When I hover over the line, the error seems to be stemming from "shp.TopLeftCell"
Alias ws_front is recognized as being the proper worksheet.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
It turns out that some cells in that range had validations associated with them. I had to delete validations from that range to get it to work again.
Not sure why that would affect the code's ability to identify shapes.
 
Upvote 0
I assume that you're using a List for your data validation, correct? In this case, when .Clear removes the validation within the target range, the drop down object associated with it remains part of the Shapes collection. And so you'll need to account for it.

VBA Code:
        With ws_front.Range("J7:BM2206")
            .Clear
            For Each shp In .Parent.Shapes
                If shp.Type = msoFormControl Then
                    shp.Delete
                Else
                    If Not Intersect(shp.TopLeftCell, .Cells) Is Nothing Then shp.Delete
                End If
            Next shp
        End With

However, deleting the form control will delete any and all other data validation of the same type located outside the target range. So, in this case, to avoid deleting them, you could loop through each shape and make sure it's not a msoFormControl before deleting it.

VBA Code:
        With ws_front.Range("J7:BM2206")
            .Clear
            For Each shp In .Parent.Shapes
                If shp.Type <> msoFormControl Then
                    If Not Intersect(shp.TopLeftCell, .Cells) Is Nothing Then shp.Delete
                End If
            Next shp
        End With

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,150
Members
453,021
Latest member
Justyna P

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