VBA - Consolidating spreadsheets including tab name

Clare1805

New Member
Joined
Apr 14, 2008
Messages
39
I have a basic understanding of VBA and am trying to create a macro that will consolidate all worksheets within a workbook into one tab.

I have the found the following code which consolidates all the data but I need it to also copy the name of the worksheet alongside the data from that sheet - can anyone help?

Sub mergeallsheets2one()
Sheets(1).Activate
lastrow = ActiveSheet.UsedRange.Rows.Count
For Each Sheet In Sheets
If Sheet.Index <> 1 Then
RowCount = Sheet.UsedRange.Rows.Count
Sheet.UsedRange.Copy Destination:=Sheets(1).Cells(lastrow + 1, 1)
lastrow = lastrow + RowCount
End If
Next Sheet
End Sub

Thanks in advance!
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Hello,

is this working as expected?

Code:
Sub mergeallsheets2one()
    Sheets(1).Activate
    lastrow = ActiveSheet.UsedRange.Rows.Count
    For Each Sheet In Sheets
        If Sheet.Index <> 1 Then
            RowCount = Sheet.UsedRange.Rows.Count
            Sheets(1).Range("A65536").End(xlUp).Offset(1, 0).Value = Sheets(Sheet.Index).Name
            Sheet.UsedRange.Copy Destination:=Sheets(1).Range("A65536").End(xlUp).Offset(1, 0)
            lastrow = lastrow + RowCount
        End If
    Next Sheet
End Sub
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,399
Latest member
alchavar

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