simpler VBA code with a loop

akashawalker

New Member
Joined
May 2, 2013
Messages
16
Hi to all
I have a piece of code that is working, but i would like to make it simpler by using a loop.
could someone assist me?

bellow is my code:

'%%%%%%%%%%% DELETE previous pictures from the worksheets%%%%%%%%%%%%%%%%

'Approval drawing 1
Dim myObj1
Dim Pictur
Set myObj1 = Worksheets("Approval Drawing 1").DrawingObjects
For Each Pictur In myObj1
If Left(Pictur.Name, 7) = "Picture" Then
Pictur.Select
Pictur.Delete
End If
Next

'Approval drawing 2
Dim myObj2
Set myObj2 = Worksheets("Approval Drawing 2").DrawingObjects
For Each Pictur In myObj2
If Left(Pictur.Name, 7) = "Picture" Then
Pictur.Select
Pictur.Delete
End If
Next

'Approval drawing 3
Dim myObj3
Set myObj3 = Worksheets("Approval Drawing 3").DrawingObjects
For Each Pictur In myObj3
If Left(Pictur.Name, 7) = "Picture" Then
Pictur.Select
Pictur.Delete
End If
Next



Thanks for your time
Akasha Walker
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Untested but is this what you want
Code:
Sub DelPic()
'Approval drawing 1
    Dim myObj
    Dim Pictur
    Dim Cnt As Long
    
        For Cnt = 1 To 3
            Set myObj1 = Worksheets("Approval Drawing " & Cnt).DrawingObjects
            For Each Pictur In myObj
                If Left(Pictur.Name, 7) = "Picture" Then
                    Pictur.Select
                    Pictur.Delete
                End If
            Next Pictur
        Next Cnt

End Sub
 
Upvote 0
Thank you Fluff
it worked like a charm.

one more question, if i want to use the same approach for bellow how could i make the Dim variables?


approvaldrawing1 = Left(Range("B5").Value, 5)
approvaldrawing2 = Left(Range("F5").Value, 5)
approvaldrawing3 = Left(Range("J5").Value, 5)
approvaldrawing4 = Left(Range("N5").Value, 5)
 
Upvote 0
What information will they hold, Numbers, text?
 
Upvote 0
In that case
Code:
Dim approvaldrawing1 As Long
 
Upvote 0

Forum statistics

Threads
1,215,532
Messages
6,125,358
Members
449,221
Latest member
chriscavsib

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