|
- Sub ykcbf() '//2025.3.27 插入小计行
- On Error Resume Next
- With ActiveSheet
- col = 1: c1 = 2: bt = 0
- xm = [{"小计","总计"}]
- r = .Cells(.Rows.Count, col).End(xlUp).Row
- c = .UsedRange.Find("*", , -4163, , 2, 2).Column
- For i = bt + 1 To r
- If InStr(.Cells(i, col), "计") Then
- MsgBox "已有小计行,不必再插入!"
- Exit Sub
- End If
- Next
- m = r + 1
- .UsedRange.Offset(bt).Interior.ColorIndex = 0
- For i = r To bt + 1 Step -1
- If .Cells(i, col) <> .Cells(i - 1, col) Then
- .Rows(m).Insert
- .Cells(m, col) = xm(1)
- .Cells(m, c1).Resize(1, c - c1 + 1).Formula = "=SUM(" & Cells(i, c1).Resize(m - i).Address(0, 0) & ")"
- .Cells(m, 1).Resize(1, c).Interior.ColorIndex = 4
- m = i
- End If
- Next
- r = .Cells(.Rows.Count, col).End(xlUp).Row
- .Cells(r + 1, col) = xm(2)
- .Cells(r + 1, 1).Resize(1, c).Interior.ColorIndex = 6
- .Cells(r + 1, c1).Resize(1, c - c1 + 1).Formula = "=SUMIFS(" & Cells(bt + 1, c1).Resize(r - bt).Address(0, 0) _
- & "," & Cells(bt + 1, col).Resize(r - bt).Address(0, 1) & "," & """" & xm(1) & """" & ")"
- .Cells(1, 1).Resize(r + 1, c).Borders.LineStyle = 1
- ActiveWindow.DisplayZeros = False
- End With
- End Sub
复制代码
|
|