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

BVOPP

New Member
Joined
Feb 9, 2015
Messages
47
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

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
73,045
Office Version
  1. 365
Platform
  1. Windows
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
 

BVOPP

New Member
Joined
Feb 9, 2015
Messages
47
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! ;)
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
73,045
Office Version
  1. 365
Platform
  1. Windows
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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
73,045
Office Version
  1. 365
Platform
  1. Windows
Glad to help & thanks for the feedback.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,631
Messages
5,832,768
Members
430,166
Latest member
Nige Leeming

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
Top