How do I apply a VLOOKUP function to an Excel colum as VBA code ?

Hi Experts,

I have an Excel sheet in which a column's cells are filled with a VLOOKUP formula. How can I translate that in VBA code ?

For example, I want to convert this Excel function on my sheet

=VLOOKUP(A3,'YTD May'!$A$2:$C$6000,3,FALSE)

and copy it down the column.

How would this look in VBA ?

Thanks
Toco
TocogroupAsked:
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.

Rodney EndrigaData AnalystCommented:
You can try this code:
Range("B3:B"&cells(rows.count,2).end(xlup).Row).FormulaR1C1 = "=VLOOKUP(RC[-1],YTD May!R1C2:R3C6000,3,FALSE)"

Open in new window


This will enter the formula with the results in Column B, assuming you have the lookup values in Column A.
0
Anthony BerenguelCommented:
So you want the result of the vlookup not the formula itself?
0
Rodney EndrigaData AnalystCommented:
UPDATED:
Range("B3:B"&cells(rows.count,1).end(xlup).Row).FormulaR1C1 = "=VLOOKUP(RC[-1],YTD May!R1C2:R3C6000,3,FALSE)"

Open in new window


This will enter the formula with the results in Column B, assuming you have the lookup values in Column A.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Rob HensonFinance AnalystCommented:
Basic line of code would be:

For Each Cell in Selection
ActiveCell.FormulR1C1 = "=VLOOKUP(RC[-8],'YTD May'!R2C1:R6000C3,3,FALSE)"
Next Cell

This assumes formula in column I, (column I -8 = column A, hence the RC[-8])

Thanks
Rob H
0
Rodney EndrigaData AnalystCommented:
3rd time the charm:

Range("B3:B"&cells(rows.count,1).end(xlup).Row).FormulaR1C1 = "=VLOOKUP(RC[-1],YTD May!R2C1:R6000C3,3,FALSE)"

Open in new window

0
TocogroupAuthor Commented:
Hi Rodney,
That works fine, thank you. If no matching value is found how do I replace the #N/A with a null ("") value ?
0
Rodney EndrigaData AnalystCommented:
You can try this code:
Range("B3:B"&cells(rows.count,1).end(xlup).Row).FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-1],YTD May!R2C1:R6000C3,3,FALSE),"""")"

Open in new window


This will result in BLANKS if there is #N/A in the lookup.
0
TocogroupAuthor Commented:
Excellent.
Finally, I'm holding the sheet name in a variable called strName. When I replace YTD May with the variable name it returns an error. How do I include the variable in the code ?
0
Rodney EndrigaData AnalystCommented:
Range("B3:B" & cells(rows.count,1).end(xlup).Row).FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-1]," & strName & "!R2C1:R6000C3,3,FALSE),"""")"

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

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

Start your 7-day free trial
TocogroupAuthor Commented:
Many thanks Rodney. That works perfectly.

Thanks for your contribution too, Rob
0
Rodney EndrigaData AnalystCommented:
No problem, Tocogroup! Glad to assist.
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.