Validation.Type

Roderick_E

Well-known Member
Joined
Oct 13, 2007
Messages
2,051
Apparently some cells don't have any validation types set??? Thus my following code throws an error popup?

Code:
Sub valtype()
MsgBox ActiveCell.Validation.Type
End Sub

ERROR:
Runtime error 1004
Application-defined or object-defined error

I want to check for this potentiality and then do something else.

Please help. Thanks
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
You could do something like this

Code:
vType = ""
On Error Resume Next
vType = Range("A1").Validation.Type
If vType <> "" Then
    MsgBox vType
Else
    MsgBox "No validation set"
End If
 
Upvote 0
This UDF returns True if all cells in its argument have some form of data validation.
Code:
Function HasDataValidation(R As Range) As Boolean
'returns true if all cells in R have Data Validation, even if the type of DV varies among the cells in R
Dim c As Range, T As Boolean
For Each c In R
    On Error Resume Next
    T = c.Validation.Type
    If Err.Number <> 0 Then
        Err.Clear
        HasDataValidation = False
        Exit Function
    End If
Next c
HasDataValidation = True
End Function
 
Upvote 0
Thanks all. I went with JoeMo's UDF since I can deploy it more globally even though sericom answered the exact issue.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top