I require some help with an error I receive when a macro executes.
The purpose of the macro is to copy data from a workbook the user selects to the workbook the macro is in. It loops though the sheets and copies data from specific cells on each sheet to a row, one sheet to each row.
The error is on line: fSheetCount = Workbooks(fName).Worksheets.Count
The error is: Run-time error’9’ Subscript out of range.
It works if the file to be copied from is also open.
Your help will be greatly appreciated. I’m an amateur so any suggestion to improve the code and add error handling is also welcome.
Thanks,
Ron
The purpose of the macro is to copy data from a workbook the user selects to the workbook the macro is in. It loops though the sheets and copies data from specific cells on each sheet to a row, one sheet to each row.
The error is on line: fSheetCount = Workbooks(fName).Worksheets.Count
The error is: Run-time error’9’ Subscript out of range.
It works if the file to be copied from is also open.
Your help will be greatly appreciated. I’m an amateur so any suggestion to improve the code and add error handling is also welcome.
Thanks,
Ron
Code:
Sub CopyBoe()
Dim taskID As String
Dim resource As String
Dim pValue As String
Dim sDate As String
Dim eDate As String
Dim rCount As Integer
Dim i As Integer
Dim ppTempFileName As String
Dim fSheetCount As Integer
Dim fName As String
Dim fileToOpen As String
ppTempFileName = ThisWorkbook.Name
fileToOpen = Application _
.GetOpenFilename("excel files (*.xls), *.xls")
fName = Dir(fileToOpen)
fSheetCount = Workbooks(fName).Worksheets.Count
rCount = 3
For i = 1 To fSheetCount
Windows(fName).Activate
Sheets(i).Select
taskID = Cells(2, 5).Value
resource = Cells(4, 2).Value
pValue = Cells(7, 8).Value
sDate = Cells(6, 2).Value
eDate = Cells(6, 4).Value
Windows(ppTempFileName).Activate
Sheets("Resources (Spread or Load)").Select
Cells(rCount, 1).Value = taskID
Cells(rCount, 2).Value = resource
Cells(rCount, 11).Value = pValue
Cells(rCount, 12).Value = sDate
Cells(rCount, 13).Value = eDate
rCount = rCount + 1
Next i
End Sub