VBA Combine all sheets into one

rplohocky

Active Member
Joined
Sep 25, 2005
Messages
292
Office Version
  1. 365
Platform
  1. Windows
Hello,
I have ben using this code to combine all the sheets in one workbook into one sheet then delete the all but the combined one. It has been working fine but I just found out that if there is just a header row it copies the header row into the combined sheet. I don't want any header rows brought into the combined sheet, it does not doit on any of the sheets with data in rows 2 and beyond only the blank sheets.

Can anyone see how to modify this code to not take the header row or ignore the sheets that don't have data in row 2 and beyond?
Code:
Sub CombineTabsInventoryReports()
'this macro will combine all sheets within a WB into one sheet
'Make sure that all header rows on all tabs are identical.

    Dim J As Integer
    On Error Resume Next
    Sheets(1).Select
    Worksheets.Add
    Sheets(1).Name = "Combined"
    Sheets(2).Activate
    Range("A1").EntireRow.Select
    Selection.Copy Destination:=Sheets(1).Range("A1")
    For J = 2 To Sheets.Count
    Sheets(J).Activate
    Range("A1").Select
    Selection.CurrentRegion.Select
    Selection.Offset(1, 0).Resize(Selection.Rows.Count - 1).Select
    Selection.Copy Destination:=Sheets(1).Range("A65536").End(xlUp)(2)
    Next

'This section will delete all tabs but the combined one
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    For Each xWs In Application.ActiveWorkbook.Worksheets
        If xWs.Name <> "Combined" Then
            xWs.Delete
        End If
    Next
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    
End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
rplohocky,

You might consider adding the rows in red...

Code:
Sub CombineTabsInventoryReports()
'this macro will combine all sheets within a WB into one sheet
'Make sure that all header rows on all tabs are identical.

    Dim J As Integer
    On Error Resume Next
    Sheets(1).Select
    Worksheets.Add
    Sheets(1).Name = "Combined"
    Sheets(2).Activate
    Range("A1").EntireRow.Select
    Selection.Copy Destination:=Sheets(1).Range("A1")
    
    For J = 2 To Sheets.Count
        Sheets(J).Activate
        Range("A1").Select
        Selection.CurrentRegion.Select
        [COLOR=#ff0000]If Selection.Rows.Count > 1 Then[/COLOR]
            Selection.Offset(1, 0).Resize(Selection.Rows.Count - 1).Select
            Selection.Copy Destination:=Sheets(1).Range("A65536").End(xlUp)(2)
        [COLOR=#ff0000]End If[/COLOR]
    Next

'This section will delete all tabs but the combined one
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    For Each xWs In Application.ActiveWorkbook.Worksheets
        If xWs.Name <> "Combined" Then
            xWs.Delete
        End If
    Next
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    
End Sub

Cheers,

tonyyy
 
Upvote 0
Hello rplohocky,

Besides what Tonyyy has suggested, you could trim the code down further as follows:-


Code:
Sub Combine()

        Dim ws As Worksheet, ws1 As Worksheet
        Set ws1 = Sheets("Combined")

Application.ScreenUpdating = False
Application.DisplayAlerts = False

For Each ws In Worksheets
        If ws.Name <> "Combined" Then
        ws.UsedRange.Offset(1).Copy ws1.Range("A" & Rows.Count).End(3)(2)
        ws.Delete
        End If
Next ws

Application.DisplayAlerts = True
Application.CutCopyMode = False
Application.ScreenUpdating = True

End Sub

It assumes that the "Combined" sheet already exists.


I hope that this helps,

Cheerio,
vcoolio.
 
Upvote 0
rplohocky,

You might consider adding the rows in red...

Code:
Sub CombineTabsInventoryReports()
'this macro will combine all sheets within a WB into one sheet
'Make sure that all header rows on all tabs are identical.

    Dim J As Integer
    On Error Resume Next
    Sheets(1).Select
    Worksheets.Add
    Sheets(1).Name = "Combined"
    Sheets(2).Activate
    Range("A1").EntireRow.Select
    Selection.Copy Destination:=Sheets(1).Range("A1")
    
    For J = 2 To Sheets.Count
        Sheets(J).Activate
        Range("A1").Select
        Selection.CurrentRegion.Select
        [COLOR=#ff0000]If Selection.Rows.Count > 1 Then[/COLOR]
            Selection.Offset(1, 0).Resize(Selection.Rows.Count - 1).Select
            Selection.Copy Destination:=Sheets(1).Range("A65536").End(xlUp)(2)
        [COLOR=#ff0000]End If[/COLOR]
    Next

'This section will delete all tabs but the combined one
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    For Each xWs In Application.ActiveWorkbook.Worksheets
        If xWs.Name <> "Combined" Then
            xWs.Delete
        End If
    Next
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    
End Sub

Cheers,

tonyyy

Hello Tonyyy,
Thanks for the help! I inserted those lines in the code but now I the only thing that gets copied is one header row, none of the other rows of data from the other 14 workbooks gets copied over to the combined sheet.
 
Upvote 0
Hello Tonyyy,
Thanks for the help! I inserted those lines in the code but now I the only thing that gets copied is one header row, none of the other rows of data from the other 14 workbooks gets copied over to the combined sheet.

Tonyyy,
Nevermind!!! My mistake I inserted the code 1 line above where it should have been. I fixed it and now it works great thanks again for your help!!
 
Upvote 0

Forum statistics

Threads
1,216,082
Messages
6,128,709
Members
449,464
Latest member
againofsoul

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