how to combine only specific files from the list

Summer7sun

New Member
Joined
Sep 14, 2017
Messages
33
I have two folders on my "desktop" : Folder 1 named : "Today" and Folder 2 named : "Destination"
In folder named "Destination" I have multiple files : A.xlsx, B.xlsx, C.xlsx with same headers.
In folder 1 have the latest sale data and I want to combine the latest data with the destination files that is A.xlsx to the A.xlsx from destination folder and b.xlsx to b.xlsx.
I have file named "Master" and have the list of file name from destination From A7:A50
I want to combine the files from the list only. Please help
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
For clarification: The files in both folders have the same name. Is this correct? Do you want to add the data from each file in "Today" to the corresponding file in"Destination"? Is the data to be added in one sheet or multiple sheets? What are the sheet names in the source files and destination files? Do the file names in the Master file (A7:A50) include the file extension (xlsx)? What is the name of the sheet containing these file names in the "Master"? Sorry for all these questions but they are necessary to get an exact understanding.
 
Upvote 0
Hi! Mumps,
1. Files have same name in both the folders.
2. Files in "Today" folder are latest , Eg- A.xlsx has the latest data and I want the latest data combined or copied and placed in Folder "Destination" file with the A.xlsx in a new line without the header as the header is already in A.xlsx in Destination folder, I want to keep adding latest every day. That is everyday i would have the older data combined with latest.
3. Sheet names are same as the file name .. eg apple.xlsx has the sheet name as apple.
4. I can add the extension if needed ... as of now its just the name eg. apple mango etc
5. Master.xlsx has 1 sheet and the sheet name is Master and the list starts from a7 ... it would be great to have the code to run till a blank cell...


I like your questions mumps ... nothing to be sorry about :)
 
Upvote 0
Place this macro in a regular module in the Master workbook and save the workbook as a macro-enabled file. This will change its extension to "xlsm". Run the macro from there.
Code:
Sub CopyData()
    Application.ScreenUpdating = False
    Dim fName As Range
    Dim Master As Worksheet
    Set Master = ThisWorkbook.Sheets("Master")
    Dim wkbDest As Workbook
    Dim wkbSource As Workbook
    For Each fName In Master.Range("A7:A50")
        Set wkbSource = Workbooks.Open(CreateObject("WScript.Shell").specialfolders("Desktop") & "\Today\" & fName & ".xlsx")
        Sheets(fName.Value).Range("A2", Sheets(fName.Value).Cells.SpecialCells(xlCellTypeLastCell)).Copy
        wkbSource.Close False
        Set wkbDest = Workbooks.Open(CreateObject("WScript.Shell").specialfolders("Desktop") & "\Destination\" & fName & ".xlsx")
        Sheets(fName.Value).Cells(Sheets(fName.Value).Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial
        wkbDest.Close True
    Next fName
    Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
thanks for the code however i am getting error .... 1st error there is large file on clipboard and the next error on this line
Code:
Sheets(fName.Value).Cells(Sheets(fName.Value).Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial
 
Upvote 0
I tried the macro on some dummy sheets and it worked properly. It is difficult to suggest a working macro when we can't use the actual files. If you received a message that the clipboard contains a large amount of data, that is not an error. What was the error you received on the line you mentioned?
 
Upvote 0
I am getting the clipboard popup ... i dont know what to say to that if not error .. LOL... and when i click no then i get an error saying pastespecial method of range class failed with an error code 1004.... I suppose as I have the same file name and excel cant open same file name at once ... so the code needs to copy close then open and paste... I dont know exactly I think may be for this m getting the error
 
Upvote 0
When you got the message that the clipboard contains a large amount of data, did you click "Yes" or "No" when you were asked if you wanted to keep the data?
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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