Hi,
I am trying to set up a 'master' workbook that collates worksheets from a variety of different workbook. It appears to work fine for the first run and it appears to run through everything ok within the second loop, until it gets to this section within the code
Then I get a run time error '9' Subscript out of Range
For contents the full script is listed below.
Any thoughts of how to fix this issue?
Also, apologies where the above is long-winded as I have no doubt others could make what I am doing a lot quicker. While I've been trying to work with VBA, I'm still very much a novice at it.
Thanks,
EMcK
I am trying to set up a 'master' workbook that collates worksheets from a variety of different workbook. It appears to work fine for the first run and it appears to run through everything ok within the second loop, until it gets to this section within the code
Code:
Workbooks(FileName2).Sheets(arr).Copy _
After:=Workbooks(FileName1).Sheets(LastSheet)
Then I get a run time error '9' Subscript out of Range
For contents the full script is listed below.
Code:
Sub CollateAllResSheets()
Dim FolderPath1 As String
Dim FileName1 As String
Dim FileName2 As String
Dim SheetName1 As String
Dim WorkBk1 As Workbook
Dim WorkBk2 As Workbook
Dim SourceRange As Range
Dim DestRange As Range
Dim data1 As Worksheet
Dim data2 As Worksheet
Dim Src As Worksheet
Dim Dest As Worksheet
Dim destbks As Worksheet
Set data1 = Worksheets("DATA")
FolderPath1 = data1.Cells(5, 3)
FileName1 = data1.Cells(4, 3)
' Call Dir the first time, pointing it to all Excel files in the folder path.
FileName2 = Dir(FolderPath1 & "*.xlsx")
' Loop until Dir returns an empty string.
Do While FileName2 <> ""
' Open a workbook in the folder
Set WorkBk2 = Workbooks.Open(FolderPath1 & FileName2)
Workbooks(FileName1).Activate
LastSheet = Workbooks(FileName1).Sheets(Sheets.Count).Name
Application.ScreenUpdating = False
With Sheets("WORKING").Activate
End With
Set wbk = Workbooks(FileName2)
Set wbk = ActiveWorkbook
Dim n As Long
n = 3
For i = 3 To Workbooks(FileName2).Sheets.Count
If Workbooks(FileName2).Sheets(i).Visible = True Then
Cells(n, 2) = Workbooks(FileName2).Sheets(i).Name
n = n + 1
End If
Next i
Dim UsdRws As Long
Dim Rng As Range
Dim cl As Range
Dim Cnt As Long
Dim arr() As Variant
With Sheets("WORKING")
UsdRws = .Range("B3").End(xlDown).Row
Set Rng = .Range("B3:B" & UsdRws)
End With
For Each cl In Rng
Cnt = Cnt + 1
ReDim Preserve arr(1 To Cnt)
arr(Cnt) = cl
Next cl
Workbooks(FileName2).Sheets(arr).Copy _
After:=Workbooks(FileName1).Sheets(LastSheet)
Application.ScreenUpdating = True
' Close the source workbook while saving changes.
Workbooks(FileName2).Save
Workbooks(FileName2).Close
With Sheets("WORKING").Activate
Range("B3:B" & UsdRws).ClearContents
End With
' Use Dir to get the next file name.
FileName2 = Dir()
Loop
End Sub
Any thoughts of how to fix this issue?
Also, apologies where the above is long-winded as I have no doubt others could make what I am doing a lot quicker. While I've been trying to work with VBA, I'm still very much a novice at it.
Thanks,
EMcK