PDA

View Full Version : Click Listbox how to show more picture ?



ms06f
01-18-2012, 08:36 PM
I try to modified (Listbox-Show Picture on Click-Select Data on Double-Click excel vba file) .
I add myname2 , myname3 range at column I & J
In userform add image2 and image3
list_box_picture.xls vba how to modifity?
Help me please....


Private Sub listBox1_Click()

Dim EmpFound As Range
Dim EmpFound2 As Range
Dim fPath As String

With Range("myName")
Set EmpFound = .Find(ListBox1.Value)
On Error Resume Next
If EmpFound Is Nothing Then
Image1.Picture = LoadPicture(fPath & "nopic.gif")
Else
With EmpFound
fPath = ThisWorkbook.Path & "\"
On Error Resume Next
Image1.Picture = LoadPicture(fPath & "\" & ListBox1.Value & ".jpg")
If Err = 0 Then Exit Sub
Image1.Picture = LoadPicture(fPath & "nopic.gif")
End With
End If
End With
Set EmpFound = Nothing

With Range("myName2")
Set EmpFound2 = .Find(ListBox1.Value)
On Error Resume Next
If EmpFound Is Nothing Then
Image2.Picture = LoadPicture(fPath & "nopic.gif")
Else
With EmpFound2
fPath = ThisWorkbook.Path & "\"
On Error Resume Next
Image2.Picture = LoadPicture(fPath & "\" & ListBox1.Value & ".jpg")
If Err = 0 Then Exit Sub
Image2.Picture = LoadPicture(fPath & "nopic.gif")
End With
End If
End With
Set EmpFound = Nothing


End Sub

mdmackillop
01-19-2012, 06:20 AM
In userform add image2 and image3

In your code you have Image1 & Image2

ms06f
01-19-2012, 11:27 PM
try attach files.

Kenneth Hobs
01-20-2012, 08:59 AM
You don't have a Photo 1 for Image1 in the worksheet. If you did more than 3, you might want to use a separate Sub rather than many If/Then/Else/EndIF to load the pictures. Avoid using On Error when you can.

Private Sub ListBox1_Click()
Dim r As Range, i As Long, fPath As String, fn As String

fPath = ThisWorkbook.Path & "\"
i = ListBox1.ListIndex + 1
Set r = Range(ListBox1.RowSource)

fn = fPath & r.Cells(i, "I").Value & ".jpg" 'Filename path to Photo 2
If Dir(fn) <> "" Then
Image2.Picture = LoadPicture(fn)
Else
Image2.Picture = LoadPicture(fPath & "nopic.gif")
End If

fn = fPath & r.Cells(i, "J").Value & ".jpg" 'Filename path to Photo 2
If Dir(fn) <> "" Then
Image3.Picture = LoadPicture(fn)
Else
Image3.Picture = LoadPicture(fPath & "nopic.gif")
End If

TextBox1.Text = ListBox1.Column(1)
TextBox2.Text = ListBox1.Column(2)
TextBox3.Text = ListBox1.Column(3)
TextBox4.Text = ListBox1.Column(5)
End Sub

ms06f
01-20-2012, 10:27 AM
Thank you very much for your help!

ms06f
01-22-2012, 09:32 AM
Why If click the image1 or 2 or 3 than the picture are lock ?

Kenneth Hobs
01-22-2012, 10:39 AM
I don't understand what you mean. You have no click event set so clicking an image control would not do anything. What did you expect to happen?

ms06f
01-22-2012, 07:39 PM
I test it at two difference computers, one is offiice2007 and otherone is office2010.

When I click the listbox, then image1 2 3 can display all pictures.
If I click the listbox other row, 3 images can update.

For example, at this moment to click image2 one time.
Next' click listbox another row, image1 and image3 can update but image2 picture unchange.

Kenneth Hobs
01-22-2012, 09:24 PM
It works for me in Vista64home and Excel 2010.

IF your files don't exist then your Else file is shown.

You may want to add code to make it show the first row images when it starts.