I try to copy all sheets of a closed workbook and paste it in the workbook I am working with.
I tried following code:
Sub copy_Ws()
Dim wb As Workbook: Set wb = ThisWorkbook
Dim sourceWb As Workbook
Dim sh As Worksheet: Set sh = wb.Worksheets(1)
Dim sourceWs As Worksheet
Dim cell As Range: Set cell = sh.Range("C1:C50")
Dim currentCell As Range
Dim filename As String
Dim sourceWbName As String
Dim path As String
For Each currentCell In cell
If IsEmpty(currentCell) = False Then
On Error Resume Next
Set sourceWb = Workbooks(currentCell.Value)
Debug.Print (currentCell.Value)
For Each ws In sourceWb.Sheets
' Copy the worksheet
ws.Copy After:=wb.Sheets(wb.Sheets.Count)
Next ws
On Error GoTo 0
End If
Next currentCell
End Sub
Just that you know in the currentCell are the paths saved.
My problem is that I get something like Index out of bounds 9 and the compiler is showing me following line "Set sourceWb = Workbooks(currentCell.Value)"
What can I do to get the needed workbook?
Thank you in advance
Import All Worksheets From Multiple Files in a List
When a file is open, you create a reference to it by using its name (not its path!), e.g.:
When a file is closed, you need to use the
Openmethod to open it and create a reference to it by using its path, e.g.: