Set range according to active sheet name VBA

lengyel109

New Member
Joined
Jan 10, 2022
Messages
4
Office Version
  1. 365
  2. 2021
  3. 2019
Platform
  1. Windows
Hello there!

I ran into some issues using VBA with my limited knowledge.
I am trying to combine a code to delete pictures in a range, which works as it should:
VBA Code:
Dim pic As Object
For Each pic In ActiveSheet.Pictures
    If Not Application.Intersect(pic.TopLeftCell, Range("A8:T35")) Is Nothing Then
        pic.Delete
    End If
Next pic
End Sub
With an If statement to set the range, if a specific sheet is active:
Rich (BB code):
Dim pic As Object
Dim lc As String
If ActiveSheet.Name = "ESC 1-4" Then lc = (G35)
For Each pic In ActiveSheet.Pictures
    If Not Application.Intersect(pic.TopLeftCell, Range("A8:lc")) Is Nothing Then
        pic.Delete
    End If    
Next pic
End Sub
The yellow part is where I get a run time error '1004' Method 'Range of object'_Global' failed.
So I guess I messed the string somehow right?
This would ofc repeat with different sheet names, with each sheet having a different end range.
Any ideas?

Thanks in Advance, Mark
 
Last edited by a moderator:

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Ye, (G35) should be "G35" and "A8:lc" should be "A8:" & lc.

Try replacing
VBA Code:
'If ActiveSheet.Name = "ESC 1-4" Then lc = (G35)
'For Each pic In ActiveSheet.Pictures
' If Not Application.Intersect(pic.TopLeftCell, Range("A8:lc")) Is Nothing Then

With
VBA Code:
If ActiveSheet.Name = "ESC 1-4" Then
    lc = "G35"
End If
For Each pic In ActiveSheet.Pictures
If Not Application.Intersect(pic.TopLeftCell, Range("A8:" & lc)) Is Nothing Then

To add more ranges depending on sheet name, expand the first If with Else Ifs:
VBA Code:
If ActiveSheet.Name = "ESC 1-4" Then 
    lc = "G35"
Else If ActiveSheet.Name = "ESC 5-8" Then
    lc = "H35"
Else If ActiveSheet.Name = "And so on" Then
    lc = "T35"
End If

For Each pic In ActiveSheet.Pictures
If Not Application.Intersect(pic.TopLeftCell, Range("A8:" & lc)) Is Nothing Then
 
Last edited:
Upvote 0
Solution
Ye, (G35) should be "G35" and "A8:lc" should be "A8:" & lc.

Try replacing
VBA Code:
'If ActiveSheet.Name = "ESC 1-4" Then lc = (G35)
'For Each pic In ActiveSheet.Pictures
' If Not Application.Intersect(pic.TopLeftCell, Range("A8:lc")) Is Nothing Then

With
VBA Code:
If ActiveSheet.Name = "ESC 1-4" Then
    lc = "G35"
End If
For Each pic In ActiveSheet.Pictures
If Not Application.Intersect(pic.TopLeftCell, Range("A8:" & lc)) Is Nothing Then

To add more ranges depending on sheet name, expand the first If with Else Ifs:
VBA Code:
If ActiveSheet.Name = "ESC 1-4" Then
    lc = "G35"
Else If ActiveSheet.Name = "ESC 5-8" Then
    lc = "H35"
Else If ActiveSheet.Name = "And so on" Then
    lc = "T35"
End If

For Each pic In ActiveSheet.Pictures
If Not Application.Intersect(pic.TopLeftCell, Range("A8:" & lc)) Is Nothing Then
Oh yeah, that worked beautifully! Thanks for the help!
 
Upvote 0

Forum statistics

Threads
1,213,543
Messages
6,114,236
Members
448,555
Latest member
RobertJones1986

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