R1C1 VLOOKUP formula to another workbook

NikToo

Board Regular
Joined
Sep 24, 2015
Messages
66
Office Version
  1. 365
Not quite sure why this keeps giving me an Application error. I'm trying to just do a VLOOKUP to another workbook, just columns A to B but I need to do it down to the final row, hence the LastPivotRow. StrFile is the file with the Table I'm looking against and PivotTable. StartCol is the first empty cell as I'm building a data table by looking up values in several PivotTables in different workbooks.

VBA Code:
Sheets(1).Activate
    StartCol = Cells(2, Columns.Count).End(xlToLeft).Offset(0, 1).Count
    Cells(2, StartCol + 1).Select
    ActiveCell.FormulaR1C1 = _
    "=VLOOKUP(R2C[-StartCol], '[" & StrFile & "]PivotTable'!R1C1:R[PivotEndRow]C2,2,0)"
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Startcol and PivotEndRow are both vba variables to you need to close the double quotes and join them the same way as you have with strFile.
VBA Code:
"=VLOOKUP(R2C[-" & StartCol & "], '[" & StrFile & "]PivotTable'!R1C1:R[" & PivotEndRow & "]C2,2,0)"
Of course, this assumes that PivotEndRow is a valid variable.
 
Upvote 0
Thanks. It didn't complain at least. Now, follow up, how do I stop it making StartCol an absolute reference...? It comes out as =VLOOKUP(A$2, '[File.xlsx]PivotTable'!$A$1:$B27031,2,0)

Or a better way of doing it...
 
Last edited:
Upvote 0
I thought that R is relative when there is no row number defined as it should refer to the current row, you could try R[0]C to see if that makes a difference (never tried it so not sure if it works, but in theory it should).
 
Upvote 0
In R1C1 notation R2C means "row 2 of same column" so A$2 would approriate if you are in column A.
 
Upvote 0
Solution
Thanks, Mike!

I misread what was asked as 'stop making the row absolute' as the column was already relative. o_O
 
Upvote 0

Forum statistics

Threads
1,212,927
Messages
6,110,720
Members
448,294
Latest member
jmjmjmjmjmjm

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