Excel/VLookUp Last Row


Excel VBA 2007:

I would like to improve this code:

   Dim LastRowMaster As Long
   LastRowMaster = Workbooks("Master.xlsx.xlsm").Worksheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row

   With Workbooks("Master.xlsx.xlsm").Worksheets("Sheet1").Range("f2").Resize(LastRowMaster, 8)
     .Formula = "=iferror(vlookup($A2,'[PriceFile.xlsx]Prices'!$A$2:$L$12000,column()-1,False),"""")"
     .Formula = .Value
   End With

In arg2 of the vlookup, I would prefer it figure out how far down to go (similar to LastRowMaster), rather than hard coding with $A$2:$L$12000. I just can't figure out how to do it, can you help?


Assuming the lastrow for sheet Prices can be ascertained from column A of that sheet then

  Dim LastRowMaster As Long, LastRowPrice as Long
  LastRowMaster = Workbooks("Master.xlsx.xlsm") _
    Worksheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
  With Workbooks("Pricefile.xlsx").Worksheets(Prices)
   LastRowPrice = .Cells(.rows.count,"A").End(xlup).row
  End With
  With Workbooks("Master.xlsx.xlsm").Worksheets("Sheet1").Range("f2").Resize(LastRowMaster, 8)
    .Formula = "=iferror(vlookup($A2,'[PriceFile.xlsx]Prices'!$A$2:$L$" & _
     LastRowPrice & ",column()-1,False),"""")"
    .Formula = .Value
  End With

Tom Ogilvy

---------- FOLLOW-UP ----------

QUESTION: Thanks Tom!
It works! (I fixed worksheets(prices) = worksheets("prices"))
One thing I don't understand is that in one place it's (Rows.Count,"A") and in another it's (.rows.count,"A"); the difference being the dot before the rows. But Excel doesn't complain about that. Is this one of those things where the "dot" doesn't really matter?

 rows.count refers to the active sheet.

If the activesheet had a million rows and the Prices sheet had 65536 rows, then it would make a difference.  I have let rows.count be qualified by the prices sheet so it is specific to the number of rows in the prices sheet.

I assumed since the other use of rows.count was working I didn't need to worry about it.

Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here


All Answers

Answers by Expert:

Ask Experts


Tom Ogilvy


Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]


Extensive experience.

Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.