我正在尝试构建一个代码,如果单元格中有更改,则仅在单元格值更改的地方清除该行中的内容。 这是我的代码,
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Set KeyCells = Range("I1:I209")
If Not Application.Intersect(KeyCells, Range(Target.Address)) _
Is Nothing Then
If MsgBox("ARE YOU SURE, YOU WANT TO DELETE?", vbYesNo + vbQuestion) = vbNo Then Exit Sub
'WHAT SHOULD BE CODE TO DELETE CONTENTS FROM THAT ROW FROM WHICH CELL VALUE CHANGED
End If
End Sub
任何帮助将不胜感激。谢谢。
答案 0 :(得分:1)
使用EntireRow
对象的Range
属性获取Target
行范围:
Target.EntireRow.ClearContents
仅清除更改的单元格时:
Target.ClearContents
如果要清除与更改的单元格相同的行中的B至,J至M和Q至S列中的单元格,则:
Intersect(Target.EntireRow, Range("B:D, J:M ,Q:S")).ClearContents
在所有情况下,您都应按照吉普(Jeeped)在其回答中显示的代码进行处理,即:
通过将清除代码beteeen Worksheet_Change
和Application.EnableEvents = False
Application.EnableEvents = True
事件
通过在Application.EnableEvents = True
之前放置On Error GoTo safe_exit
语句并将Application.EnableEvents = False
移到{之后,确保任何可能的错误不会阻止表单到达Application.EnableEvents = True
语句{1}}语句
答案 1 :(得分:1)
尝试
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Range("I1:I209"), Target) Is Nothing Then
If MsgBox("ARE YOU SURE, YOU WANT TO DELETE?", vbYesNo + vbQuestion) <> vbYes Then
Exit Sub
Else
On Error GoTo safe_exit
Application.EnableEvents = False
Dim t As Range
For Each t In Intersect(Range("I1:I209"), Target)
intersect(range("B:D,J:M,Q:S"), t.EntireRow).ClearContents
Next t
End If
safe_exit:
Application.EnableEvents = true
End Sub