Excel - VLOOKUP table_array parameter as a variable

April 2017




Issue


I have this piece of code:
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-10],Position!R2C1:R50C2,2,FALSE)" 


However, I want to make Position!R2C1:R50C2 a variable. Meaning, this range may not always be the same row and column. I want it to be something like a variable range (ie, 'Range(Range("A2").End(xlDown), Range("A2").End(xlToRight))' for Position worksheet.

But it isn't working.

Solution


Dim src As String  
Dim ws As Worksheet  
Set ws = Sheets("Position")  

lastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row  
lastColumn = ws.Cells(1, Columns.Count).End(xlToLeft).Column  
src = "R1C1:R" & lastRow & "C" & lastColumn  

ActiveCell.FormulaR1C1 = _  
"=VLOOKUP(RC[-10],Position!" & src & ",2,FALSE)"


Thanks to rizvisa1 for this tip.

Related


Published by aakai1056. Latest update on December 8, 2011 at 05:05 AM by aakai1056.
This document, titled "Excel - VLOOKUP table_array parameter as a variable," is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (ccm.net).