wfcrr
asked on
How to combine or merge 50 excel spreadsheets
I have approximately 50 different excel files I need to turn into one file. Is there a way to do that? I am not sure if I should be saying merge, or combine, or convert...what I need to do is have all the data that is in 50 separate excel files somehow now be in one excel file. If I can't find a method for doing that, I will simply copy/paste from each file into one very large file. However, I am thinking you guys probably have a formula or method for doing that?
Here's some code that I have used for years.
Option Explicit
'---------------------------------------------------------------------------------------
' Module : Data
' Author : Roy Cox (royUK)
' Website : for more examples and Excel Consulting
' Date : 19/11/2011
' Purpose : Combine data from several workbooks
' Disclaimer: Disclaimer; This code is offered as is with no guarantees. You may use it in your
' projects but please leave this header intact.
'---------------------------------------------------------------------------------------
Sub CombineData()
Dim oWbk As Workbook
Dim rRng As Range
Dim rToCopy As Range
Dim rNextCl As Range
Dim lCount As Long
Dim bHeaders As Boolean
Dim sFil As String
Dim sPath As String
With Application
.ScreenUpdating = False
.DisplayAlerts = False
.EnableEvents = False
' On Error GoTo exithandler
' assumes workbooks are in a sub folder named "Data"
sPath = ThisWorkbook.Path & Application.PathSeparator & "Data"
ChDir sPath
sFil = Dir("*.xl**") 'file type
Do While sFil <> "" 'will start LOOP until all files in folder sPath have been looped through
With ThisWorkbook.Worksheets(1)
Set rRng = .Range("A1").CurrentRegion
If rRng.Cells.Count = 0 Then
'no data in master sheet
bHeaders = False
Else: bHeaders = True
End If
Set oWbk = Workbooks.Open(sPath & Application.PathSeparator & sFil) 'opens the file
'A1 must be within the data, if not amend the Range below
Set rToCopy = oWbk.ActiveSheet.Range("A1").CurrentRegion
If Not bHeaders Then
Set rNextCl = .Cells(1, 1)
bHeaders = True
Else: Set rNextCl = .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0)
'headers exist so don't copy
Set rToCopy = rToCopy.Offset(1, 0).Resize(rToCopy.Rows.Count - 1, _
rToCopy.Columns.Count)
End If
rToCopy.Copy rNextCl
End With
oWbk.Close False 'close source workbook
sFil = Dir
Loop ' End of LOOP
'sort to remove empty rows
Set rRng = ThisWorkbook.Worksheets(1).UsedRange
rRng.Sort Key1:=.Range("A2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
exithandler:
.ScreenUpdating = True
.DisplayAlerts = True
.EnableEvents = True
End With
End Sub
ASKER
@Neeraj, I can't open the file. Says it is not a valid format or extension? I am using Excel 2007...
@Roy, can you sort of idiot proof some instruction on how to use that code? I really am not much of a tech guy...but I can follow 2nd grader instructions.
@Roy, can you sort of idiot proof some instruction on how to use that code? I really am not much of a tech guy...but I can follow 2nd grader instructions.
Which version of Excel are you using? I can provide you with a suitable workbook with the code in if you let me know which version.
The code that I have posted will work in any version of Excel.
It requires the source workbooks to be in a sub folder called Data within the folder that contains the workbook with the code in.
The code that I have posted will work in any version of Excel.
It requires the source workbooks to be in a sub folder called Data within the folder that contains the workbook with the code in.
ASKER
Thanks Roy, I am using Excel 2007. If you could idiot proof it, that would be awesome!
Do you have an example of the data and the resulting merge?
ASKER
I attached a file, for example.
Merged-really-was-just-copy-paste.xls
Merged-really-was-just-copy-paste.xls
Merged-really-was-just-copy-paste.xls
Merged-really-was-just-copy-paste.xls
@Neeraj, I can't open the file. Says it is not a valid format or extension? I am using Excel 2007
That's a temporary bug here. I also have the same issue at my end and report this bug to tech team and hopefully it will resolved soon.
To deal with this, download and SAVE this workbook on your system and then open it.
The workbook attached belongs to the Excel 2013,
Another option is, you can just copy the code into a blank workbook's standard module and run the code from there.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
@Roy....Is the file I uploaded downloading as an Excel file? I uploaded the Excel file, but when I tried to download it just now, it keeps coming in as a Word doc. Were you able to download it as an Excel file?
@wfcrr
I tried downloading one of your sample workbook and it was opened in Excel application. So no issue at my end. That seems to be another bug. Lol
I tried downloading one of your sample workbook and it was opened in Excel application. So no issue at my end. That seems to be another bug. Lol
ASKER
@Neeraj, I was able to open the file you uploaded, but when I hit the button "Combine Files" and navigate to the folder that has the excel files, nothing in the folder if viewable. All the excel files in the folder are excel 2007 files, but, none of them appear in the folder when I navigate using the "Combine Files" button...what should I do?
While navigating to the source folder, you don't need to open or double click the folder instead you just select the source folder and click on Confirm to proceed.
Remember it's a folder picker dialog box not the file picker.
Remember it's a folder picker dialog box not the file picker.
I'm just downloading your files. They are opening OK for me. Why are you using.xls if your version is Excel 2007?
I need to know if you are using .xls or .xlsm.
ASKER
@Roy, I don't know why I'm using .xls. What should I be doing? I just saved the file...
Also, I have to leave the office, can you upload the file you want me to use? I'll plan to look at it either tomorrow, or for sure on Monday morning. Will you be around on Monday?
Also, I have to leave the office, can you upload the file you want me to use? I'll plan to look at it either tomorrow, or for sure on Monday morning. Will you be around on Monday?
I have added the code to workbook based on your downloads.
Basically the code will import data from a sub folder named Data which stores the data files. This folde should be in the main folder that houses the master workbook.
To run the code there is a new section in the right of the Data Tab of the Ribbon named Merge Files with a button to run the code.
As you won't be testing immediately I'm going to add some features and attach the example soon.
Basically the code will import data from a sub folder named Data which stores the data files. This folde should be in the main folder that houses the master workbook.
To run the code there is a new section in the right of the Data Tab of the Ribbon named Merge Files with a button to run the code.
As you won't be testing immediately I'm going to add some features and attach the example soon.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Pleased to help
In the attached file, you will find a button on Sheet1 called Combine Files, you may click this button to run the code. The code will prompt you to select the folder where all your excel files are saved.
Open in new window
CombineFilesIntoOneFile.xlsm