Importing images into userform

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.
 
Re: Help importing images into userform

So you opened the Userform and clicked the CommandButton. The Club names appeared but No Pictures, is that right ???
Are there Logos (picture) in sheet2, and have those pictures got the same names as the names in sheet1 column1??

NB:- There should only be pictures on pages1 to 5
 
Last edited:
Upvote 0

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Re: Help importing images into userform

@MickG
I downloaded your file with same result

This may help you resolve :)
I forced code to stop running after file exported (see below)
- I looked at temp.jpg - it is an empty picture
- also the chart that is in Sheet1 is empty
- so the code is not copying the image from sheet2 into the chart

Code:
Sub Pict(n, pic)
rest of code
      [COLOR=#ff0000]cht.Chart.Export Strpath[/COLOR]
      [COLOR=#000080]End[/COLOR]
rest of code
 
Upvote 0
Re: Help importing images into userform

Yongle
Thanks for the effort, If you step through this basic code in the file below , you could see what's happening and if the chart/Image1 control is being filled.

NB:- if it does not work perhaps you could try the code and another image in a new Workbook, see what happens

https://app.box.com/s/xb7b374uoyiwup6r8kn8wqvm24ljxdgw
 
Last edited:
Upvote 0
Re: Help importing images into userform

So you opened the Userform and clicked the CommandButton. The Club names appeared but No Pictures, is that right ???
Are there Logos (picture) in sheet2, and have those pictures got the same names as the names in sheet1 column1??
NB:- There should only be pictures on pages1 to 5

Yes, I did all this but still can not see the pictures in userform.
I tried in another file but still can not see those images in userform.

We did not get the file in post #23 either. At the push of the button nothing happens.
I ran the code manually with F8 and so it went.
What else could I try?
 
Last edited:
Upvote 0
Re: Help importing images into userform

@MrPink1986


How about zipping the workbook AND the jpegs for distribution
- solves the issue with solution per post#2
 
Last edited:
Upvote 0
Re: Help importing images into userform

@MrPink1986
Try this



This is a modification of what I posted previously
- procedure added which saves jpegs of the images (in the same folder as the workbook)
- original procedure amended to match new jpeg location

Sheet "Logos"
A1:A20 - the names of each team EXACTLY as they appear in textboxes
B1:B20 - the logos - ensure that column B cells are the correct size to fit userform frame




Code:
Private Sub UserForm_Initialize()
    Dim c As Control, r As Integer, team As String, picPath As String
    picPath = ThisWorkbook.Path & "\"
    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

Code:
Sub CreateJpegs()
    Dim Ws As Worksheet, Logo As Range, Chrt As ChartObject
    Set Ws = Sheets("Logos")
    For Each Logo In Ws.Range("B1:B20")
        Logo.CopyPicture xlScreen, xlPicture
        Set Chrt = Ws.ChartObjects.Add(0, 0, Logo.Width, Logo.Height)
        Chrt.Activate
            With Chrt.Chart
                .Paste
                .Export ThisWorkbook.Path & "\" & Logo.Offset(, -1) & ".jpg"
            End With
        Chrt.Delete
    Next Logo
End Sub
 
Upvote 0
Re: Help importing images into userform

@MickG - puzzle with your code resolved :)
- the chart must be activated before pasting the image
- I should have tried that earlier :banghead:

Code:
   Set cht = ActiveSheet.ChartObjects.Add(100, 0, pic.Width, pic.Height)
   [COLOR=#ff0000]cht.Activate[/COLOR]
   cht.Chart.Paste
 
Upvote 0
Re: Help importing images into userform

Yongle, Good Catch !!

I don't deal with Charts much, but I had noticed some of "Walkenbach" codes emphasised the need for "Chart.Activate".
Strange that it did not effect my code !!

I'll make a note !!
 
Upvote 0
Re: Help importing images into userform

Yongle - I was on holidays for the past week and a half - I will test this code in the next day or so and get back to you with my results.
Once quick one is it possible to have the images and team names on a tab specifically with this information?
 
Upvote 0
Re: Help importing images into userform

Once quick one is it possible to have the images and team names on a tab specifically with this information?
I do not understand what you want :confused:
- are you asking if it is possible to have ALL the logos & team names on the same tab?
- are you referring to the userform or the worksheet
 
Upvote 0

Forum statistics

Threads
1,223,905
Messages
6,175,297
Members
452,633
Latest member
DougMo

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