我需要根据B列中输入的数字创建一个Excel文档,并在A列中更新总计。每当添加新值时,A行中的每个相应单元格都应根据其等效的B单元格值进行更新,然后将添加到A中的值清除到B中输入的值。
我已经完成了单行工作,但是没有知识或了解如何最好地使整列中的每个单元对都有效。我真的不想复制并粘贴此10,000次并更新单元格以引用正确的对。单个单元格的代码:
Private bIgnoreEvent As Boolean
Private Sub Worksheet_Change(ByVal Target As Range)
If bIgnoreEvent Then Exit Sub
bIgnoreEvent = True
Cells(1, 2) = Cells(1, 2) + Cells(1, 1)
Cells(1, 1) = ""
bIgnoreEvent = False
End Sub
我希望可以通过循环功能或某种范围的方法来实现。
答案 0 :(得分:0)
这应该有效:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range, c As Range
'any updates in ColB?
Set rng = Application.Intersect(Target, Me.Columns(2))
If Not rng Is Nothing Then
Application.EnableEvents = False '<< prevent re-triggering of event
'Process each updated cell
For Each c In rng
If IsNumeric(c.Value) Then
With c.Offset(0, -1)
.Value = .Value + c.Value
End With
c.ClearContents
End If
Next c
Application.EnableEvents = True '<< re-enable event
End If
End Sub