Merging same format table from several spreadsheets into one

Jaffabfc

Board Regular
Joined
Jul 5, 2013
Messages
230
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have in a folder around 2-300 spreadsheets that i am wanting to merge into one.
They are all setup with the exact same titles but i am wanting to merge them all into one.

With an added column.
So at the moment cells B2:Q2 are all the exact same in every spreadsheet, i am wanting to merge them all but an added column (maybe A starting at A2) and calling it the name of the spreadsheet its been pulled from.

If this possible? ideally without a bit of SQL, but if it has to be then thats fine.

Thanks
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Try this macro (untested). Change the folder path (in red) to suit your needs.
Rich (BB code):
Sub CopyRange()
    Application.ScreenUpdating = False
    Dim desWS As Worksheet, srcWB As Workbook, LastRow As Long
    Set desWS = ThisWorkbook.Sheets(1)
    Const strPath As String = "C:\Test\" 
    ChDir strPath
    strExtension = Dir(strPath & "*.xlsx")
    Do While strExtension <> ""
        Set srcWB = Workbooks.Open(strPath & strExtension)
        With srcWB.Sheets(1)
            LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            .Range("B2:Q" & LastRow).Copy desWS.Cells(desWS.Rows.Count, "B").End(xlUp).Offset(1, 0)
            desWS.Cells(desWS.Rows.Count, "A").End(xlUp).Offset(1, 0).Resize(LastRow - 1) = srcWB.Name
        End With
        srcWB.Close savechanges:=False
        strExtension = Dir
    Loop
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi,
Thanks for this.
I created a brand new spreadsheet that was blank and put this macro in and ran it but nothing happened.

i changed the path as well.
guessing it may be wrong.

thanks though
 
Upvote 0
What changes did you make to the path? Try stepping through the macro one line at a time by pressing the F8 key. As you do that see if any file is being opened when you get to this line of code:
VBA Code:
Set srcWB = Workbooks.Open(strPath & strExtension)
 
Upvote 0
i only changed the location of the folder as suggested in the original post.

no files are been opened, is it possible that it wont work if the files are not xls/xlsx as i have just realised they are all xlsb files
 
Upvote 0
i only changed the location of the folder as suggested in the original post.

no files are been opened, is it possible that it wont work if the files are not xls/xlsx as i have just realised they are all xlsb files
Then you will need to change this:
VBA Code:
    strExtension = Dir(strPath & "*.xlsx")
to this:
VBA Code:
    strExtension = Dir(strPath & "*.xlsb")
 
Upvote 0
Changed that as suggested and still isnt doing anything, just doing the same result
 
Upvote 0
Please post the exact full path.
 
Upvote 0
VBA Code:
Sub CopyRange()
    Application.ScreenUpdating = False
    Dim desWS As Worksheet, srcWB As Workbook, LastRow As Long
    Set desWS = ThisWorkbook.Sheets(1)
    Const strPath As String = "C:\Users\carlbow\Desktop\Non Pickable"
    ChDir strPath
    strExtension = Dir(strPath & "*.xlsb")
    Do While strExtension <> ""
        Set srcWB = Workbooks.Open(strPath & strExtension)
        With srcWB.Sheets(1)
            LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            .Range("B2:Q" & LastRow).Copy desWS.Cells(desWS.Rows.Count, "B").End(xlUp).Offset(1, 0)
            desWS.Cells(desWS.Rows.Count, "A").End(xlUp).Offset(1, 0).Resize(LastRow - 1) = srcWB.Name
        End With
        srcWB.Close savechanges:=False
        strExtension = Dir
    Loop
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try inserting a back slash at the end of the path string.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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