macro to import multiple sheets from closed file to open file

Ali M

Active Member
Joined
Oct 10, 2021
Messages
304
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
Hi experts
I have open file and the same time another closed file in the same folder .
in the closed file contains multiple sheets and different structure data with buttons and shapes the closed file is xlsm extension
the open file contains list in column G contains sheets names .
the macro should import sheets based on column G from closed file with the same arranging based on column G and when run the macro every time then just replace data for sheets have already existed , not add sheets every time when run the macro except there is new sheet should import based on column G
the files are existed in this directory "C:\Users\NAME\Desktop\
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Try this macro. The sheet names start in G2.
VBA Code:
Public Sub Import_Named_Sheets_From_Workbook()

    Dim fromWorkbookFileName As Variant
    Dim openWb As Workbook, fromWb As Workbook
    Dim importSheets As Variant
    Dim sheetName As Variant
        
    'Choose closed workbook
    
    fromWorkbookFileName = Application.GetOpenFilename("Excel Files (*.xlsm), *.xlsm", Title:="Select closed workbook")
    If fromWorkbookFileName = False Then Exit Sub
    
    Set openWb = ActiveWorkbook
    
    With openWb.ActiveSheet
        importSheets = .Range("G2:G" & .Cells(.Rows.Count, "G").End(xlUp).Row).Value
    End With
    
    Application.ScreenUpdating = False
    
    'Open the closed workbook
    
    Set fromWb = Workbooks.Open(Filename:=fromWorkbookFileName)
    
    With openWb
        For Each sheetName In importSheets
            Application.DisplayAlerts = False
            On Error Resume Next
            .Sheets(sheetName).Delete
            On Error GoTo 0
            Application.DisplayAlerts = True
            fromWb.Sheets(sheetName).Copy After:=.Sheets(.Sheets.Count)
        Next
    End With
        
    fromWb.Close savechanges:=False

    Application.ScreenUpdating = False

End Sub
 
Upvote 1
Hi John,
thanks for the macro , it works greatly !
just there is problem if the sheet is not existed will show mismatch error or subscript out of range and crash the file . I want ignoring any sheet is not existed .
 
Upvote 0
just there is problem if the sheet is not existed will show mismatch error or subscript out of range and crash the file . I want ignoring any sheet is not existed .
Simply move On Error GoTo 0 immediately above Next.
 
Upvote 0
I can't know where is debug error in which line, sorry !
it will crash after show error .

still shows mismatch error
 
Upvote 0
Where have you moved the On Error GoTo 0 to? It should be this:
VBA Code:
Public Sub Import_Named_Sheets_From_Workbook()

    Dim fromWorkbookFileName As Variant
    Dim openWb As Workbook, fromWb As Workbook
    Dim importSheets As Variant
    Dim sheetName As Variant
        
    'Choose closed workbook
    
    fromWorkbookFileName = Application.GetOpenFilename("Excel Files (*.xlsm), *.xlsm", Title:="Select closed workbook")
    If fromWorkbookFileName = False Then Exit Sub
    
    Set openWb = ActiveWorkbook
    
    With openWb.ActiveSheet
        importSheets = .Range("G2:G" & .Cells(.Rows.Count, "G").End(xlUp).Row).Value
    End With
    
    Application.ScreenUpdating = False
    
    'Open the closed workbook
    
    Set fromWb = Workbooks.Open(Filename:=fromWorkbookFileName)
    
    With openWb
        For Each sheetName In importSheets
            Application.DisplayAlerts = False
            On Error Resume Next
            .Sheets(sheetName).Delete
            Application.DisplayAlerts = True
            fromWb.Sheets(sheetName).Copy After:=.Sheets(.Sheets.Count)
            On Error GoTo 0
        Next
    End With
        
    fromWb.Close savechanges:=False

    Application.ScreenUpdating = False

End Sub
 
Upvote 1
Solution
I know no what my bad when asked me put the line On Error GoTo 0 ,sorry
now it works excellently without problem.(y)
thank you so much .:)
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,988
Members
452,373
Latest member
TimReeks

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