Determine if cell is using data validation

Roderick_E

Well-known Member
Joined
Oct 13, 2007
Messages
2,051
Say I'm looping through rows 1-50, columns 1-10 and I want to determine if a cell is using data validation. How can I determine that?
I tried .validation but that must not be it. Thanks

NEVERMIND
-- look like validation.type = 3
 
Last edited:

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
.validation what, looking at other code it seems to be ok.

I'm sure you are aware of this but i will throw it in for anyone reading the thread. there is data validation option on the find and selct on the home tab to show any data validation on the sheet
 
Upvote 0
.validation what, looking at other code it seems to be ok.

I'm sure you are aware of this but i will throw it in for anyone reading the thread. there is data validation option on the find and selct on the home tab to show any data validation on the sheet

Thanks I need VBA since I'm resetting some cells. type=3 isn't working perfectly yet.
 
Upvote 0
How about specialcells
Code:
Cells.SpecialCells(xlCellTypeAllValidation).Select
 
Upvote 0
Using a function...
Code:
Sub testValid()
    Dim myCell As Range

    For Each myCell In Range("A1:J50")
        If HasVal(myCell) Then Debug.Print myCell.Address
    Next
End Sub

Function HasVal(cTest As Range) As Boolean
    Dim vTest
    vTest = Null

    On Error Resume Next
    vTest = cTest.Validation.Type
    On Error GoTo 0

    HasVal = Not IsNull(vTest)
End Function

or following on from Fluff's suggestion...

Code:
Sub testValid2()
    Dim myCell As Range, myVal As Long

    For Each myCell In Range("A1:J50")
        myVal = 0

        On Error Resume Next
        myVal = myCell.SpecialCells(xlCellTypeSameValidation).Count
        On Error GoTo 0

        If myVal <> 0 Then Debug.Print myCell.Address

    Next

End Sub
 
Upvote 0
Using a function...
Code:
Sub testValid()
    Dim myCell As Range

    For Each myCell In Range("A1:J50")
        If HasVal(myCell) Then Debug.Print myCell.Address
    Next
End Sub

Function HasVal(cTest As Range) As Boolean
    Dim vTest
    vTest = Null

    On Error Resume Next
    vTest = cTest.Validation.Type
    On Error GoTo 0

    HasVal = Not IsNull(vTest)
End Function

or following on from Fluff's suggestion...

Code:
Sub testValid2()
    Dim myCell As Range, myVal As Long

    For Each myCell In Range("A1:J50")
        myVal = 0

        On Error Resume Next
        myVal = myCell.SpecialCells(xlCellTypeSameValidation).Count
        On Error GoTo 0

        If myVal <> 0 Then Debug.Print myCell.Address

    Next

End Sub

Thanks. I'll give these a try. I thought type 3 was always validation types but apparently not.
 
Upvote 0
Doesn't this error if a cell doesn't have validation?
Yes, but you'd wrap it in an error handler, in the same way that Mark858 has done in post#6
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

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