Reputation: 137
I launch the following code from workbook1, and want to count the number of rows in workbook2(wb.Name). Why does partII return the row count from workbook1?
Dim partII As String
Workbooks(wb.Name).Activate
Workbooks(wb.Name).Worksheets("sheet1").Select
partII = Range("A" & Rows.Count).End(xlUp).Row
For Each myRecord In Workbooks(wb.Name).Worksheets("sheet1").Range("A1:A" & partII)
Upvotes: 2
Views: 93
Reputation: 5962
Try the following simplified code:
with wb.worksheets("sheet1")
partII = .Range("A" & .Rows.Count).End(xlUp).Row
For Each myRecord In .Range("A1:A" & partII)
'...
Next myRecord
end with
Upvotes: 1