VBA: Macro to delete range runs normally, gives error 1004 if I click on a cell with data validation

RichardHell

New Member
Joined
Apr 20, 2021
Messages
5
Office Version
  1. 365
Platform
  1. Windows
This is my first post here, so sorry if I made any mistakes (this forum has helped me a lot before tho, thanks!).

I'm trying to make a macro that will delete everything within a range of cells, but I'm running into a weird issue.

There's a couple of checkboxes and a picture that don't get deleted with the .Delete method, so I include a loop that goes through every shape and deletes it if it's inside the range. This all works perfectly fine, until I click on one of the cells that has data validation. Here's the code:

VBA Code:
Dim sh As shape
Dim ws As Worksheet
Dim rangeval As range

Set ws = Worksheets("Historial")
Set rangeval = ws.range("A1:K53")

For Each sh In ws.shapes
    If Not Intersect(sh.TopLeftCell, rangeval) Is Nothing Then
        sh.Delete
    End If
Next

rangeval.Delete (xlShiftUp)

Throughout the range I've got some cells that pull data from a list located in a different sheet, using data validation, like this:

1618937816393.png


The problem is when I click on any of these cells. I don't even have to edit the content, or do anything. As soon as I click on one of them, even if I click away, it gives me an error.

1618938223569.png


When I Debug them problem seems to be in this line:

1618937997107.png

If I check the range that I'm trying to delete after running the macro it does seem to delete every shape, but gets stuck in (maybe?) the final iteration of the loop. Is it trying to pull something from the sheet that has the List values? I'm out of ideas honestly, any help will be appreciated.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hi & welcome to MrExcel.
A brute force approach
VBA Code:
Dim sh As Shape
Dim ws As Worksheet
Dim rangeval As Range, Tlc As Range

Set ws = Worksheets("Historial")
Set rangeval = ws.Range("A1:K53")

For Each sh In ws.Shapes
   Set Tlc = Nothing
   On Error Resume Next
   Set Tlc = sh.TopLeftCell
   On Error GoTo 0
   If Not Tlc Is Nothing Then
      If Not Intersect(sh.TopLeftCell, rangeval) Is Nothing Then
          sh.Delete
      End If
   End If
Next

rangeval.Delete (xlShiftUp)
 
Upvote 0
Solution
Thank you so much, this worked! I'm kind of a VBA noob, so if you don't mind, could you mind telling me what it does? Is it just telling excel to ignore any errors that come up?
 
Upvote 0
Is it just telling excel to ignore any errors that come up?
Pretty much. It tries to set the range variable Tlc to the Top Left Corner of the shape & if it can't then Tlc will be nothing so it checks for that
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
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