Combining named worksheets with Macro

Exl_in_Training

New Member
Joined
Dec 6, 2023
Messages
4
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
I have this Macro that combines the contents of all sheets from the 3rd to last sheet in the work document and presenting this consolidated result in sheet that is called "Master Register", which is the second sheet in the workbook. From the Macro code below, you'll see that I pick up the first sheets to include as part of the merging exercise from Sheet 3 to last (in total 9 sheets).

However, I now need to run a report extracts from this master sheet, which means a new sheet is added to my workbook. What other way can I use to let the code pick up the first worksheet if the actual sheet number keeps changing? (Not sure if I phrased this correctly.). Regardless of how many other sheets I put in front, the data merging should always start with the sheet called Daata Sheet 3.
1702090133230.png



Sub Combine()

Dim M As Integer

On Error Resume Next
'Selecting first sheet for merging
Sheets(2).Activate
Range("A3:a2000").EntireRow.Delete

Sheets(3).Activate

Range("A3:R").EntireRow.Select

Selection.Copy Destination:=Sheets(1).Range("A3:R")

For M = 3 To Sheets.Count

Sheets(M).Activate

ActiveSheet.Range("A3", "R" & sheetlastrow).Copy

Selection.CurrentRegion.Select

Selection.Offset(2, 0).Resize(Selection.Rows.Count - 2).Select

Selection.Copy Destination:=Sheets(2).Range("A65536").End(xlUp)(2)

Sheets(2).Activate
Range("A:R").WrapText = True

Next

'Column_Width

End Sub
 

Attachments

  • 1702090069386.png
    1702090069386.png
    5.8 KB · Views: 13

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Maybe something like this.
VBA Code:
Sub Combine1()
    Dim WS As Worksheet, WSMR As Worksheet                             'master register
    Dim LastRow As Long
    Dim rng As Range, rngDest As Range

    Set WSMR = Worksheets("Master Register")
    
    WSMR.Activate
    WSMR.UsedRange.Offset(2).ClearContents

    For Each WS In Worksheets
        If UCase(Left(WS.Name, 4)) = "DATE" And WS.UsedRange.Rows.Count > 2 Then
            With WSMR
                LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
                Set rngDest = .Range("A" & .Rows.Count).End(xlUp).Offset(1)
            End With

            With WS
                Set rng = .Range("A3", .Range("A" & .Rows.Count).End(xlUp))
            End With

            rng.EntireRow.Copy rngDest
        End If
    Next WS

    With WSMR
        .Range("A:R").WrapText = True
        .UsedRange.Columns.AutoFit
    End With
End Sub
 
Upvote 0
Another option
VBA Code:
For M = Worksheets.Count - 8 To Worksheets.Count
 
Upvote 0
Thanks everyone. This one did the trick

For M = Worksheets("Data Sheet 3").Index To Worksheets.Count

Really appreciate the help
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,252
Members
452,623
Latest member
Techenthusiast

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