MrPink1986

Active Member
Joined
May 1, 2012
Messages
252
Hi,

I have a multi page user form that I am using to predict scores for the premier league soccer fixtures each week. Within each page I have 1 fixture made up of a home team and an away team. This data changes week on week depending on the a web import. I want to add two images to each page which will represent the team crest of the team in each text box.

I have the team crests on a tab named "Pictures" with each crest in a cell beginning with C2.
My text boxes are named Fixture1_Home Fixture1_Away - Fixture10_Home Fixture10_Away this will pick up the 20 teams each week.

In my image box I want some code to say if Fixture1_Home is X then pick up the picture and apply this to the image box (my image boxes are named Fix1_Home_Image, Fix1_Away_Image etc)

Once this is defined I am then presuming I can call the Sub UserForm_Initialize stage.

I have attempted the below for the Manchester United team as the away team in the fixture 9 test box.

Code:
Private Sub Fix9_Away_Image_Click()If Fixture9_Away_Change = "Manchester United" Then
Fix9_Away_Image.Picture = Sheets("Pictures").Range("C2")
End If
End Sub

Any help is greatly appreciated.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Re: Help importing images into userform

Here is a method to insert 2 images (from a folder not a worksheet) into 2 frames on each page in a multipage userform
If you create a simple 2 page userform and name everything exactly the same it will work and then you can tailor it to suit yourself

Instructions
- logos are saved in a single folder (make sure path to folder in code is amended accordingly)
- the logo file names are EXACTLY the same as the name of the teams as it they appear in a textbox
- tested with a 2 page form where each page contains 2 textboxes and 2 frames (for the logos)
- I used 2 jpegs "Manchester United.jpg" and Liverpool.jpg
- the names used in the textboxes "Manchester United" and "Liverpool"
- each page contains 2 frames and 2 textboxes
- the name of each frame is the name its textbox with "_Logo" added at the end
- Page 1 objects = Fixture1_Home_Logo (frame), Fixture1_Home (textbox), Fixture1_Away_Logo (frame), Fixture1_Away (textbox)
- Page 2 objects = Fixture2_Home_Logo (frame), Fixture2_Home (textbox), Fixture2_Away_Logo (frame), Fixture2_Away (textbox)

This is how the code works - using one of the frames as an example
The code loops all pages and all controls
If the control is a frame then VBA uses the name of the frame (Fixture1_Home_Logo)
Textbox name (Fixture1_Home) is obtained by removing _Logo from the name of the frame
Team name is obtained with Fixture1_Home.Text
The image is inserted with UserForm1.Controls.Item(Fixture1_Home_Logo).Picture = LoadPicture(picPath & team & ".jpg")

Code:
Private Sub UserForm_Initialize()
    Dim picPath As String, c As Control, r As Integer, team As String
    picPath = "C:\Test\Logos\"            'end path with \
    On Error Resume Next
    For r = 0 To Me.MultiPage1.Pages.Count - 1
        For Each c In Me.MultiPage1.Pages(r).Controls
            If TypeName(c) = "Frame" Then
                    team = UserForm1.Controls(Replace(c.Name, "_Logo", "")).Text
                    UserForm1.Controls.Item(c.Name).Picture = LoadPicture(picPath & team & ".jpg")
            End If
        Next c
    Next r
End Sub
 
Upvote 0
Re: Help importing images into userform

Thanks I will try this later today however the approach will not work as a permanent fix as some of the users will not have access to any folders I have and where the pictures will reside.
 
Upvote 0
Re: Help importing images into userform

The "simple" method is to insert the pictures into Image controls embedded on the worksheet. Then you can just assign the Picture property of one to the Picture property of the relevant control on the form. This may lead to a larger file though.

If they are inserted as regular pictures, you need either API code or to copy and paste them to a blank chart so that you can export that at run time as an image and then load from disk into the Image control (as above)
 
Upvote 0
Re: Help importing images into userform

As the text box data will change all the time the corresponding image will need to be updated to the correct image. Each image will be on the sheet "Pictures" and will not change cell (or even named range) - within the image control is there a way to contstruct a statement that essentially is a If statement for my 20 teams and once it finds the image corresponding to the data in the text box exit the sub.
I dont want to embed the picture down as once the fixtures update the image will not be correct - i hope that makes sense.
 
Upvote 0
Re: Help importing images into userform

Your question does make sense but suggest that you may not have had time to test my code
Every time the form is initialized the images reflect the "latest" values in the text box
The image is only "embedded" as long as the UserForm is open
 
Upvote 0
Re: Help importing images into userform

OK thanks for the replies - I dont follow the steps outlined for this to work if I am honest. I inserted my image box - I double click it to enter the code - should the parameter in the top right drop down be a click? Do I need to enter code here? In the initial step I want to call this upon opening the userform. Please do let me know how I may make this easier fore you to explain to me.
Your question does make sense but suggest that you may not have had time to test my code
Every time the form is initialized the images reflect the "latest" values in the text box
The image is only "embedded" as long as the UserForm is open
 
Upvote 0
Re: Help importing images into userform

I think I do get it now - my question is in my image how can I reference the image from a cell reference? I seem to only have the option to synch from a file when I use the picture Icon.
OK thanks for the replies - I dont follow the steps outlined for this to work if I am honest. I inserted my image box - I double click it to enter the code - should the parameter in the top right drop down be a click? Do I need to enter code here? In the initial step I want to call this upon opening the userform. Please do let me know how I may make this easier fore you to explain to me.
 
Upvote 0
Re: Help importing images into userform

Forget about cell references for the moment

You wanted to load 2 images into each page of your userform based on the value of the text in the textbox (ie the team name)
- I provided you with a method to achieve that

The only difference is that instead of having 20 logos on the worksheet, the logos are jpegs and are loaded from a folder on your hard drive

What is it that the code is not doing that it should be doing?
 
Upvote 0
Re: Help importing images into userform

Ok this does work as described for ME, thank you. - this is something I will be using with people who will not have access to the drive that I have saved the photos to. Is there any options other than having the pictures in a folder on a directory? Can a web reference be used if a cell reference cannot be used as the source?
Forget about cell references for the moment

You wanted to load 2 images into each page of your userform based on the value of the text in the textbox (ie the team name)
- I provided you with a method to achieve that

The only difference is that instead of having 20 logos on the worksheet, the logos are jpegs and are loaded from a folder on your hard drive

What is it that the code is not doing that it should be doing?
 
Upvote 0

Forum statistics

Threads
1,217,052
Messages
6,134,302
Members
449,864
Latest member
daffyduck1970

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