COMBINE MULTIPLE EXCEL FILES WITH BOTH MATCHING AND DIFFERING FIELDS IN ONE MASTER WORKBOOK
See this how to video posted to my YouTube channel, and the below instructions taken from the video.
Collect all of your Excel files in a single folder. In this sample set we can see that while some of the columns in the spreadsheets have the same headings, many are unique to the individual Excel files.
Create a new Access database. Enter Visual Basic by pressing ALT + F11
We'll be using three macros which I have already inserted in separate modules in Visual Basic. To create a new module just right click on the left on the Modules folder and select Insert . . . Module
The first module will import the source Excel files. The vba code was created by Ken Snell and is available on the Tip of the Night for October 5, 2017. See www.litigationsupporttipofthenight.com Be sure to set the line beginning blnHasFieldNames = to True not False, to import the first row of the Excel files as the column headings.
Enter the path to the folder containing the Excel files on the line beginning: strPath =
Press the play button and the vba code should import the Excel files as separate tables into the Access database.
Now we want to get a list of all of the fields in each of table. Go back to visual basic and use the vba code I have inserted in the second module, which was created by Ken Puls and was posted as the Tip of the Night for October 6, 2017.
It should not be necessary to edit this vba code. Press play and an Excel spreadsheet will be generated which lists the table names in column A and the field names in column B.
Now we'll de-dupe the field names and use the transpose paste option in Excel to put them in the first row of a worksheet.
Note that the vba code we just used to generate the table field names has a flaw. It does not pick up the names of the first field in each table. In this example only the first field in the Parks table is omitted, and there is overlap between the tables which accounts for the first field for the other tables.
Add the missing field names to the worksheet.
In the second row we want each cell to have more than 255 characters. This will prompt Access to make each field a Long Text field when the field names are imported.
Use the LEN formula in Excel to confirm you have the right number of characters. Then select cell A2, then select the cells to the right and press CTRL + R to copy the data to all of the other cells on the second row.
Next we'll go to the External Data tab of the Access ribbon and select New Data Source . . . From File . . .Excel to import the fields into a new table.
In the Import Wizard be sure to check off the box making the data in the first row the column headings of the Access table.
The third and final vba code will insert the source tables one by one into the new master table. This code is available in the MS Office Dev Center and was posted as the Tip of the Night for October 8, 2017
First we need to enter the path to the Access database we're working in.
On the line containing the SQL command INSERT INTO enter the name of the master table. Then put the name of the source table following FROM in parentheses. It's necessary to run this macro once for each source table.
The data has been added for the first source table - Batting.
As you can see, where the tables had a field in common, the data from all sources is now in a single field.