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 ?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.
Anthony BerenguelCommented:
So you want the result of the vlookup not the formula itself?
Rodney EndrigaData AnalystCommented:
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.
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

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])

Rob H
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

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 ?
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.
TocogroupAuthor Commented:
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 ?
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

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
Rodney EndrigaData AnalystCommented:
No problem, Tocogroup! Glad to assist.
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.