Target.Validation.Type 导致 "Application-defined or object-defined" 错误

Target.Validation.Type results in "Application-defined or object-defined" error

我正在尝试使用以下语句确定单元格是否具有数据验证:

If Target.Validation.Type = 3 Then

但是,我在 VBA 中遇到错误:

application-defined or object-defined error

我尝试使用 On error Resume NextOn error Goto 0,但没有用。

如何检查单元格是否包含数据验证?

这是一种方法。这将检查 sheet 中的任何单元格是否已验证。如果没有,则退出子程序。如果有,则它检查当前单元格是否是那些验证单元格的一部分

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim r As Range

    Application.EnableEvents = False

    On Error Resume Next
    Set r = Cells.SpecialCells(xlCellTypeAllValidation)
    On Error GoTo 0

    If Not r Is Nothing Then
        On Error GoTo Whoa

        If Not Intersect(Target, r) Is Nothing Then
            If Target.Validation.Type = 3 Then
                '
                '~~> Your code
                '
            End If
        End If
    End If

Letscontinue:
    Application.EnableEvents = True
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume Letscontinue
End Sub

如果您的错误检查选项是 VBA(工具、选项、常规)中的“遇到未处理的错误时中断”,则忽略“出错时继续下一步”。