要将值放在活动单元中,请使用循环尝试:
Sub SumAboveLoop()
Dim r As Range, rAbove As Range
Dim v As Variant
Set r = ActiveCell
Set rAbove = Range(r.Offset(-1, 0), Cells(1, r.Column))
v = 0
For Each rr In rAbove
v = v + rr.Value
Next rr
r.Value = v
End Sub
要在没有循环的情况下在活动单元中放置一个值,请尝试:
Sub SumAboveV()
Dim r As Range, rAbove As Range
Dim wf As WorksheetFunction
Set wf = Application.WorksheetFunction
Set r = ActiveCell
Set rAbove = Range(r.Offset(-1, 0), Cells(1, r.Column))
r.Value = wf.Sum(rAbove)
End Sub
要将公式放置在没有循环的活动单元格中,请尝试:
Sub SumAboveF()
Dim r As Range, rAbove As Range
Dim wf As WorksheetFunction
Set wf = Application.WorksheetFunction
Set r = ActiveCell
Set rAbove = Range(r.Offset(-1, 0), Cells(1, r.Column))
r.Formula = "=SUM(" & rAbove.Address & ")"
End Sub