# Fastest Way Change Value of Cell in Excel Based on Another Sheet VBA

Hello Everyone,

I have a sheet named "Pull" with zip codes pulled from my database in Column A.  The other sheet I have named "Zips" has every zip code in the United States in Column A - and the City Name in Column B.  I want to create code that will replace the zip code in Column A of sheet "Pulls" with the City name from the sheet named "Zips".

I know I can use find and replace, or if statments, but I am looking for a way that will not bog down the program seeing as there are more than 42000 zip codes in the sheet "Zips".  Thank you.
LVL 2
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Could you use a lookup function? have the data in a separate sheet
0
Mechanical EngineerCommented:
If you know that every Zip code is found in the other sheet--and that sheet has been sorted in ascending order by Zip Code--then the formula method is pretty fast:
=VLOOKUP(A1,Zips!A\$1:B\$45000,2)              copy this formula down by double-clicking the little square at the bottom right corner of the selection marquee

If the Zip code might not be found in the other sheet (or your Zip codes aren't sorted), then try:
=VLOOKUP(A1,Zips!A\$1:B\$45000,2,FALSE)

0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Mechanical EngineerCommented:
The first formula took about 1 second on my laptop to determine 42,000 randomly chosen city names using Excel 2003. The second formula took 25 seconds.

0
Mechanical EngineerCommented:
Here is a macro that automates the first formula. After the formulas have calculated, they are replaced with the values returned. Remember that it has the limitation of requiring the Zips worksheet to be sorted by column A and for it to contain a complete list. You'll get errors if either condition is not satisfied 100% of the time!

This macro should be installed in a regular module sheet, as shown in the sample workbook. This workbook is somewhat large, so be patient while it downloads.
``````Sub GetCityNames()
Dim rg As Range, rgZIPtable As Range
Set rgZIPtable = Worksheets("Zips").Range("A1")     'First ZIP number in ZIP table
Set rgZIPtable = Range(rgZIPtable, rgZIPtable.Worksheet.Cells(Rows.Count, rgZIPtable.Column).End(xlUp)).Resize(, 2)     'The complete table, including city names
Set rg = Range("A1")    'First ZIP number in active worksheet
Set rg = Range(rg, Cells(Rows.Count, rg.Column).End(xlUp)).Resize(, 2)      'The complete Pull list, including space for city names
Application.ScreenUpdating = False
rg.Columns(2).Formula = "=VLOOKUP(" & rg.Cells(1, 1).Address(False, False) & "," & rgZIPtable.Address(external:=True) & ",2)"
rg.Columns(2).Formula = rg.Columns(2).Value     'Replace the formula with the value
Application.ScreenUpdating = True
End Sub
``````
GetZipsQ25878580.xls
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.