get data from multiple files for the same sheet in one folder

Abdo

Board Regular
Joined
May 16, 2022
Messages
228
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Hello

I need macro ot brings data from multiple files are existed in this directory "C:\Users\PC LP\Downloads\ Sales Report \" for the same sheet name" REPORT" for each file .

so my data in all of the files are different in locations . this means should match based on header even there are many duplicates data also should brings all of data from all of the files . the headers in file combined are A1: H1 in sheet REPORT where run the macro from it if they are matched with headers for the others files then should brings all of the files , if the headers are not matched , then should show message box and tell me what's the headers are not existed by indicate headers names in file combined . and every time run the macro should overwriting data by clearing data before brings them .
the files in the folder are xlsx and somtimes xlsm .

thank
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Macro (n), Power Query (y)! Power Query is designed for just this kind of situation. It can pull in data from multiple files in multiple sheets and be transformed as needed - in most cases using just the UI and maybe using the UI to set up a manual tweak. It will save you HOURS of time, particularly over VBA. Great YouTube playlists on it here and here.

When posting, please use XL2BB for data, and the board code formatters (XLS, VBA, M (Power Query), etc.).
 
Upvote 0
actually i search for vba . I no know why you don't prefer vba

ok here is my data maybe somebody help .

file 1
sdf.xlsm
ABCDEFGH
1ITEMCODEDESCIBEREFDEL.NQTY1QTY2CASE
21CMTR1BIG-DTR1RE4DL-1245200
32CMTR2BIG-DTR2RE5DL-1367300
43CMTR3BIG-DTR3RE6DL-1487400
54CMTR4BIG-DTR4RE7DL-1545500
65CMTR5BIG-DTR5RE8DL-16340
76CMTR6BIG-DTR6RE9DL-173456600
87CMTR7BIG-DTR7RE10DL-1876812
98CMTR8BIG-DTR8RE11DL-1989700
109CMTR9BIG-DTR9RE12DL-20864800
1110CMTR10BIG-DTR10RE13DL-21235900
1211CMTR11BIG-DTR11RE14DL-22671000
1312CMTR12BIG-DTR12RE15DL-235431100
1413CMTR13BIG-DTR13RE16DL-247891200
1514CMTR14BIG-DTR14RE17DL-254571300
1615CMTR15BIG-DTR15RE18DL-263461400
REPORT



file 2
dfr.xlsx
EFGHIJKL
1ITEMCODEDESCIBEREFDEL.NQTY1QTY2CASE
21CMTR11BIG-DTR11RE41DL-121457676
32CMTR21BIG-DTR21RE51DL-136789
43CMTR3BIG-DTR3RE6DL-1487346
54CMTR4BIG-DTR4RE7DL-153487
65CMTR5BIG-DTR5RE8DL-1634178
76CMTR6BIG-DTR6RE9DL-178907890
87CMTR7BIG-DTR7RE10DL-1890433
98CMTR8BIG-DTR8RE11DL-19876700
109CMTR9BIG-DTR9RE12DL-2045800
1110CMTR10BIG-DTR10RE13DL-2167900
1212CMTR12BIG-DTR12RE15DL-23981100
1313CMTR13BIG-DTR13RE16DL-247688
1414CMTR14BIG-DTR14RE17DL-255490
1515CMTR15BIG-DTR15RE18DL-266787
REPORT



file master before

master.xlsm
ABCDEFGH
1ITEMCODEDESCIBEREFDEL.NQTY1QTY2CASE
2
3
4
5
6
7
8
9
10
11
12
13
14
15
REPORT


the result in file master
master.xlsm
ABCDEFGH
1ITEMCODEDESCIBEREFDEL.NQTY1QTY2CASE
21CMTR1BIG-DTR1RE4DL-1245200
32CMTR2BIG-DTR2RE5DL-1367300
43CMTR3BIG-DTR3RE6DL-1487400
54CMTR4BIG-DTR4RE7DL-1545500
65CMTR5BIG-DTR5RE8DL-16340
76CMTR6BIG-DTR6RE9DL-173456600
87CMTR7BIG-DTR7RE10DL-1876812
98CMTR8BIG-DTR8RE11DL-1989700
109CMTR9BIG-DTR9RE12DL-20864800
1110CMTR10BIG-DTR10RE13DL-21235900
1211CMTR11BIG-DTR11RE14DL-22671000
1312CMTR12BIG-DTR12RE15DL-235431100
1413CMTR13BIG-DTR13RE16DL-247891200
1514CMTR14BIG-DTR14RE17DL-254571300
1615CMTR15BIG-DTR15RE18DL-263461400
1716CMTR11BIG-DTR11RE41DL-121457676
1817CMTR21BIG-DTR21RE51DL-136789
1918CMTR3BIG-DTR3RE6DL-1487346
2019CMTR4BIG-DTR4RE7DL-153487
2120CMTR5BIG-DTR5RE8DL-1634178
2221CMTR6BIG-DTR6RE9DL-178907890
2322CMTR7BIG-DTR7RE10DL-1890433
2423CMTR8BIG-DTR8RE11DL-19876700
2524CMTR9BIG-DTR9RE12DL-2045800
2625CMTR10BIG-DTR10RE13DL-2167900
2726CMTR12BIG-DTR12RE15DL-23981100
2827CMTR13BIG-DTR13RE16DL-247688
2928CMTR14BIG-DTR14RE17DL-255490
3029CMTR15BIG-DTR15RE18DL-266787
REPORT
 
Upvote 0
So many reasons to not like VBA. Aside from that it's verbose, has a terrible US, and difficult to code to get anything accomplished, its roots are in the earliest version of scripting for Office apps - before they were "Office" apps and in second place at best to the leaders in their fields - Lotus 123 for Spreadsheets, Word Perfect for Documents, and one whose name I can't remember for presentations.
Before I was forced into retirement due to COVID layoffs (obviously I've been doing this for a LONG time!) I was pulling data from a Mobile Device database with over 350K records and cross referencing that with data from four carriers - all in different layouts and formats. To reconcile one carrier could take more than a day. Then I discovered Power Query and had just started using it before I was let go. Had I learned even a bit of it sooner, I probably could have cut that down to less than an hour with only a little bit of PQ knowledge. That cannot be done with VBA.
I'll take a look at what you've posted (thanks for using XL2BB!) and see if I can help further.
 
Upvote 0
So here's what PQ did in 21 minutes from scratch:
Book1
ABCDEFGHI
1NameITEMCODEDESCIBEREFDEL.NQTY1QTY2CASE
2file 11CMTR1BIG-DTR1RE4DL-1245200
3file 12CMTR2BIG-DTR2RE5DL-1367300
4file 13CMTR3BIG-DTR3RE6DL-1487400
5file 14CMTR4BIG-DTR4RE7DL-1545500
6file 15CMTR5BIG-DTR5RE8DL-16340
7file 16CMTR6BIG-DTR6RE9DL-173456600
8file 17CMTR7BIG-DTR7RE10DL-1876812
9file 18CMTR8BIG-DTR8RE11DL-1989700
10file 19CMTR9BIG-DTR9RE12DL-20864800
11file 110CMTR10BIG-DTR10RE13DL-21235900
12file 111CMTR11BIG-DTR11RE14DL-22671000
13file 112CMTR12BIG-DTR12RE15DL-235431100
14file 113CMTR13BIG-DTR13RE16DL-247891200
15file 114CMTR14BIG-DTR14RE17DL-254571300
16file 115CMTR15BIG-DTR15RE18DL-263461400
17file 21CMTR11BIG-DTR11RE41DL-121457676
18file 22CMTR21BIG-DTR21RE51DL-136789
19file 23CMTR3BIG-DTR3RE6DL-1487346
20file 24CMTR4BIG-DTR4RE7DL-153487
21file 25CMTR5BIG-DTR5RE8DL-1634178
22file 26CMTR6BIG-DTR6RE9DL-178907890
23file 27CMTR7BIG-DTR7RE10DL-1890433
24file 28CMTR8BIG-DTR8RE11DL-19876700
25file 29CMTR9BIG-DTR9RE12DL-2045800
26file 210CMTR10BIG-DTR10RE13DL-2167900
27file 212CMTR12BIG-DTR12RE15DL-23981100
28file 213CMTR13BIG-DTR13RE16DL-247688
29file 214CMTR14BIG-DTR14RE17DL-255490
30file 215CMTR15BIG-DTR15RE18DL-266787
Sales Report

I got a little lost and forgot how to not delete the source file's name (the Name column after removing ".xlsx" from it). Once I remembered, I just had to select all columns and use Detect Data Type from the Transform tab.
Here's the actual code:
Power Query:
let
    Source = Folder.Files("C:\PC LP\Downloads\Sales Report"),
    LowercasedText = Table.TransformColumns(Source,{{"Extension", Text.Lower, type text}}),
    FilteredRows = Table.SelectRows(LowercasedText, each [Extension] = ".xlsx"),
    ReplacedDotXLSX = Table.ReplaceValue(FilteredRows,".xlsx","",Replacer.ReplaceText,{"Name"}),
    #"Filtered Hidden Files1" = Table.SelectRows(ReplacedDotXLSX, each [Attributes]?[Hidden]? <> true),
    #"Invoke Custom Function1" = Table.AddColumn(#"Filtered Hidden Files1", "Transform File", each #"Transform File"([Content])),
    #"Removed Other Columns1" = Table.SelectColumns(#"Invoke Custom Function1",{"Name", "Transform File"}),
    #"Expanded Table Column1" = Table.ExpandTableColumn(#"Removed Other Columns1", "Transform File", Table.ColumnNames(#"Transform File"(#"Sample File"))),
    ChangedType = Table.TransformColumnTypes(#"Expanded Table Column1",{{"ITEM", Int64.Type}, {"CODE", type text}, {"DESCIBE", type text}, {"REF", type text}, {"DEL.N", type text}, {"QTY1", Int64.Type}, {"QTY2", Int64.Type}, {"CASE", type any}})
in
    ChangedType
The first step is automatic - pulls in all the files from the specified folder. The next 3 steps are my cautionary steps to make sure I'm only getting the files I want and removed ".xlsx" from the File name. PQ automatically generated steps 5-8, and I manually did the ChangedType step. Additionally PQ generated a Parameter1 query, created a Sample File query, created a Transform File function, and created a Transform Sample File query (which is where changes to all files pulled in can be done).
I did run into one problem, however. Initially I named the Table in file 2 Table2, but that wasn't working because the function was only looking for Table1. I changed the name of the table and it worked fine. I could have changed the query to pull in the Worksheet itself, but that would have been locked to Sheet1. It would have been nearly identical and decided to take the cleaner route.
And it really took 21 minutes - that's when it said I posted my first reply when I started this one. In fact that includes time to create the folder structure and the two files! I suspect it took longer to make this post!
 
Upvote 0
thanks I tried implementing on my files but gives error for file name despite I using copy file name and paste to avoid error . I no know what's wrong !!!
 
Upvote 0
thanks I tried implementing on my files but gives error for file name despite I using copy file name and paste to avoid error . I no know what's wrong !!!
I used the file path you had mentioned, so that shouldn't be a problem, and the query pulls in all the files, so filename isn't it either.
Edit the query, and when it opens an error message should appear at the top and a button to Go to Error. Click that, and if you don't understand it, let me know what step the problem is in.
 
Upvote 0
nd when it opens an error message should appear at the top and a button to Go to Error
I did it and inform me the file name is not available ,despite of I make sure is existed in the folder !
 
Upvote 0
I did it and inform me the file name is not available ,despite of I make sure is existed in the folder !
The Query is to pull in ALL files in the folder - and subfolders, not just one. The first step presents you will ALL the files in the folder.
1670930902110.png

From there, you can filter both the Name and Extension columns to get only the files you need. I don't understand how you're getting an error that the FILENAME is not available. In the screenshot above, if you remove all the columns except Content and expand it, you'll be asked what element (Sheet or Table) you want to use.
1670931258736.png

This is where a problem can arise. If all the files don't have a table named Table1, those files will not combine. If they all have Sheet1, you can use that instead. The main difference is that with the table headers are set up properly. Using the Sheet, headers won't be promoted, but that's easy enough to clean up.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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