LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 58
Default avoiding #N/A when using vlookup

I'm using the Worksheet_Change event and the vlookup function in VBA to
populate the city and state fields in a worksheet when the user types a
value into the zip code field. This is what I'm using to do it:

With Target
.Offset(-2, 0) = "=VLOOKUP(ContactZip,zipCodeLookup,2,FALSE)"
.Offset(-2, 0).Copy
.Offset(-2, 0).PasteSpecial Paste:=xlPasteValues
.Offset(-1, 0) = "=VLOOKUP(ContactZip,zipCodeLookup,3,FALSE)"
.Offset(-1, 0).Copy
.Offset(-1, 0).PasteSpecial Paste:=xlPasteValues
End With

I'm only storing the zip codes for our state in Range("zipCodeLookup"), and
for the most part I'm avoiding #N/A errors when users enter zip codes from
other states by testing the inequalities

If Target.Address = "$E$19" And _
CVar(Target.Value) = 90000 And _
CVar(Target.Value) <= 96699 Then
. . .

This all works fine as far as it goes. However, if a user enters a zip code
between 90000 and 96699 that's not in the list of zip codes I'm using, they
get the #N/A message. So I need to modify my code so that if they type a
value between 90000 and 96699 that's not in my list, it doesn't produce the
#N/A message, and it runs the Exit Sub command. It's also important that
the procedure doesn't clear whatever values the user might have entered
manually in the City and State fields ( Offset(-1, 0) and Offset(-2, 0) ).

So what I need is an "If" statement that would test to see if the value
entered in the cell exists in the list. If not, I'd like to run the Exit
Sub command. Can anyone tell me what that If condition should be?

Thanks in advance.

Paul


 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Avoiding #N/A gibbylinks Excel Discussion (Misc queries) 8 February 6th 10 12:51 PM
Avoiding #N/A Heather Excel Discussion (Misc queries) 4 August 25th 09 08:41 PM
Avoiding #value MicroMain Excel Worksheet Functions 2 January 25th 06 05:11 AM
Avoiding #NUM! Bruno Campanini Excel Worksheet Functions 9 September 14th 05 02:34 PM
vlookup avoiding #N/A Lillian Eik Excel Worksheet Functions 6 July 6th 05 03:03 PM


All times are GMT +1. The time now is 08:40 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"