Exl_in_Training
New Member
- Joined
- Dec 6, 2023
- Messages
- 4
- Office Version
- 365
- 2021
- Platform
- 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.
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
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.
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