Excel Macro to combine worksheets

pbajeff29

New Member
Joined
Aug 16, 2010
Messages
20
Office Version
  1. 365
I am looking for a macro that will step through each sheet and add all rows with data to the first sheet. It will need to look from row 2 down and then paste to the first empty row at the bottom of the list in page 1.

Example:
Sheet 1 - Results
Sheet 2 - AM
Sheet 3 - PM

Columns (A-P) match in all three row. And each sheet will be variable number of rows and changing daily.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
VBA Code:
Sub master_sheet()

    Dim wsM As Worksheet
    Set wsM = Sheets("Sheet1")
    
    'assumes Master sheet is the first sheet
    For x = 2 To Worksheets.Count
        Sheets(x).UsedRange.Copy Destination:=wsM.Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1)
        
    Next x
  
End Sub
 
Upvote 0
VBA Code:
Sub master_sheet()

    Dim wsM As Worksheet
    Set wsM = Sheets("Sheet1")
   
    'assumes Master sheet is the first sheet
    For x = 2 To Worksheets.Count
        Sheets(x).UsedRange.Copy Destination:=wsM.Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1)
       
    Next x
 
End Sub
It did not like the := so I removed the : and it accepted. Once I tried to run the Macro it gives me a run-time error '424': Object Required
 
Upvote 0
As is, the macro requires your sheet names shall be Sheet1, Sheet2, Sheet3, etc.

If your sheets are names something else, you'll need to adjust the macro to match.
 
Upvote 0
Ok I got it to run, but instead of it grabbing the information and putting it into the columns that exist on Sheet1, it is copying the entire list including the headers and adding it to the end of the column headers. It did that for each sheet, so it is not putting them down, it is doing them side to side. So after two sheets, I had 54 columns (first 18 with the headers, second 18 are headers and data from Sheet2 and 3rd 18 are headers and data from Sheet3. I need it to not copy the headers and to place them under the existing headers in Sheet1 and continue adding them straight down.
 
Upvote 0
My bad .... the first suggested macro does precisely what you are experiencing ... copying to new columns to the right.

I believe this is what you are seeking :

VBA Code:
Option Explicit

Sub Maybe()
    Dim sh As Worksheet
    
    For Each sh In ActiveWorkbook.Sheets
        If sh.Name <> "Sheet1" Then sh.UsedRange.Offset(1).Copy Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Offset(1)
    Next sh
    
End Sub
 
Upvote 0
Since you are using O365, you have the option of using Power Query to do this. Look at this video tutorial as an alternative to VBA

 
Upvote 0
Since you are using O365, you have the option of using Power Query to do this. Look at this video tutorial as an alternative to VBA

Love this thank you, it got me almost to where I need to be. I got the master list and I created a pivot off of it, which is something I need. But when I refresh all, I then have to do a separate refresh directly on the pivot. I am creating a button so that when users need to update, they click it and it pulls the data from SharePoint into the respective sheets and the Append sheet updates as well. But I also need it to update the Pivot. I did the following code for the refresh button and it works for everything but the pivot, any thoughts?

Sub Refresh()

ThisWorkbook.RefreshAll

Pivot.PivotTables("PivotTable1").PivotCache.Refresh

End Sub
 
Upvote 0
Try this

VBA Code:
ActiveWorkbook.RefreshAll
Nope, not working. For some reason it is not accepting anything I throw at it to update the pivot. It is refreshing the SharePoint queries in the individual sheets and doing the Append function, but it will not update the pivot automatically.
 
Upvote 0

Forum statistics

Threads
1,215,133
Messages
6,123,235
Members
449,092
Latest member
SCleaveland

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