Dears, Can anybody help me why stops this code with error message (Run-time error '9': Subscript out of range)?
The code snippet:
CollectFileName = "C:\Users\g\Documents\CAFM\VBS\access.xlsx"
Workbooks.Open (CollectFileName)
ActiveWindow.Visible = False
LastDayRow = Workbooks(CollectFileName).Sheets(1).Range("B" & Rows.Count).End(xlUp).Row
When using a workbook to specify the Sheet, you should also do the same with Rows.Count
(and others, like Cells()
, , Columns()
, Rows()
, etc.):
LastDayRow = Workbooks(CollectFileName).Sheets(1).Range("B" & Workbooks(CollectFileName).Sheets(1).Rows.Count).End(xlUp).Row
or
CollectFileName = "C:\Users\g\Documents\CAFM\VBS\access.xlsx"
Dim wb as Workbook
Set wb = Workbooks.Open(CollectFileName)
With wb.Sheets(1)
lastDayRow = .Range("B" & .Rows.Count).End(xlUp).Row
End With
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.