A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
Ok, dynamic offset. Scott, please check below code. Update range per your requirement. Have converted column nos to column letters & this should be valid for column limit per Excel 2007 (16384 columns).
Sub InsertRows_1()
Dim lastRow As Long
Dim rowPtr As Long
Dim lStartRow As Long, lEndRow As Long
Dim lColNo As Long, sOffset As String
lastRow = Range("A" & Rows.count).End(xlUp).Row
lEndRow = lastRow + 1
For rowPtr = lastRow To 2 Step -1
If Not IsEmpty(Range("A" & rowPtr)) Then
If Range("A" & rowPtr) <> Range("A" & rowPtr - 1) Then
Range("A" & rowPtr).EntireRow.Insert
lStartRow = rowPtr + 1
For lColNo = 1 To 7
'this inserts sum formula in columns C, D & E
If lColNo < 6 And lColNo > 2 Then
sOffset = Split(Cells(1, lColNo).Address, "$")(1)
Range("A" & lEndRow + 1).Offset(0, lColNo - 1) = "=SUM(" & sOffset & lStartRow & ":" & sOffset & lEndRow & ")"
End If
Next lColNo
lEndRow = lStartRow - 1
End If
End If
Next
lStartRow = 1
For lColNo = 1 To 7
'this inserts sum formula in columns C, D & E
If lColNo < 6 And lColNo > 2 Then
sOffset = Split(Cells(1, lColNo).Address, "$")(1)
Range("A" & lEndRow).Offset(0, lColNo - 1) = "=SUM(" & sOffset & lStartRow & ":" & sOffset & lEndRow - 1 & ")"
End If
Next lColNo
End Sub
Have also updated the OR to AND within the IF statement.
Regards,
Amit Tandon