我目前正在尝试将一个脚本添加到excel中。请原谅我的术语,我对编程并不热衷!
我在excel 2003上完成了所有会计工作,我希望能够将{cell} f6
的值添加到f27
到单元格e6
到{{1 }}, 分别。问题是,我想要" f"每次重置的列。
到目前为止,我已经找到了这个代码,如果我将其复制并粘贴到VBA中,它就可以工作。但它只允许我在一行上使用它:
e27
有人会很友好地解释我如何编辑它以通过我所有想要的细胞做同样的事情吗?我尝试过添加Range(" f7",[f8],[f9]等等。但我真的不知道。
答案 0 :(得分:0)
首先,你需要定义应该被捕获的范围&#34 ;;也就是说,定义要跟踪更改的范围。我找到了一个例子here。然后,只需将值添加到另一个单元格:
Private Sub Worksheet_Change(ByVal Target as Range)
Dim r as Range ' The range you'll track for changes
Set r = Range("F2:F27")
' If the changed cell is not in the tracked range, then exit the procedure
' (in other words, if the intersection between target and r is empty)
If Intersect(Target, r) Is Nothing Then
Exit Sub
Else
' Now, if the changed cell is in the range, then update the required value:
Cells(Target.Row, 5).Value = Cells(Target.Row, 5).Value + Target.Value
' ----------------^
' Column 5 =
' column "E"
' Clear the changed cell
Target.ClearContents
End if
End Sub
希望这有帮助
答案 1 :(得分:0)
试试这个
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo ErrHandler
Application.ScreenUpdating = False
Application.EnableEvents = False
If Intersect(Target, Range("B1:B5,F6:F27")) Then 'U can define any other range
Target.Offset(0, -1) = Target.Offset(0, -1).Value + Target.Value ' Target.Offset(0,-1) refer to cell one column before the changed cell column.
'OR: Cells(Target.row, 5) = Cells(Target.row, 5).Value + Target.Value ' Where the 5 refer to column E
Target.ClearContents
End If
ErrHandler:
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub