我具有标记D列单元格更改时间的功能。现在,我想向worksheet_change事件添加另一个函数,如果在D列单元格中写入了特定的文本,它将相应地更改E列。
我遇到的问题是,尽管条件不满足(其他文本写入D列单元格),但E列每次都会更改。有人可以指出正确的方向吗,我对VBA还是很陌生,所以也许我错过了一些东西。
我的代码:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim xCellColumn As Integer
Dim xTimeColumn As Integer
Dim xPaymentColumn As Integer
Dim xRow, xCol As Integer
xCellColumn = 4
xTimeColumn = 23
xPaymentColumn = 5
'part for marking date, works as intended
On Error Resume Next
xRow = Target.Row
xCol = Target.Column
If Target.Text <> "" Then
If xCol = xCellColumn Then
Cells(xRow, xTimeColumn) = Now()
End If
End If
'part for changing column E according column D selection
'I would like this part to trigger only if text cell in column D changed _
to "Send request" or "Start evaluation", if any other text is written in column D, _
column E should remain intact
If Target.Text = "Send request" Or "Start evaluation" Then
If xCol = xCellColumn Then
Cells(xRow, xPaymentColumn) = "Yes"
End If
End If
End Sub
答案 0 :(得分:5)
将您的If
更改为
If Target.Text = "Send request" Or Target.Text = "Start evaluation" Then
您当前的if-Condition引发错误Type mismatch
,但是您正在用On Error Resume Next
-消息吞噬所有错误。因此,VBA会按照您的要求进行操作:忽略运行时错误并愉快地执行下一条语句-If块中的语句。
一般建议:切勿使用On Error Resume Next