Insert Picture from Speradsheet into Userform Image VBA

daveycrock

New Member
Joined
Dec 5, 2014
Messages
2
Hello All!

I am trying to create a blackjack game and have all the pictures of the 52 cards in a deck in a column (1 card per cell). I am trying to insert a certain cell into the image "box" on the userform using VBA code, depending on what the random number comes up as. For example, if the random number = 3, I want the picture in cell A3 (lets say the 3 of hearts) to populate Userform1.Image1 (the image "box" on the userform). How can I do this? Any help would be GREATLY appreciated!!! (I've been stuck for quite a bit now :confused:). I feel like there is an easy way to do this, I just can't figure it out. Thanks everyone!

The reason I can't use the filepath (C:/...) is that this application must be transferable (so I can email the excel file to other people). That is why I'm trying to put each image in a cell on the worksheet and reference the images that way.
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Hi
Welcome to the board

It's possible to copy the image in a picture to an image control, but it's complex and it involves using the windows API.

A much easier solution is instead of inserting pictures in the worksheet use instead image controls.

This way you just copy directly the picture inside.

You can name the image controls in the worksheet with something like "CardXX" and you won't have to bother on which cell(s) the control is.

To copy one of the images in the worksheet to the image control in the userform, assuming its name is ImageCard, it's just:


Code:
ImageCard.Picture = Worksheets("Sheet1").OLEObjects("Card03").Object.Picture
 
Upvote 0
Thanks for your reply pgc01! No wonder I was struggling - using the windows API isn't exactly up my alley. :) What I ended up doing (if anyone else encounters this issue or is curious) is building the blackjack game directly in Excel without using a userform. This way, we can just copy and paste cells, rather than trying to copy and paste a cell into a image "box." So far, this has been working, but the project isn't quite complete. Thanks again!
 
Upvote 0
I tried the following

Code:
ImageCard.Picture = Worksheets("Sheet1").OLEObjects("Card03").Object.Picture

Unfortunately I am receiving a

Run-time error '1004': unable to get the OLEObjects property of the Worksheet class.

Am I missing something?
 
Upvote 0
Hi
Welcome to the board

Did you insert an image control in the worksheet (not a picture object) like I wrote in post #2?
 
Upvote 0
Hello,

Some users opening the Excel file (Stored in a SharePoint doc lib) have ran into a RunTime error (38209) when the spreadsheet call the images using the OLE inserted pictures. Any knowledge on why this may be happening?

Thanks

M
 
Upvote 0

Forum statistics

Threads
1,223,922
Messages
6,175,386
Members
452,639
Latest member
RMH2024

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