For loop with image controls on worksheet

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
Hello all
I am issue looping through this code:
Code:
For i = 1 To 20 
Sheet1.Controls ("Image" & i).Picture = LoadPicture  ("")
Next i

And I get the error message :
Code:
Method or data member not found

What did I do wrongly?
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Are the objects on the sheet? If so:
Code:
Sheet1.OLEObjects("Image" & i).Object.Picture = LoadPicture("")
 
Upvote 0
Yesssss! ! Thanks for the help.

But I just got myself into a challenge:
I used this code to load the controls
Code:
Sheet1.Image1. Picture = LoadPicture (fPath & "\" & Sheet1. Range ("B2") & ".jpg")
Sheet1.Image2. Picture = LoadPicture (fPath & "\" & Sheet1. Range ("B12") & ".jpg")
Sheet1.Image3. Picture = LoadPicture (fPath & "\" & Sheet1. Range ("B22") & ".jpg")
........ to 20 controls

I wanted the error code to deal with only controls with the err 53 yet it happens to be clearing all controls. How do I get over this?
 
Last edited:
Upvote 0
Is it due to control or jpg not existing? You can use Dir() to determine if the file exists. e.g.
Code:
If Dir(fPath & "\" & Sheet1. Range ("B2") & ".jpg")) <> "" then _
 Sheet1.Image1. Picture = LoadPicture (fPath & "\" & Sheet1. Range ("B2") & ".jpg")

If you have alot of these, you might consider poking the filenames and or control names into array(s). Iterating arrays will then make code easier to maintain.
 
Last edited:
Upvote 0
Is it due to control or jpg not existing? You can use Dir() to determine if the file exists. e.g.
Code:
If Dir(fPath & "\" & Sheet1. Range ("B2") & ".jpg")) <> "" then _
 Sheet1.Image1. Picture = LoadPicture (fPath & "\" & Sheet1. Range ("B2") & ".jpg")

If you have alot of these, you might consider poking the filenames and or control names into array(s). Iterating arrays will then make code easier to maintain.

What is happening is that the range ("B2") may be blank and that will cause the err 53. So I want a way to check and apply the err 53 code to only the controls which have that error.

Oh okay but I have no idea about arrays. Can you assist me here:
Regards
 
Last edited:
Upvote 0
IF B2 is "blank" then Dir() will know that the filename does not exit and skip updating the pic.

Change and adapt to suit.
Code:
Sub Main()
  Dim i As Integer, fPath As String, a(1 To 2) As String
  
  fPath = Environ$("USERPROFILE") & "\Pictures\"
  
  'Sheet1.[B2] = "city"
  a(1) = fPath & Sheet1.[B2] & ".jpg"
  'Sheet1.[B12] = "water"
  a(2) = fPath & Sheet1.[B12] & ".jpg"
  
  For i = 1 To UBound(a)
    If Dir(a(i)) <> "" Then _
      Sheet1.OLEObjects("Image" & i).Object.Picture = LoadPicture(a(i))
  Next i
End Sub
 
Last edited:
Upvote 0
The code did not show the images.
Should I uncomment the Sheet1. [B2] = "city" etc?
Code:
  fPath = Environ$("USERPROFILE") & "\Pictures\"
What is the above line doing? I don't understand it. Thanks
Kelly
 
Last edited:
Upvote 0
a(1) holds the first filename's full path.
a(2) will hold the 2nd.
and so on.

In the Dim a(1 to 2), change 2 to however many elements you want to store. Assign the paths to each element as needed. The loop will never need to be changed.

fPath should be the path to your pics.

You can easily test the code as is in a new file's Sheet1 if you have the city.jpg and water.jpg files in your Pictures folder. Add two Image objects to Sheet1 and run the macro.
 
Upvote 0
Can I use this path:
Code:
fPath = ThisWorkbook.Path & "\" & "Pictures"
 
Upvote 0
Yes, it it exists. I like to concatenate the trailing backslash but that is your call.
 
Upvote 0

Forum statistics

Threads
1,214,644
Messages
6,120,709
Members
448,983
Latest member
Joaquim_Baptista

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