【发布时间】:2022-01-02 11:48:07
【问题描述】:
我需要有关 Excel VBA 代码的帮助。
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 3 And Target.Cells.Count = 1 Then
If LCase(Target.Value) = "-1" Then
With Target.EntireRow.ClearContents
End With
End If
End If
If Target.Column = 3 And Target.Cells.Count = 1 Then
If LCase(Target.Value) = "1000" Then
With Target.EntireRow
.Copy Sheets("Week Schedule").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
.Delete
End With
End If
End If
End Sub
如果我们在第三列输入-1,它将清除该行。如果我们输入 1000,它将被复制到另一个工作表并从当前工作表中删除。
上面的代码工作正常。我不想清除行数据,而是想删除该行。 所以加了
Line 4 With Target.EntireRow.ClearContents to With Target.EntireRow.Delete
但它显示一个错误。
【问题讨论】:
-
请用
= -1替换= "-1"。否则,条件将查找字符串。然后,不需要两次迭代。你可以只用一个。并且您必须在删除范围之前将EnableEvents设为False,这将再次触发Change事件... -
我想您想要检查数字(-1 和 1000),而不是看起来像数字的字符串。这种理解正确吗?
-
删除第 4 行的
With和End With。
标签: excel vba excel-formula excel-2010