Merge ranges from specific sheets by adding the sheet name as the first column

tabrizbalasi

New Member
Joined
Sep 14, 2014
Messages
2
I have quite a number of sheets whose names end in _A or _B.
GbUPA.png


9z3na.png

VplHl.png


I would like to merge all sheets ending in _A or _B under one another., only ranges A:C until last value in column A. (There is always a value in cells in column A, but not necessarily in corresponding columns B and C, that is why I take count of rows based on A.)
They have the same number of columns, but different number of rows across these sheets. However, when merging, I want the sheetname to be repeated all the way down the last row of that range from each sheet in the merged sheet. Result:

gcrkv.png

I have tried this but I run into error in line:
Code:
ws.RangeToMerge.Copy Destination:=Worksheets("Merged").Range("B" & RowsPresent + 1)

Here is the full code:


Code:
Sub Merge_Resposes()


Dim RowsToMerge As Integer
Dim RowsPresent
Dim RangeToMerge As Range

For i = 1 To ActiveWorkbook.Worksheets.Count
      If InStr(Worksheets(i).Name, "_A") <> 0 Or InStr(Worksheets(i).Name, "_B") <> 0 Then
            Set ws = Worksheets(i)
            RowsToMerge = ws.Cells(Rows.Count, "A").End(xlUp).Row
            RowsPresent = Sheets("Merged").Cells(Rows.Count, "A").End(xlUp).Row
            Set RangeToMerge = ws.Range("A1:C" & RowsToMerge)
            ws.RangeToMerge.Copy Destination:=Worksheets("Merged").Range("B" & RowsPresent + 1)
            Worksheets("Merged").Range("A" & RowsPresent + 1) = ws.Name

      End If
Next

End Sub

Could somebody help me debug this and run a working version?
Thanks.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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