find最后一个填充的行而不打开文件和条件

我想做什么:

i. Locate an excel document using path and doc name √ ii. Obtain the last filled row's number with a spreadsheet name without opening the file X iii. Obtain the last filled row's number BEFORE a certain row number, X ex: last filled row before row 40. There may be filled cells between 45-52, but I would like the function to return say "32" and not "52" like it does for me 

我拥有的:

该文件的path是一个经典的目录+文件名,它们被写入电子表格中具有该macros的两个单独的单元格中。 这是无关的,只是为了显示File_Path =path…

 File_Path = Chr(34) & Server & "\" & Range(Workbook_Loc).Value 'That's step one 

在位于File_Path的文件中,我希望进入名为MONTH YEAR的电子表格(例如:File_Path(“2015年7月”)),并find最后一个填充行的编号。 在第三行中,我尝试用B中最后一行的值填充一个单元格。我知道这里有很多在线的东西,但是我不能以某种方式使它工作:

 Set wb = Workbooks.Open(File_Path) ' This should open my workbook lastRow = wb(Chr(34) & Month & " " & Year & Chr(34)).Cells(1000, col).End(xlUp).Row Range(CurrentValue_Loc).Formula = "=INDEX('" & Server & "\" & Workbook & Month & " " & Year & "'!B" & lastRow & ",1)" 

前面的代码停在第二行; 它打开位于File_Path的工作簿,并给我以下错误:

 Run-time error '438' Object doesn't support this property method 

我究竟做错了什么?

最后,对于我的三。 点(见顶部的例子),我试图做到这一点:

  lastRow = wb(Chr(34) & Month & " " & Year & Chr(34)).Cells(40, col).End(xlUp).Row 

但它仍然给我最后一排,即使是在第40行之后

谢谢!

尝试查找最后一行时,不必再次键入工作簿的名称。 只需使用已经设置的工作簿variables即可。

 Sub lastRow() Dim lastRow As Long lastRow = wb.Sheets(1).Cells(Rows.Count, 2).End(xlUp).Row lastRow = wb.Sheets(1).Cells(40, 2).End(xlUp).Row End Sub 

只需使用已经声明的variables,然后使用Sheets() 方法来定义您正在查找最后一行的工作表。 在上面的示例中,我们正在查找工作簿中第一个工作表上的最后一行。