在找出最后一行的问题

我正在尝试这个代码来找出最后一行 –

msgbox objExcel.ActiveWorkbook.Sheets(1).Range("A" & Rows.Count).EndXL(up).Row

但我一直在收到这个错误 –

Object required: Rows

这是你正在尝试?

 Dim oXLApp, wb, ws '~~> Define xlUp Const xlUp = -4162 Set oXLApp = CreateObject("Excel.Application") oXLApp.Visible = True '~~> Open file. Chnage path as applicable Set wb = oXLApp.Workbooks.Open("C:\Sample.xlsx") Set ws = wb.Worksheets(1) With ws MsgBox .Range("A" & .Rows.Count).End(xlUp).Row End With 

另请注意使用Worksheets而不是Sheets请参阅此链接以获取解释。

尝试

 objExcel.ActiveWorkbook.Sheets(1).Range("A" & Rows.Count).End(xlUp).Row 

只是你的End(xlUp)部分是错误的

这可能工作:

 dim last as integer ActiveCell.SpecialCells(xlLastCell).Select last = ActiveCell.SpecialCells(xlLastCell).Row