我對以下代碼有疑問
TestCheck:
Dim Comm as Range
Dim TestComment as Range
Application.EnableEvents = False
Set TestComment = Intersect(Application.ActiveSheet.Range("I:I"), Target)
On Error GoTo Cancellation
If TestComment = "" Then
Else
For Each Comm In TestComment
If Not VBA.IsEmpty(Comm.Value) Then
Comm.Offset(0, 1).Value = Date
Comm.Offset(0, 1).NumberFormat = "dd/mm/yyyy"
Application.EnableEvents = True
End If
Next
End If
它不斷提出錯誤
'物件變數或未設定塊變數'
在 If 陳述句上
If TestComment = "" Then
我知道它等于空,所以它嚇壞了,但我不介意它不等于空,實際上在這種情況下,如果它不等于空,它什么也不做,而不是拋出錯誤。On Error GoTo 似乎也不起作用。
uj5u.com熱心網友回復:
如果 Target 不在列中I,則函式Intersect將回傳Nothing,因此TestComment設定為Nothing。
您無法檢查 的值Nothing,您需要檢查它是否設定為某個值,您可以使用is Nothing它。
我假設您的代碼是事件例程的一部分。EnableEvents請注意,當代碼仍在作業時,您不應設定
為 True - 將其放在代碼的末尾(在 label 后面Cancellation)以確保即使發生錯誤也能執行它。
Application.EnableEvents = False
On Error GoTo Cancellation
Dim testComment As Range, comm As Range
Set testComment = Intersect(Range("I:I"), Target)
If Not testComment Is Nothing Then
For Each comm In testComment
If Not IsEmpty(comm.Value) Then
comm.Offset(0, 1).Value = Date
comm.Offset(0, 1).NumberFormat = "dd/mm/yyyy"
End If
Next
End If
Cancellation:
Application.EnableEvents = True
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/483643.html
上一篇:根據r中的多個條件創建新列
