VBA to store Vlookup Function in a cell

mrexcelhelp123

New Member
Joined
Mar 10, 2010
Messages
38
Hi There!

I have a workbook where I need a cell to be populated in an array of worksheets I specfiy, with below function:

=IF(ISNA(VLOOKUP($C$3&" * ",'Y:\ttd\January 2010\[Jan_costs.xlsx]revdata'!$A$11:$AA$1239,5,0))=TRUE,"",VLOOKUP($C$3&" * ",'Y:\ttd\January 2010\[Jan_costs.xlsx]revdata'!$A$11:$AA$1239,5,0))"

This function performs a vlookup in another workbook.

This is what I thought I could do but I keep getting a data mismatch error. I'm a novice VBA user so I'm sure that I'm doing something obviously wrong. Thanks.


Sheets(Array("NAT", "MCHS")).Select

Range("C2").Select
ActiveCell.Formula = "=IF(ISNA(VLOOKUP($C$3&" * ",'Y:\ttd\January 2010\[Jan_costs.xlsx]revdata'!$A$11:$AA$1239,5,0))=TRUE,"",VLOOKUP($C$3&" * ",'Y:\ttd\January 2010\[Jan_projcosts.xlsx]revdata'!$A$11:$AA$1239,5,0))"
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Try

Code:
Dim ws As Worksheet
For Each ws In Sheets(Array("NAT", "MCHS"))
    ws.Range("C2").Formula = "=IF(ISNA(VLOOKUP($C$3&"" * "",'Y:\ttd\January 2010\[Jan_costs.xlsx]revdata'!$A$11:$AA$1239,5,0))=TRUE,"""",VLOOKUP($C$3&"" * "",'Y:\ttd\January 2010\[Jan_projcosts.xlsx]revdata'!$A$11:$AA$1239,5,0))"
Next ws
 
Upvote 0
Sheets(Array("NAT", "MCHS")).Select

Range("C2").Select
ActiveCell.Formula = "=IF(ISNA(VLOOKUP($C$3&" * ",'Y:\ttd\January 2010\[Jan_costs.xlsx]revdata'!$A$11:$AA$1239,5,0))=TRUE,"",VLOOKUP($C$3&" * ",'Y:\ttd\January 2010\[Jan_projcosts.xlsx]revdata'!$A$11:$AA$1239,5,0))"


I think the quotes within the formula are messing you up. To have a literal quote within a text string you have to use two quotes e.g.
$C$3&"" * "",'Y:
 
Upvote 0
Thanks! Works great except is there anyway to bypass the pop up window that requests for file location of Jan_cost.xlsx? Right now, I can just hit Cancel twice but it would be great if I didn't have to. Otherwise, is the only solution to this is to have the file open?
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top