Using VLOOKUP to return a picture Ken Puls, Microsoft MVP

Transcription

Using VLOOKUP to return a picture Ken Puls, Microsoft MVP
Begin your journey to spreadsheet mastery
with PD Net’s Excel Certificate Program
Using VLOOKUP to return a picture
by Ken Puls, Microsoft MVP
Something that can be very handy when you’re building a dashboard is to return a
certain picture depending on a condition. We can use VLOOKUP to look up data in a
table and return the corresponding value from a different column, but
unfortunately we can’t do that with pictures.
I’ve seen the “how can I return a picture using VLOOKUP” question asked in forums
many times, and until recently, I would have reached to a VBA macro to do this. In
fact, I actually have dashboards at my day job that do exactly that. At the 2013 MVP
summit though, I learned a trick to do this without any VBA at all, and I thought I’d
share it here.
This example is based on looking up a picture to display the appropriate icon for a
weather forecast; something we use on our dashboards from our golf course. We
update the weather data daily via a weather feed, and really don’t want to have to
manually update each picture, so this technique comes in really handy.
Step 1: Creating the Picture Table
The first thing we need to do is create a table of our pictures. For this example,
we’ll start with each of the potential weather conditions that could be returned by a
weather feed, as follows:
This list is contained in A1:A11 on the “Pictures” worksheet.
www.cga-pdnet.org
Using VLOOKUP to return a picture
1
Begin your journey to spreadsheet mastery
with PD Net’s Excel Certificate Program
Next, we need images for the forecasts. The attached example file contains weather forecast images
snapped with my favourite screen capture program (SnagIt by TechSmith) which were then pasted in the
table next to the correct description:
Now, there are a couple of really important things to notice here:
•
•
The pictures don’t all have to be the same size but they should be close
The pictures MUST fit entirely in the cells with at least a little bit of white space around them
Next, we need to name the cells that hold the hold the pictures. Because we have a lot of them, the
easiest and fastest way to do this is by using the “Create from Selection” feature:
• Select cells A2:B11
• Go to Formulas  Defined Name  Create from selection
• Choose to created names from values in the Left column
What this will do for us is create a defined name of “rain” for B2, “isoshower” for B3, and so on down
the table. You can verify that this works by selecting cell B6 and noticing that it says “rain” in the name
box:
www.cga-pdnet.org
Using VLOOKUP to return a picture
2
Begin your journey to spreadsheet mastery
with PD Net’s Excel Certificate Program
Great, now our table is set up correctly.
Step 2: Setting Up the Driver Cell(s)
So the next step is to build the plumbing that will drive which picture will be shown. This could be done
in any number of manners including any of the following:
•
•
•
•
Data being pulled in from a query table
Manual entry
A VLOOKUP function
A data validation list
The possibilities are truly endless, with the only key being, the value in the driver cell MUST match one
of the names in the Picture Name list that we have in cells A2:A11. To that end, we’re going to set up a
quick table that looks like this:
Cells A3:E3 are text values that were entered, but A4:E4 were set up using a data validation list in this
case. To do that:
•
•
•
•
•
Select cells A4:E4
Go to DataData Validation  Data Validation
Choose to allow a List
Select A2:A11 on the Pictures worksheet (or just type =Pictures!$A$2:$A$11 )
Click OK
You’ll now be able to select items from the list by clicking the dropdown arrows in the cells. Remember
though, this is just for simplicity here, and you could drive these cells in via any manner or formula you
wanted, so long as it returns an item that matches your list.
Now, we also need a few more names as well. In fact, we need a name for each of the individual days.
So let’s set those up by doing the following:
•
•
•
Select cells A4:E5
Go to Formulas  Defined Name  Create from selection
Choose to created names from values in the Top Row (only)
www.cga-pdnet.org
Using VLOOKUP to return a picture
3
Begin your journey to spreadsheet mastery
with PD Net’s Excel Certificate Program
Cool, so now A4 has a name of “Day_01”, B4 is “Day_02”, and so on. We’re almost there, but we need
to make a minor modification to these named ranges. So let’s open the Name Manager:
•
•
•
Go to Formulas  Name Manager
Select Day_01
Click Edit
What we need to do here is change the Refers To formula from =Forecast!$A$4 to
=INDIRECT(Forecast!$A$4)
The reason for this is that cell A4 contains the text value of “rain”. What the INDIRECT function does is
essentially tries to interpret the text as a formula. As it happens, we have a defined name called “rain”,
which refers to cell B2 on the Pictures worksheet. So the INDIRECT function will return a reference to
that cell for us!
Once you’ve updated that formula, make sure you also update the other Day_0x names as well:
•
•
•
•
Day_02:
Day_03:
Day_04:
Day_05:
=INDIRECT(Forecast!$B$4)
=INDIRECT(Forecast!$C$4)
=INDIRECT(Forecast!$D$4)
=INDIRECT(Forecast!$E$4)
Now, I know that this doesn’t look like much, but you’ve laid all the groundwork to make some magic
happen!
Step 3: The Picture Lookup
What we need to do now is go and copy any picture from the Pictures worksheet. It doesn’t matter
which one, but you need to do this correctly:
•
•
•
•
•
Find the picture you want
Select the cell, NOT the picture!
Press CTRL+C to copy it
Go to the Forecast worksheet
Right click, and choose to Paste as a Linked Picture
Careful here! You need to get the right icon to paste with. In Excel 2010 you’ll find it here:
www.cga-pdnet.org
Using VLOOKUP to return a picture
4
Begin your journey to spreadsheet mastery
with PD Net’s Excel Certificate Program
Now, with that done, you have one more thing to do:
•
•
Select the newly pasted picture
Replace the formula in the formula bar with: =Day_01
And that’s it! Try changing the value in A1 and see what happens… instant picture lookup!
To do the others, you just follow a similar route. Copy any cell, and paste as a linked picture. In fact,
you can even paste the four remaining ones one after the other. Once done you need to select each
picture and update the formula to =Day_02, =Day_03, etc…
www.cga-pdnet.org
Using VLOOKUP to return a picture
5
Begin your journey to spreadsheet mastery
with PD Net’s Excel Certificate Program
Ending Thoughts
This is a really cool trick, doesn’t need a single line of VBA, and actually doesn’t even use a VLOOKUP
function at all! It’s purely driven by the INDIRECT function pointing back to a cell range with the Camera
object (invoked by the Paste Picture as Link ability.)
Do be warned that the camera object does have some issues, and you may not find the pictures stable if
you use too many. In my experience, that’s been more than 10 linked pictures. (Although the table of
pictures can have hundreds of pictures in it to choose from with no issues.)
You can download both the starting and completed versions of the file from the links below:
PictureLookup-Start.xlsx
Picturelookup-complete.xlsx
About Ken Puls
Ken Puls, CMA, Microsoft MVP, is Controller/Director of IT at Fairwinds Community & Resort on
Vancouver Island, BC. Building on his solid accounting background and passion for IT, Ken is an expert at
developing automated solutions and internal controls to improve Business Intelligence systems. A firm
believer that human brain power should be devoted to solving problems, not data input, Ken’s passion
lies in developing new systems using technology, such as Excel spreadsheets, that enable his staff to
focus on what is truly important: running the business.
Ken has been an active participant in numerous Excel Web forums since 2002 and hosts a website
knowledgebase and blog at www.excelguru.ca. NASA has made use of Ken’s expertise, using some of his
examples to help monitor the power supply levels on the International Space Station.
In recognition of his contributions to the online community, Ken was awarded the prestigious Microsoft
Most Valuable Professional — Excel award in October 2006, a distinction he holds to this day. In
Microsoft's words, "MVPs are a highly select group of experts representing technology's best and
brightest who share a deep commitment to community and a willingness to help others."
Worldwide, there are more than 100 million participants in technical communities; of these participants,
there are fewer than 200 who hold the distinction of MVP-Excel, and Ken is one of only four Excel MVPs
in Canada.
www.cga-pdnet.org
Using VLOOKUP to return a picture
6
Begin your journey to spreadsheet mastery
with PD Net’s Excel Certificate Program
Ken is dedicated to teaching users how to get the most out of Excel spreadsheets in order to improve
their business, and for the past several years has been developing in-depth courses and training
products for various companies and associations.
Enjoyed this cool tutorial? PD Net offers video courses by Ken Puls that allows
participants to work through relevant examples and explanations of the
concepts. Follow along with extensive course resources, eBooks, and
examples to enhance your learning experience — and they are yours to keep!
Courses
•
Excel Certificate Program
•
Introduction to Excel: Beginning the Journey to Spreadsheet Mastery
•
Formulas and Functions: An Introduction to the Power of Excel
•
Formatting Tools: Adding Eye Catching Elements
•
Formulas and Functions: Logic and Lookups
•
Math and Finance Functions: Formulas for Business Decision Makers
•
Date and Time Functions: Theory and Application
•
Text and Information Functions: It’s Not All About the Numbers
•
Excel Tables and Tabular Data
•
Introduction to PivotTables in Excel
•
Introduction to Data Visualization: Turning Data into Information
•
Designing Dynamic Financial Spreadsheet Models in Excel
•
Designing Stable Spreadsheet Models in Excel
www.cga-pdnet.org
Using VLOOKUP to return a picture
7