Target.Validation.Type导致“应用程序定义或对象定义”错误

我试图确定一个单元格是否有数据validation使用此语句:

If Target.Validation.Type = 3 Then 

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

应用程序定义的或对象定义的错误

我试着用On error Resume NextOn error Goto 0但它没有帮助。

如何检查单元是否包含数据validation?

这是一种方法。 这将检查工作表中的任何单元格是否具有validation。 如果不存在,则退出分支。 如果有,那么它检查当前单元是否是那些validation单元的一部分

 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