Determine if cell contains data validation
I know this question is old, but since it comes up when Googling "excel vba check if cell has validation", I figured I would add my grain of salt.
If the Range
object on which you call SpecialCells
represents only a single cell, the entire sheet will be scanned to find matches. If you have a very large amount of data, the methods provided in previous answers may become a bit slow.
Hence, here is a more efficient way to check if a single cell has validation:
Function HasValidation(cell As Range) As Boolean
Dim t: t = Null
On Error Resume Next
t = cell.Validation.Type
On Error GoTo 0
HasValidation = Not IsNull(t)
End Function
Dim cell As Range, v As Long
For Each cell In Selection.Cells
v = 0
On Error Resume Next
v = cell.SpecialCells(xlCellTypeSameValidation).Count
On Error GoTo 0
If v = 0 Then
Debug.Print "No validation"
Else
Debug.Print "Has validation"
End If
Next
If you only want to test the activecell, then:
Sub dural()
Dim r As Range
On Error GoTo noval
Set r = Cells.SpecialCells(xlCellTypeAllValidation)
If Intersect(r, ActiveCell) Is Nothing Then GoTo noval
MsgBox "Active cell has validation."
Exit Sub
noval:
MsgBox "Active cell has no validation."
On Error GoTo 0
End Sub