Riaan Lehmkuhl's Blog

Subversion, Progamming, Tips & Tricks and whatever else springs to mind.
12Jun

Microsoft Excel - How to check if a value exists in a range

12 June 2008 23:04 by Riaan Lehmkuhl
I had to compare some simple lists. I could have used a database, loaded the lists and used sql to do the comparisons. Not in the mood for that, then let's just use Excel.
We have the following two lists in a spreadsheet and we want to know which of the values in column B exists in column A:
  A B C
1 Fusce Vestibulum  
2 mauris mattis  
3 nulla Fusce  
4 pellentesque consequat  
5 iaculis libero  
6 aliquam eu  
7 eu sem  
8 tincidunt Nullam  
9 consequat iaculis  
10 metus elit  


After some digging around in the Excel functions, I found VLOOKUP that does the trick:
  A B C
1 Fusce
Vestibulum
=VLOOKUP(B1,$A$1:$A$10,1,FALSE)
2 mauris mattis #N/A
3 nulla Fusce Fusce
4 pellentesque consequat consequat
5 iaculis libero #N/A
6 aliquam eu eu
7 eu sem #N/A
8 tincidunt Nullam #N/A
9 consequat iaculis iaculis
10 metus elit #N/A


In column C it prints the search value if it was found, or "#N/A" if not found. This is ok, but we can make it look a bit nicer by adapting the formula a little bit:
  A B C
1 Fusce
Vestibulum
=ISNA(VLOOKUP(B1,$A$1:$A$10,1,FALSE)=B1)=FALSE
2 mauris mattis FALSE
3 nulla Fusce TRUE
4 pellentesque consequat TRUE
5 iaculis libero FALSE
6 aliquam eu TRUE
7 eu sem FALSE
8 tincidunt Nullam FALSE
9 consequat iaculis TRUE
10 metus elit FALSE


Using the updated formula, "TRUE" is printed if the value in column B is the same as the value returned from VLOOKUP otherwise "#N/A" is returned. This in turn is handled by the ISNA function, that's return value is then inverted so that the "TRUE" for "#N/A" is displayed as "FALSE" (the value was not found).

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Comments are closed

Riaan Lehmkuhl


Me, a disorder of the brain that results in a disruption in a person's thinking, mood, and ability to relate to others.

Recent comments

Comment RSS

Thingies

Calendar And Month List

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar

Disclaimer & Privacy

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Privacy:
We use third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.

Most comments

Cool Quote

I know that you believe that you understood what you think I said, but I am not sure you realize that what you heard is not what I meant. - Robert McCloskey