D列不合并的话可以在D2输入=IF(A2="","",IF(A3="",SUM(C2:C3),C2))向下填充。合并了公式就无法填充,只能一个一个输入。
Sub 宏1()
Columns("A:A").Copy
Columns("D:D").PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
xr = ActiveSheet.UsedRange.Rows.Count
Range("d2:d" & xr) = 1
For i = 2 To xr
If Range("D" & i) <> "" And Range("D" & i).MergeCells Then
ss = Split(Range("D" & i).MergeArea.Address, ":")
s1 = Split(ss(0), "$")(2)
s2 = Split(ss(1), "$")(2)
Range("D" & i) = Application.Sum(Range("C" & s1 & ":C" & s2))
Else
Range("D" & i) = Range("c" & i)
End If
Next
End Sub
录制了一个
坐等大神出没
自动合并单元格的真没听说过