如何获得整列的总和?

时间:2019-01-30 19:17:24

标签: excel vba

我有一些代码,可以给我A至J行中的值的总和,但是它仅将行1至2中的值相加。我试图让它对行1至10中的值求和。

Sub addcol()
Dim i As Long, j As Long
Dim lastRow As Long
lastRow = Cells.Find(What:="*", _
After:=Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlPrevious, _
MatchCase:=False).Row

Dim LastColumn As Long
LastColumn = Cells.Find(What:="*", _
After:=Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column

For j = 1 To lastRow
    For i = 1 To LastColumn
    Cells(lastRow + 1, i) = Cells(lastRow + 1, i) + Application.WorksheetFunction.Sum(Cells(j, i))
    Next i
Next j

End Sub

0 个答案:

没有答案