我需要将单元格A添加到同一行的单元格B,然后将单元格A重置为零。我做了一些搜索,发现这个代码完美无缺,但我需要让它适用于整个列而不仅仅是一行。
因此,所有A列都应添加到B列,但只能添加到同一行。这是我找到的代码:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Target.Address = Range("a1").Address Then
Range("b1") = Range("b1") + Range("a1")
Range("a1").ClearContents
End If
Application.EnableEvents = True
End Sub
和
Private Sub Worksheet_Change(ByVal Target As Range)
Dim A1 As Range
Set A1 = Range("A1")
If Intersect(Target, A1) Is Nothing Then
Else
Application.EnableEvents = False
With A1
.Offset(0, 1) = .Offset(0, 1) + .Value
.ClearContents
End With
Application.EnableEvents = True
End If
End Sub
两组代码都可以很好地将单元格A添加到B并将A重置为零,但我希望这适用于整个(A列和B列)以及所有行。
这是我想要的照片。我希望这一点清楚易懂。我感谢任何人都能提供的任何帮助。对此,我真的非常感激。谢谢。
答案 0 :(得分:0)
尝试一下:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim T As Range, r As Range
Set T = Intersect(Target, Range("A:A"))
If T Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each r In T
With r
.Offset(0, 1).Value = .Offset(0, 1).Value + .Value
.ClearContents
End With
Next r
Application.EnableEvents = True
End Sub