Copy several sheets (names in array) from a Workbook to my active open Workbook

BVOPP

Board Regular
Joined
Feb 9, 2015
Messages
50
I created the below code and it works fine for the first sheet in the array. But when selecting the second sheet it shows an error: Run-time error '1004': Select method of Worksheet class failed

What is the correct code to solve this issue? Thanks in advance for the support!

VBA Code:
Sub SheetGetFromWorkbook3(List_of_Sheets As Variant, message As String)

    Dim ActBook As Workbook
    Dim ExistBook As Workbook
    Dim ActBook_Name As String
    Dim FromBook_Name As String
    Dim NewFileType As String
    Dim X As Integer
    
    Set ActBook = ActiveWorkbook
    ActBook_Name = ActiveWorkbook.Name
    
    NewFileType = "Excel Files 2007 (*.xlsx), *.xlsx," & _
                   "Excel Files 1997-2003 (*.xls), *.xls," & _
                   "Report Files *.xlsm (*.xlsm),"
    
    FileToOpen = Application.GetOpenFilename(Title:=message, FileFilter:=NewFileType)
    If FileToOpen = False Then
        MsgBox "No File Specified.", vbExclamation, "ERROR"
        Exit Sub
    Else
        Set ExistBook = Workbooks.Open(Filename:=FileToOpen)
        FromBook_Name = ActiveWorkbook.Name
    End If
    
        Application.DisplayAlerts = False
 
        For X = LBound(List_of_Sheets) To UBound(List_of_Sheets)
            Workbooks(FromBook_Name).Sheets(List_of_Sheets(X)).Visible = True
  [COLOR=rgb(209, 72, 65)]          Workbooks(FromBook_Name).Sheets(List_of_Sheets(X)).Select[/COLOR]
            Workbooks(FromBook_Name).Sheets(List_of_Sheets(X)).Copy Before:=Workbooks(ActBook_Name).Sheets(1)
        Next X
        Application.DisplayAlerts = True
    
        'suppress saving of the existing workbook and close it
        ExistBook.Saved = True
        ExistBook.Close

End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
How about
VBA Code:
   For x = LBound(List_of_Sheets) To UBound(List_of_Sheets)
      With ExistBook.Sheets(List_of_Sheets(x))
         .Visible = xlSheetVisible
         .Copy Before:=Workbooks(ActBook_Name).Sheets(1)
      End With
   Next x
 
Upvote 0
Wow !!! that works thanks a lot Fluff !!

one additional.. if i would have a 2 dimensional array with also the rename of the several sheets... how would the rename of the sheet work in your code?

Hope i am not to greedy now! ;)
 
Upvote 0
Try
VBA Code:
   For x = LBound(List_of_Sheets) To UBound(List_of_Sheets)
      With ExistBook.Sheets(List_of_Sheets(x, 1))
         .Visible = xlSheetVisible
         .Copy Before:=Workbooks(ActBook_Name).Sheets(1)
      End With
      Workbooks(ActBook_Name).Sheets(List_of_Sheets(x, 1)).Name = List_of_Sheets(x, 2)
   Next x
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,561
Messages
6,120,242
Members
448,951
Latest member
jennlynn

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top