累计另一个单元格中一个单元格的值之和

时间:2019-02-25 11:09:40

标签: excel vba sum accumulate

问题是:
我如何求和另一个单元格的输入值?

例如:

  1. 我在单元格A1中输入值5,而单元格A2中显示5;
  2. 我在单元格A1中输入值3,而单元格A2中显示8;
  3. 我在A1单元格中输入值4,而A2则显示12。

1 个答案:

答案 0 :(得分:0)

将其放置在工作表模块中

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim inpt As Range, otpt As Range

    Set inpt = Range("A1"): Set otpt = Range("A2")

    If Not Intersect(Target, inpt) Is Nothing Then
        otpt.Value = otpt.Value + inpt.Value
        'inpt.Value = "" 'Option - Resets A1 after input
        'inpt.Select'Option - Returns selection to input cell
    End If
End Sub