Hi,
I am a new analyst working on a Macro-enabled worksheet. I ran into some rather popular run-time error while trying to run my macros (i.e., create, delete) multiple times on the same worksheet. I research on the internet and found that the error occurred because my object/variable has not been released every time I re-run the macro. The quick workaround is that I save my worksheet every time I make a change before I run the macros; however, there seems to be a better solution that would get rid of this problem entirely by changing the code to "re-define" my objects every I run the macros. Since I am pretty new to VBA, I am seeking some help with the below lines of code:
Sub Delete_Icons()
' INTERSECT COMMAND DETERMINES IF A SHAPE EXISTS WITHIN A SPECIFIED RANGE.
' IF THERE IS A SHAPE WITHIN ROWS 3 AND 1000 THEN IT WILL BE DELETED.
' THIS DOES NOT DELETE THE MACRO BUTTON IN ROWS 1 AND 2.
<code style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; white-space: inherit; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">Dim shp As Shape
For Each shp In ActiveSheet.Shapes
' this below line caused run-time error 1004
If Not Intersect(shp.TopLeftCell, Range("A3:F1000")) Is Nothing Then
shp.Delete
End If
Next shp
</code>Any help to fix the run-time error in this line of code would be appreciated. Thanks.
Regards,
Ross
I am a new analyst working on a Macro-enabled worksheet. I ran into some rather popular run-time error while trying to run my macros (i.e., create, delete) multiple times on the same worksheet. I research on the internet and found that the error occurred because my object/variable has not been released every time I re-run the macro. The quick workaround is that I save my worksheet every time I make a change before I run the macros; however, there seems to be a better solution that would get rid of this problem entirely by changing the code to "re-define" my objects every I run the macros. Since I am pretty new to VBA, I am seeking some help with the below lines of code:
Sub Delete_Icons()
' INTERSECT COMMAND DETERMINES IF A SHAPE EXISTS WITHIN A SPECIFIED RANGE.
' IF THERE IS A SHAPE WITHIN ROWS 3 AND 1000 THEN IT WILL BE DELETED.
' THIS DOES NOT DELETE THE MACRO BUTTON IN ROWS 1 AND 2.
<code style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; white-space: inherit; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">Dim shp As Shape
For Each shp In ActiveSheet.Shapes
' this below line caused run-time error 1004
If Not Intersect(shp.TopLeftCell, Range("A3:F1000")) Is Nothing Then
shp.Delete
End If
Next shp
</code>Any help to fix the run-time error in this line of code would be appreciated. Thanks.
Regards,
Ross