P
Paul James
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
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