Check if cell has data validation with VBA

neodjandre

Well-known Member
Joined
Nov 29, 2006
Messages
950
Office Version
  1. 2019
Platform
  1. Windows
Hello,

How can I check if a cell has data validation with a VBA procedure?

I want to do this because I am using a procedure to add data validation and I get an error if the cell already contains data validation...

thanks
andy
 
I have copied the code below from the post here above but it dos not have an "End Sub" or and "End With". I keep getting an error with the code.

Sub DELETE_VALIDATION()

With Range("B2:AV217").Validation
.DELETE
.Add Type:=xlValidateWholeNumber, AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, Formula1:="4", Formula2:="6"
 
Upvote 0

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Try

Code:
Sub DELETE_VALIDATION()

With Range("B2:AV217").Validation
    .Delete
    .Add Type:=xlValidateWholeNumber, AlertStyle:=xlValidAlertStop, _
    Operator:=xlBetween, Formula1:="4", Formula2:="6"

End With
End Sub
 
Upvote 0
I used this code and it works. Now if I wanted to also output the name of the list validated here, how do I do that?

Thank you
This might do what you want
Code:
Sub test()
    On Error Resume Next
        If ActiveCell.SpecialCells(xlCellTypeSameValidation).Cells.Count < 1 Then
            MsgBox "Active Cell does not have validation"
        Else
            MsgBox "Active cell has Validation"
        End If
    On Error GoTo 0
End Sub
 
Upvote 0
I don't have Excel with me at the moment, but I think that if you look at the .Formula1 property of something inside ActiveCell.Validation it will have the range from which the validation list comes. The ObjectBrowser will help you get from a range object to the .Formula1 property.
 
Upvote 0
I got home and did some testing.

I put List Validation on three cells (three styles of list source) the these are the returns from ThatCell.Validation.Formula1

=$A$1:$A$3

=MyNamedRange

Bob,Carol,Ted,Alice
 
Upvote 0
Would you know how I can translate this is in MATLAB? I am able to identify the type but not being able to then assign the values of the named range to a cell.
This might do what you want
Code:
Sub test()
    On Error Resume Next
        If ActiveCell.SpecialCells(xlCellTypeSameValidation).Cells.Count < 1 Then
            MsgBox "Active Cell does not have validation"
        Else
            MsgBox "Active cell has Validation"
        End If
    On Error GoTo 0
End Sub
 
Upvote 0
I know this an old thread but what about this solution...
Code:
Sub validationtest()
If InStr(ActiveCell.SpecialCells(xlCellTypeAllValidation).Address, ActiveCell.Address) = False Then
MsgBox ActiveCell.Address & " does not have validation"
Else
 MsgBox ActiveCell.Address & " cell has Validation"
        End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

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