Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]() 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
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
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 |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Insert rows (1 to 4) if the cell value equals a fixed word | Excel Discussion (Misc queries) | |||
Help with this conditional IF statement | Excel Discussion (Misc queries) | |||
How to insert line on other worksheet depending upon result in cell? | Excel Discussion (Misc queries) | |||
cell color index comparison | New Users to Excel | |||
Copy cell format to cell on another worksht and update automatical | Excel Worksheet Functions |