所以我收到一个错误:结束如果没有阻止如果,我是VBA的新手并试图将其他线程的答案应用到我自己没有运气。你能帮我么。 提前致谢
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
' The variable KeyCells contains the cells that will
' cause an alert when they are changed.
Set KeyCells = Range("B2:B6")
If Not Application.Intersect(KeyCells, Range(Target.Address)) _
Is Nothing Then
' Display a message when one of the designated cells has been
' changed.
' Place your code here.
MsgBox "Cell " & Target.Address & " has changed."
For Each KeyCells In Range(Target.Address)
If KeyCells.Value <> "" Then KeyCells.Value = KeyCells.Value & "-CN"
Next
End If
End Sub
答案 0 :(得分:1)
要确保只在您需要关闭代码中的Events
后才会触发,因为代码本身会对单元格进行更改,这会再次触发您正在使用的确切事件。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
' The variable KeyCells contains the cells that will
' cause an alert when they are changed.
Set KeyCells = Range("B2:B6")
If Not Application.Intersect(KeyCells, Target) _
Is Nothing Then 'since Target is range it will suffice here
MsgBox "Cell " & Target.Address & " has changed."
Application.EnableEvents = False 'turn off events to avoid endless loop
For Each KeyCells In Range(Target.Address)
If KeyCells.Value <> "" Then KeyCells.Value = KeyCells.Value & "-CN"
Next
Application.EnableEvents = True 'turn back on so events continue to fire
End If
End Sub