事件宏引发错误“对象必需”

问题描述 投票:1回答:2

我有这个简单的事件宏,由于某种原因抛出我

所需对象

这条线上的错误If Not AppDate Is Nothing Then。知道可能导致这种情况的原因吗?

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim LastRow As Integer
    Dim ThisRow As String
    Dim AppDate As Variant
    Application.EnableEvents = False

    LastRow = Cells(Rows.Count, "C").End(xlUp).Row
    If Target.Column = 3 Then
        ThisRow = Target.Row
        AppDate = Target.Value
        If Not AppDate Is Nothing Then
            (...) Recalculate Date in Column F
        Else
        End If
    End If

    Application.EnableEvents = True
End Sub

任何建议将不胜感激。谢谢!

excel vba excel-vba object events
2个回答
4
投票

使用:

…
If Not IsEmpty(AppDate) 
…

为了帮助澄清,here is a lengthy tutorial关于Nothing。大声笑


7
投票

Is运算符使用对象而不是值。


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim x As Variant
    Dim y As New Collection
    Dim z As Object

    x = Target.Value

    If IsEmpty(x) Then
        MsgBox "may be use this."
    End If

    MsgBox TypeName(x)

    If y Is Nothing Then
     MsgBox "Works"
    End If

    If z Is Nothing Then
     MsgBox "Works"
    End If

    If x Is Nothing Then '/This won't work

    End If


End Sub
© www.soinside.com 2019 - 2024. All rights reserved.