Display Image Based on Lookup Value

mcmahobt

Board Regular
Joined
Sep 2, 2014
Messages
55
Hello all,

I am relatively new to VBA, and hope this is an easy question for you experts out there to address. I am currently using the code supplied at McGimpsey & Associates : Excel : Display picture based on cell value to display a picture based on a cell value. The code is shown below:

Code:
Private Sub Worksheet_Calculate() 
Dim oPic As Picture Me.Pictures.Visible = False 
With Range("F1") 
For Each oPic In Me.Pictures 
If oPic.Name = .Text Then 
oPic.Visible = True 
oPic.Top = .Top 
oPic.Left = .Left 
Exit For 
End If 
Next oPic 
End With 
End Sub

I am wondering what the best way (if possible) of having this code be applied to a range of cells, say F1:F4, would be? I tried simply changing the F1 in the code to the range F1:F4, but it did not seem to work. Any help is appreciated!
 
Last edited:

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Hi,

Try this:

Code:
Private Sub Worksheet_Calculate()
    Dim oPic As Picture
    Dim rCell As Range
    Me.Pictures.Visible = False
    For Each rCell In Range("F1:F4")
        With rCell
            For Each oPic In Me.Pictures
                If oPic.Name = .Text Then
                    oPic.Visible = True
                    oPic.Top = .Top
                    oPic.Left = .Left
                    Exit For
                End If
            Next oPic
        End With
    Next rCell
End Sub

You just need to loop round the cells in the range.
 
Upvote 0
RickXL,

This is great. Thanks for the help, and sorry about my late response. However, I was wondering if you could answer two more (hopefully quick questions) to point me in the right direction...

1) What if I'm trying to apply this code to cells across columns? For example, instead of having the picture return values within cells F1:F4 only, could it be possible to apply it to cells F1:F4;G1:G4?

2) If a same value is entered in cells F1:F4, only one picture will appear, and the other cell(s) will just return the VLOOKUP of the picture's defined name. Any suggestions on altering this so that a multiple of one picture can be entered in multiple cells simultaneously?

Thanks again for the help!
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,916
Members
449,093
Latest member
dbomb1414

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