-2

我希望将一个单元格中的值累积到附近的单元格中。它应该对值求和,我在下面的代码中使用,但我需要的单元格范围是 A1 到 A10,累积结果应该在 B1 到 B10 中。对不起我是新手

Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Range("A1"), Target) Is Nothing Then Exit Sub
[B1] = [B1] + [A1]

结束子

4

1 回答 1

0

首先,丢弃 [A1] 风格的寻址,忘记你曾经知道它的存在。

扩大相交范围并处理目标,其中目标可能不止一个单元格。

当您更改工作表中的值时,临时挂起事件处理或 Worksheet_Change 将尝试自行运行。

处理错误,否则您可能会遇到卡住的工作簿。

Private Sub Worksheet_Change(ByVal Target As Range)
    If not Intersect(Range("A1:A10"), Target) is nothing then
        on error goto meh
        application.enableevents = false
        dim t as range
        for each t in Intersect(Range("A1:A10"), Target)
            t.offset(0, 1) = t.offset(0, 1) + t.value2
        next t
    end if

meh:
    application.enableevents = true
End Sub
于 2018-10-01T09:18:50.210 回答