我使用此宏来计算总和,但它不起作用。这是我到目前为止所尝试的:
Sub sum()
I = .Range("E2").End(xlDown).Row
cumul = 0
For E = 2 To I
cumul = cumul + Cells(E, 2)
Next
Range("E7") = cumul
End Sub
答案 0 :(得分:0)
如果我理解正确,下面的代码将汇总E列中的所有值(从Cell E2开始),并将结果放在Cell B2中。 (您可以轻松修改位置)
注意:我添加了“选项2”,而不是使用For
循环,您可以使用WorksheetFunction.Sum
对范围内的所有值求和需要迭代。
<强>代码强>
Option Explicit
Sub SumColE()
Dim cumul As Long, I As Long, E As Long
With Worksheets("Sheet1") ' <-- modify "Sheet1" to your sheet's name
I = .Range("E2").End(xlDown).Row '<-- get last row in Column E
cumul = 0
' option 1: using a loop
For E = 2 To I
cumul = cumul + .Range("E" & E) '<-- Sum up values in Column E
Next E
.Range("B2").Value = cumul '<-- put the result in B2
' option 2: using WorksheetFunction SUM
.Range("B2").Value = Application.WorksheetFunction.Sum(.Range("E2:E" & I))
End With
End Sub