I currently use the below code (MACRO1) to copy ALL of the rows (variable in quantity) from the sheet "Template" and paste them to the Active Sheet to the first blank row. All pasted rows except the first row, are then grouped, and a number in the first row incremented. Each time the macro is run, another "set" (all rows in Template) is pasted to the next blank row of the active sheet and then grouped and the next incremented number placed in its first row.
What I would like to do in addition is:
- When rows on the Template sheet are changed (formula, data, row addition/deletion etc), then I want to manually run another macro (MACRO2) to update EACH of the existing groups on all sheets, so that they remain the same as the the rows in "Template". The only difference being the Template rows are not grouped.
Option Explicit
Sub Macro1()
Dim copySheet As Worksheet, pasteSheet As Worksheet, ws As Worksheet, LRow As Long, csLastRow As Long, i As Long, StartNumber As Long, varString As String
Set copySheet = ThisWorkbook.Worksheets("Template")
varString = copySheet.Cells(2, 2).Value2
Set pasteSheet = ThisWorkbook.ActiveSheet
StartNumber = 1
ActiveSheet.Outline.ShowLevels RowLevels:=2, ColumnLevels:=0
With pasteSheet
If .Name = "Template" Then MsgBox "Cannot Paste to Template": Exit Sub
If Application.WorksheetFunction.CountA(.Cells) = 0 Then
LRow = 2
Else
LRow = .Range("A" & .Rows.Count).End(xlUp).Row + 1
For i = LRow To 1 Step -1
If .Cells(i, 2).Value2 = varString Then
StartNumber = .Cells(i, 6).Value2 + 1
Exit For
End If
Next i
End If
If .Outline.SummaryRow <> xlSummaryAbove Then .Outline.SummaryRow = xlSummaryAbove
csLastRow = copySheet.Cells(Rows.Count, 1).End(xlUp).Row
copySheet.Range("2:" & csLastRow).Copy
.Rows(LRow).PasteSpecial Paste:=xlPasteAll
.Range(.Cells(.Rows.Count, 1).End(xlUp), .Cells(.Rows.Count, 1).End _
(xlUp).Offset(-(csLastRow - 3), 1)).EntireRow.Group
ActiveSheet.Outline.ShowLevels RowLevels:=1, ColumnLevels:=0
.Cells(LRow, 6).Value = StartNumber
.Cells(LRow, 6).NumberFormat = "000"
End With
End Sub