Copy all sheet contents from selected workbook and paste into current workbook sheets of the same name

TurpsUK

New Member
Joined
Apr 6, 2021
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hello there,
I am relatively new to excel VBA, so any help here much appreciated!

I have a source processing workbook with defined sheet names "X", "Y", "Z" etc . They are empty of data before the Macro is executed. I want to pull in all the data content from another workbook that have the same sheet names using copy paste values. So, using a file picker open the target file (which has been extracted from an internal database as an xlxs. into downloads folder) and copy each sheet range content from "A1: x" into the workbook with the same names.
I have managed to develop a Macro (below) that copies the sheets in, which works okay. But the problem is that in the processing workbook, there are a series of nested formula's that will use the data introduced, and therefore it creates multiple formula Reference errors. I'm hoping CopyPaste Values from the specific ranges will allow relative references and sheet names to be maintained, making the forward work much easier.

Kind Regards, Turps

Sub Select_file()

Dim FilePicker As FileDialog
ChDir "C:\"
Dim b1 As Workbook
Dim ws As Worksheet
Set b1 = ActiveWorkbook
Set FilePicker = Application.FileDialog(msoFileDialogFilePicker)
With FilePicker
.Title = "Please select your Watson Data Download File to Copy"
.AllowMultiSelect = False
.ButtonName = "Confirm"
If .Show = -1 Then
mypath = .SelectedItems(1)
Else
End
End If
End With

Workbooks.Open Filename:=mypath
For Each ws In ActiveWorkbook.Sheets
ws.Copy after:=b1.Sheets(b1.Sheets.Count)
Next ws
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
Try this:
VBA Code:
Public Sub Copy_Workbook_Sheets_Values_To_Same_Sheets()

    Dim FilePicker As FileDialog
    Dim mainWb As Workbook, targetWb As Workbook
    Dim targetFile As String
    Dim ws As Worksheet
    
    Set FilePicker = Application.FileDialog(msoFileDialogFilePicker)
    With FilePicker
        .Title = "Please select your Watson Data Download File to Copy"
        .AllowMultiSelect = False
        If .Show Then
            targetFile = .SelectedItems(1)
        Else
            Exit Sub
        End If
    End With
    
    Set mainWb = ActiveWorkbook
    
    Application.ScreenUpdating = False
    
    Set targetWb = Workbooks.Open(targetFile)
    For Each ws In targetWb.Worksheets
        ws.Range("A1").CurrentRegion.Copy
        mainWb.Worksheets(ws.Name).Range("A1").PasteSpecial xlPasteValuesAndNumberFormats
    Next
    Application.CutCopyMode = False
    targetWb.Close SaveChanges:=False
    
    Application.ScreenUpdating = True
    
    MsgBox "Finished"
    
End Sub
 
Upvote 1
Solution
Hi John,
Thank you very much for the code. It worked fantastically well - very quick!
The only issue I had to work through were a few spelling mismatches on sheet names from source to target.

Best Regards

Paul
 
Upvote 0

Forum statistics

Threads
1,226,113
Messages
6,189,045
Members
453,521
Latest member
Chris_Hed

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