Combine all same name worksheets into one worksheet

sklee

New Member
Joined
Apr 12, 2023
Messages
5
Office Version
  1. 2021
Platform
  1. Windows
Dear VBA experts,

I would like to combine all the data of each same name worksheet into one worksheet.
For example, there are 3 (or more than 3) xlsx files which are currently open;

1st xlslx file with 4 sheets named "TODO", "DONE", "FUTURE", "PAST"
2nd xlslx file with 3 sheets named "TODO", "FUTURE", "TODAY"
3rd xlsx file with 3 sheets named "TODO", "DONE", "TOMORROW"

Then running a macro will open a new xlsx file and then have the sheets named as below with combined data ;

TODO (data of TODO sheet of 1st, 2nd, 3rd xlsx are combined)
DONE (data of DONE sheet of 1st, 3rd xlsx are combined)
FUTURE (data of FUTURE sheet of 1st, 2nd xlsx are combined)
PAST (data of PAST of 1st xlsx)
TODAY (data of TODAY of 2nd xlsx)
TOMORROW (data of TOMORROW of 3rd xlsx)

Could any one of you help?

Warm wishes,
sklee
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Dear VBA experts,

I would like to combine all the data of each same name worksheet into one worksheet.
For example, there are 3 (or more than 3) xlsx files which are currently open;

1st xlslx file with 4 sheets named "TODO", "DONE", "FUTURE", "PAST"
2nd xlslx file with 3 sheets named "TODO", "FUTURE", "TODAY"
3rd xlsx file with 3 sheets named "TODO", "DONE", "TOMORROW"

Then running a macro will open a new xlsx file and then have the sheets named as below with combined data ;

TODO (data of TODO sheet of 1st, 2nd, 3rd xlsx are combined)
DONE (data of DONE sheet of 1st, 3rd xlsx are combined)
FUTURE (data of FUTURE sheet of 1st, 2nd xlsx are combined)
PAST (data of PAST of 1st xlsx)
TODAY (data of TODAY of 2nd xlsx)
TOMORROW (data of TOMORROW of 3rd xlsx)

Could any one of you help?

Warm wishes,
sklee
Hi Sklee,
I use Kutools all the time for needs such as this. Check it out, it's and amazing add-in.
 
Upvote 0
Hi Sklee,
I use Kutools all the time for needs such as this. Check it out, it's and amazing add-in.
Hello Anfinsen,
Thanks for yout tip. I know Kutools has 4 combine options, but I just would like to have the VBA code which only does the thing I just described.
Additionally I need to add it in my other VBA macro to run. ;)
Thanks again for your expertise.
Sklee
 
Upvote 0
See if the following comes close to what you want. It assumes that all the data you want to extract are in files that are already open:
there are 3 (or more than 3) xlsx files which are currently open
It may take a few attempts to get this just right, but here's a start.

VBA Code:
Option Explicit
Sub Consolidate()
    Application.ScreenUpdating = False
    'Create a new Consolidated Data workbook
    Dim MySheets, wb1 As Workbook, i As Long
    MySheets = Array("TODO", "DONE", "FUTURE", "PAST", "TODAY", "TOMORROW")
    Set wb1 = Workbooks.Add
    Do Until i = 6
        Sheets.Add After:=Sheets(Sheets.Count)
        i = wb1.Sheets.Count
    Loop
    For i = 1 To wb1.Worksheets.Count
        Sheets(i).Name = MySheets(i - 1)
    Next i
    wb1.SaveAs ThisWorkbook.Path & "\Consolidated Data.xlsx"

    'Loop through each open workbook & extract data
    Dim wb As Workbook, x As String, j As Long, LRow As Long
    For Each wb In Application.Workbooks
        If wb.Name <> "Consolidated Data.xlsx" Then
            For i = 1 To wb.Sheets.Count
                x = wb.Sheets(i).Name
                For j = LBound(MySheets) To UBound(MySheets)
                    If x = MySheets(j) Then
                        LRow = wb1.Sheets(x).Cells(Rows.Count, "A").End(xlUp).Row + 1
                        wb.Sheets(x).Range("A1").CurrentRegion.Copy wb1.Sheets(x).Range("A" & LRow)
                    End If
                Next j
            Next i
        End If
    Next wb
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
See if the following comes close to what you want. It assumes that all the data you want to extract are in files that are already open:

It may take a few attempts to get this just right, but here's a start.

VBA Code:
Option Explicit
Sub Consolidate()
    Application.ScreenUpdating = False
    'Create a new Consolidated Data workbook
    Dim MySheets, wb1 As Workbook, i As Long
    MySheets = Array("TODO", "DONE", "FUTURE", "PAST", "TODAY", "TOMORROW")
    Set wb1 = Workbooks.Add
    Do Until i = 6
        Sheets.Add After:=Sheets(Sheets.Count)
        i = wb1.Sheets.Count
    Loop
    For i = 1 To wb1.Worksheets.Count
        Sheets(i).Name = MySheets(i - 1)
    Next i
    wb1.SaveAs ThisWorkbook.Path & "\Consolidated Data.xlsx"

    'Loop through each open workbook & extract data
    Dim wb As Workbook, x As String, j As Long, LRow As Long
    For Each wb In Application.Workbooks
        If wb.Name <> "Consolidated Data.xlsx" Then
            For i = 1 To wb.Sheets.Count
                x = wb.Sheets(i).Name
                For j = LBound(MySheets) To UBound(MySheets)
                    If x = MySheets(j) Then
                        LRow = wb1.Sheets(x).Cells(Rows.Count, "A").End(xlUp).Row + 1
                        wb.Sheets(x).Range("A1").CurrentRegion.Copy wb1.Sheets(x).Range("A" & LRow)
                    End If
                Next j
            Next i
        End If
    Next wb
    Application.ScreenUpdating = True
End Sub
Hi kevin9999,
Thanks a billion kevin for your help! Oh sorry again that my question was not clear enough.
The nubmer of sheets and their names above are just samples, actuallly they are not fixed but changeable.
Sometime, they are 3,4,5,6,7,8,9,10 sheets etc. and their names are changed.
Simply put, I would like to combine any same name worksheet of all open xlsx files into in Consolidated Data.xlsx file, Just like the attached (combine_sheets.jpg) :-).
(CurrentRegion.Copy just does not copy data if empty column exist between them. I need to copy all data region including the empty columns.)
Sincerely yours,
sklee
 

Attachments

  • combine_sheets.jpg
    combine_sheets.jpg
    105.7 KB · Views: 7
Upvote 0
The nubmer of sheets and their names above are just samples, actuallly they are not fixed but changeable.
Sometime, they are 3,4,5,6,7,8,9,10 sheets etc. and their names are changed.
The number of sheets doesn't matter, the following line of code loops through all sheets in each open workbook - irrespective of how many there are:
VBA Code:
For i = 1 To wb.Sheets.Count
The names, however, are important. The following line of code looks for sheets with the exact names specified in the code. If they differ, you'll need to change it in this line:
VBA Code:
MySheets = Array("TODO", "DONE", "FUTURE", "PAST", "TODAY", "TOMORROW")
As far as the non-contiguous ranges goes, try changing this line:
VBA Code:
wb.Sheets(x).Range("A1").CurrentRegion.Copy wb1.Sheets(x).Range("A" & LRow)
to this:
VBA Code:
wb.Sheets(x).UsedRange.Copy wb1.Sheets(x).Range("A" & LRow)
and see if that works for you.
 
Upvote 0
Once again thanks for your sharing kevin999 and it seems that the data are combined in Consolidated Data.xlsx.
Like I said, the number of xlsx files and sheets, sheets names are not fixed but changable.
Instead of using MySheets = Array() where I should specify the sheet names, I would want to combine all the same name sheets of the xlsx files, "which are currently open".
(All the same name sheets have their own format, so I want the combined sheets to keep that format (like column width etc.) as well)
I think I am almost there :-)
 
Upvote 0
Instead of using MySheets = Array() where I should specify the sheet names, I would want to combine all the same name sheets of the xlsx files, "which are currently open".
This is significantly different from what you asked for in your initial post. I did say that you needed to specify the sheet names:
The following line of code looks for sheets with the exact names specified in the code. If they differ, you'll need to change it in this line:
VBA Code:
MySheets = Array("TODO", "DONE", "FUTURE", "PAST", "TODAY", "TOMORROW")
however, you seem to be asking that the code should determine the names of all sheets, in all open files, at run time to enable their consolidation? That would not only require considerably more coding, it is also (potentially) fraught with problems - what if another xlsx file happened to be left open inadvertently? If that's the way you want to go, I suggest you start a new thread with your full intentions clearly articulated from the start.
 
Upvote 1

Forum statistics

Threads
1,223,632
Messages
6,173,469
Members
452,516
Latest member
archcalx

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