Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2
Default How to insert zipcode(number) in one cell when city(text) put in another

I want a formula or vb script that would insert a zipcode in one cell
when I put the city in an adjacent cell.

Cell 1 Cell 2
Culpeper 22701

  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 102
Default How to insert zipcode(number) in one cell when city(text) put in another


SlowPoke wrote:
I want a formula or vb script that would insert a zipcode in one cell
when I put the city in an adjacent cell.

Cell 1 Cell 2
Culpeper 22701


Depending on how involved your list of cities would be, there are two
ways that I know of to accomplish this. If this is to be used on a
national type basis (All cities in the U.S. then you would have to
construct a table with two columns. One would contain the city name,
the second the corresponding Zip. You could then use data validation
to lookup the appropriate code. Another way would be to use code
behind the scenes and a SelectCase or Choose statement. I wouldn't do
this unless I had 20 cities or less to work with though. Hope that
helps

  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2
Default How to insert zipcode(number) in one cell when city(text) put in another

The number of cities is small (less than 20). I am not very literate
at doing VB scripting. Here was my attempt that did not work. Can
someone give some guidance? Thanks.

Sub Towns()
For Col = 0 To 100 Step 1
Select Case Cells(A, Col).Value
Case "Ravenswood"
Cells(B, Col) = 26164
Case "Harrisburg"
Cells(B, Col) = 17110
Case "Culpeper"
Cells(B, Col) = 22701
End Select
Next Col
If LCase(Selection.Value).Value="ravenswood"
Then Cells(B,Col).Value="26164
End Sub

Mark wrote:
SlowPoke wrote:
I want a formula or vb script that would insert a zipcode in one cell
when I put the city in an adjacent cell.

Cell 1 Cell 2
Culpeper 22701


Depending on how involved your list of cities would be, there are two
ways that I know of to accomplish this. If this is to be used on a
national type basis (All cities in the U.S. then you would have to
construct a table with two columns. One would contain the city name,
the second the corresponding Zip. You could then use data validation
to lookup the appropriate code. Another way would be to use code
behind the scenes and a SelectCase or Choose statement. I wouldn't do
this unless I had 20 cities or less to work with though. Hope that
helps


  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 102
Default How to insert zipcode(number) in one cell when city(text) put in another

I realize that this will be a slightly different approach than you were
using but it will be in the ballpark. If you dont like the way it
works, just let me know and we can work on it till it functions exactly
as you want... Copy and paste this into the worksheet code module that
you are using...

Private Sub Worksheet_Change(ByVal Target As Range)

Dim CurRng, CtyRng, ZipRng As Range

'CHECK TO SEE IF THE CURRENTLY SELECTED CELL IS WITHIN
'THE APPROPRIATE CELL RANGE:
Set CurRng = Union(Target, Range("A1:A100"))
If CurRng.Address < "$A$1:$A$100" Then Exit Sub
'SET A REFERENCE TO THE CURRENTLY SELECTED CELL
Set CtyRng = Target
'SET A REFERENCE TO THE CELL IMMEDIATELY TO THE RIGHT
'OF THE CURRENTLY SELECTED CELL
Set ZipRng = CtyRng.Offset(0, 1)
'COMPARE THE ENTRY IN THE CURRENT CELL TO THE FOLLOWING
'LIST IN ORDER TO DETERMINE THE APPROPRIATE ZIP CODE
Select Case CtyRng
'NOTICE THAT IN THIS CASE, SEPERATING THE TWO CHOICES BY A
'COMMA WILL RESULT IN BOTH THE UPPER AND LOWER CASES
'YIELDING THE ZIP 26164. THIS MAY HELP YOU LATER ON WITH OTHER
'SIMILAR CHOICES
Case "Ravenswood", "ravenswood"
ZipRng = 26164
Case "Harrisburg"
ZipRng = 17110
Case "Culpeper"
ZipRng = 22701
End Select

End Sub


This will automatically enter the appropriate zip in the B column once
an entry has been detected anywhere in the range A1:A100. Hope this is
what you needed.....

Reply
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
Insert rows (1 to 4) if the cell value equals a fixed word Joy Excel Discussion (Misc queries) 1 July 20th 06 08:40 AM
Help with this conditional IF statement C-Dawg Excel Discussion (Misc queries) 3 May 15th 06 06:01 PM
How to insert line on other worksheet depending upon result in cell? Pheasant Plucker® Excel Discussion (Misc queries) 4 March 17th 06 10:05 AM
cell color index comparison MINAL ZUNKE New Users to Excel 1 June 30th 05 07:11 AM
Copy cell format to cell on another worksht and update automatical kevinm Excel Worksheet Functions 21 May 19th 05 11:07 AM


All times are GMT +1. The time now is 06:45 AM.

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

About Us

"It's about Microsoft Excel"