Pasting from one workbook with multiple sheets to various workbooks

motopdx

New Member
Joined
Feb 24, 2020
Messages
13
Office Version
  1. 365
Platform
  1. Windows
I'm trying to paste from one workbook with multiple sheets to several different workbooks. Example: Workbook "X" has 50 sheets labeled 1-50. I need each sheet copied to the respective workbook "Y". Each sheet in workbook "X" can have a different range of active cells that need to be copied over.

Examples:
workbook "X" sheet "1" copied to workbook "Y" sheet "Jan" (cell A2 to cell Z50)
workbook "X" sheet "2" copied to workbook "Y" sheet "Jan" (cell A2 to cell Z125)

Here is what I was trying with no luck and probably not even close.

Sub Data_Feb()
Dim x As Workbook
Dim y As Workbook

Set x = Workbooks.Open("workbook file path")
Set y = Workbooks.Open("workbook file path")

'Copy from workbook "X" & specific sheet
x.Sheets("584").Range ("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy

'Paste to workbook "Y"
y.Sheets("Feb").Range("A2").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

'Close & Save workbooks
x.Close
y.Save
y.Close


1582572250269.png
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Each sheet in worksheet X will be copied to a different workbook. i.e sheet 584 will be copied into workbook 584; sheet 589 will be copied into workbook 589 and so forth.
 
Upvote 0
Each sheet in worksheet X will be copied to a different workbook. i.e sheet 584 will be copied into workbook 584; sheet 589 will be copied into workbook 589 and so forth.
it seems you are saying different things... why is workbook Y have months as sheet names unlike Workbook X that has figures as sheet names?
 
Upvote 0
Workbook X relates to business locations which is represented by numbers. Each business location have their own workbook and within their workbook is each month to track data by month.
 
Upvote 0
Ok.
1. does the business location workbooks have the same file names as the sheets names in workbook X?
2. Are you copying the whole sheet content to "Jan" sheet in workbook Y alone?
 
Upvote 0
Yes, all the sheet names in workbook X have the same names as workbook Y. However, I run this file each month to populate the new data and is why there are the 12 months in workbook Y

At the end of the year I will have 12 workbooks X but only 1 workbook per branch which include the data from all 12 months. Sorry for the sloppy screenshot but hopefully it helps.


1582747674902.png

1582747724394.png




1582747137898.png

1582747610454.png
 

Attachments

  • 1582747569768.png
    1582747569768.png
    24.6 KB · Views: 1
Upvote 0
try this on a sample data
VBA Code:
Sub copySheetData()
Dim w1 As Workbook, w2 As Workbook, sel As Boolean, folder$, fol As FileDialog, ws As Worksheet
Dim sourceD As String, sMonth As String, adDr As Variant
If MsgBox("Please, Click Yes to choose the source file", vbYesNo) = vbYes Then

    With Application.FileDialog(msoFileDialogFilePicker)
        ' show the file picker dialog box
        If .Show <> 0 Then sourceD = .SelectedItems(1)
    End With
Else
Exit Sub
End If

MsgBox "Please, select the folder of the Business Location"
Set fol = Application.FileDialog(msoFileDialogFolderPicker)
fol.AllowMultiSelect = False
sel = fol.Show
If sel Then folder = fol.SelectedItems(1) & "\" Else Exit Sub
sMonth = InputBox("Input the month")
Set w1 = Workbooks.Open(sourceD)
For Each ws In w1.Worksheets
    If FileThere(folder & ws.Name & " " & Format(Date, "yyyy") & ".xlsx") Then
    Set w2 = Workbooks.Open(folder & ws.Name & " " & Format(Date, "yyyy"))
    a = Split(ws.UsedRange.Address, "$")
        For i = 0 To UBound(a)
        If adDr = "" Then adDr = a(i) Else adDr = adDr & a(i)
        Next i
        On Error GoTo nFound
    w2.Worksheets(sMonth).Range(adDr).Value = ws.UsedRange.Value
    w2.Save
    w2.Close
    n = n + 1
    End If
    adDr = ""
Next ws
w1.Close
MsgBox IIf(n > 0, n & " files copied", "Files not found")
MsgBox n & " files copied"
Exit Sub
nFound:
MsgBox "Month not found"
End Sub
Function FileThere(filename As String) As Boolean
     FileThere = (Dir(filename) > "")
End Function
 
Upvote 0
The above macro didn't work but it also didn't pop-up an error. The macro is more complicated than I understand so here is some additional screen shots below.

This is the source data with a limited # of tabs (5). I think the full file has about 75 tabs. I would copy the data from A1 to W1 and then use to copy all the data in the tab.

x.Sheets("8").Range ("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select

1582821740625.png


This is the destination folder with the files label as the tab names from the source data.
1582821883948.png


This is what the destination files looks like with the 12 months.
1582821788809.png


Lets say the source data file is Jan and we want to paste values into cell A1 on the Jan tab for each respective workbook

Also I tried to upload the workbooks but it didn't let me with the file extensions.

Thank you for all the help.
 
Upvote 0
I thought the year "2020" was added to file names e.g. 64 2020.xlsx, 56 2020.xlsx.
Because if the file names are not in that format, it won't work
 
Upvote 0

Forum statistics

Threads
1,215,262
Messages
6,123,939
Members
449,134
Latest member
NickWBA

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