Copying Worksheets from another Workbook To existing Worksheets in Destination Workbook

sspatriots

Well-known Member
Joined
Nov 22, 2011
Messages
572
Office Version
  1. 365
Platform
  1. Windows
Good morning,

I have this code that will copy a worksheet from a source workbook to an existing worksheet my destination workbook. Looking for some assistance on making this same code work to copy several worksheets from the source workbook to existing worksheets in my destination workbook without having to close the source workbook each time between copying each worksheet over to the destination workbook.

This is the code that I have now for copying just the one worksheet over:

VBA Code:
Sub Update_AllWSData()

Dim WB As Workbook
Dim DestSht As Worksheet
Dim SourceSht As Worksheet

Application.ScreenUpdating = False
Application.DisplayAlerts = False

Set DestSht = ThisWorkbook.Worksheets("WIP")
Set WB = Workbooks.Open(Filename:="G:\CompanyName\Manufacturing Detail Schedule1.xlsx")  'Selected File full path
Set SourceSht = WB.Worksheets("WIP")

    lastRow = SourceSht.Cells(Rows.Count, "B").End(xlUp).Row
    
    SourceSht.Range("B1:V" & lastRow).Copy
    
    WB.Close

    Worksheets("WIP").Select
    
    DestSht.Range("A1").Select
    ActiveSheet.PasteSpecial Format:="Unicode Text", Link:=False, _
        DisplayAsIcon:=False, NoHTMLFormatting:=True
    Range("A2").Select

    Worksheets("Jobs").Select

Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Perhaps something like this.
VBA Code:
Sub Update_AllWSData()
    
    Dim WB As Workbook
    Dim DestSht As Worksheet
    Dim SourceSht As Worksheet
    Dim lastRow As Long
    
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    Set WB = Workbooks.Open(Filename:="G:\CompanyName\Manufacturing Detail Schedule1.xlsx")  'Selected File full path
    ThisWorkbook.Activate
    For Each SourceSht In WB.Worksheets
        Select Case SourceSht.Name
            Case "WIP", "WIP_AUG", "WIP_SEP", "WIP_OCT"  '<-names of worksheets you want to copy data from.
                With SourceSht
                    lastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
                    SourceSht.Range("B1:V" & lastRow).Copy
                End With
                
                Set DestSht = ThisWorkbook.Worksheets(SourceSht.Name)
                With DestSht
                    .Activate
                    .Range("A1").Select
                    .PasteSpecial Format:="Unicode Text", Link:=False, DisplayAsIcon:=False, NoHTMLFormatting:=True
                End With
        End Select
    Next SourceSht
    ThisWorkbook.Worksheets("Jobs").Select
    WB.Close False
    
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Perhaps something like this.
VBA Code:
Sub Update_AllWSData()
   
    Dim WB As Workbook
    Dim DestSht As Worksheet
    Dim SourceSht As Worksheet
    Dim lastRow As Long
   
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
   
    Set WB = Workbooks.Open(Filename:="G:\CompanyName\Manufacturing Detail Schedule1.xlsx")  'Selected File full path
    ThisWorkbook.Activate
    For Each SourceSht In WB.Worksheets
        Select Case SourceSht.Name
            Case "WIP", "WIP_AUG", "WIP_SEP", "WIP_OCT"  '<-names of worksheets you want to copy data from.
                With SourceSht
                    lastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
                    SourceSht.Range("B1:V" & lastRow).Copy
                End With
               
                Set DestSht = ThisWorkbook.Worksheets(SourceSht.Name)
                With DestSht
                    .Activate
                    .Range("A1").Select
                    .PasteSpecial Format:="Unicode Text", Link:=False, DisplayAsIcon:=False, NoHTMLFormatting:=True
                End With
        End Select
    Next SourceSht
    ThisWorkbook.Worksheets("Jobs").Select
    WB.Close False
   
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub
Thank you, I will give this a go and let you know how it works out. Much appreciated.
 
Upvote 0
Perhaps something like this.
VBA Code:
Sub Update_AllWSData()
   
    Dim WB As Workbook
    Dim DestSht As Worksheet
    Dim SourceSht As Worksheet
    Dim lastRow As Long
   
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
   
    Set WB = Workbooks.Open(Filename:="G:\CompanyName\Manufacturing Detail Schedule1.xlsx")  'Selected File full path
    ThisWorkbook.Activate
    For Each SourceSht In WB.Worksheets
        Select Case SourceSht.Name
            Case "WIP", "WIP_AUG", "WIP_SEP", "WIP_OCT"  '<-names of worksheets you want to copy data from.
                With SourceSht
                    lastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
                    SourceSht.Range("B1:V" & lastRow).Copy
                End With
               
                Set DestSht = ThisWorkbook.Worksheets(SourceSht.Name)
                With DestSht
                    .Activate
                    .Range("A1").Select
                    .PasteSpecial Format:="Unicode Text", Link:=False, DisplayAsIcon:=False, NoHTMLFormatting:=True
                End With
        End Select
    Next SourceSht
    ThisWorkbook.Worksheets("Jobs").Select
    WB.Close False
   
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub
I seem to be getting an error message at the line that says ".PasteSpecial Format:=.....". The message is "Method 'PasteSpecial' of object '_Worksheet' failed". Is it not capturing what was copied in the Source workbook maybe?
 
Upvote 0
I just changed the following:

VBA Code:
                    .Range("A1").Select
                    .PasteSpecial Format:="Unicode Text", Link:=False, DisplayAsIcon:=False, NoHTMLFormatting:=True


To this:
VBA Code:
                    .Range("A1").PasteSpecial Paste:=xlPasteValues

This seems to be working very well. Thanks so much for your help.
 
Upvote 0

Forum statistics

Threads
1,215,084
Messages
6,123,028
Members
449,092
Latest member
ikke

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