13

I am writing a VBA code that goes through a range of cells checking if each cell has data validation (drop down menu) and if not assign one to it from a list on another sheet.

I currently have trouble with the line that checks if the current cell already has data validation. I get error 1004 "no cells were found".

Sub datavalidation()

    Dim nlp As Range
    Dim lrds As Long
    Dim wp As Double
    Dim ddrange As Range

    Sheets("DataSheet").Select

        lrds = ActiveSheet.Range("A1").Offset(ActiveSheet.rows.Count - 1, 0).End(xlUp).Row

        Set nlp = Range("I3:I" & lrds)

        For Each cell In nlp

    'error on following line

            If cell.SpecialCells(xlCellTypeSameValidation).Cells.Count < 1 Then
                wp = cell.Offset(0, -8).Value

                Set ddrange = ddrangefunc(wp)

            End If

        Next

End Sub

Any ideas? Thank you

Community
  • 1
  • 1
user2385809
  • 955
  • 7
  • 15
  • 26

8 Answers8

34

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
AgentRev
  • 749
  • 1
  • 8
  • 20
  • 2
    Do note that you can pass a multi-cell range in to `cell`. If you do and part of that range contains data validation then it'll incorrectly report `False`. – IvenBach Apr 17 '18 at 21:50
16
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
Tim Williams
  • 154,628
  • 8
  • 97
  • 125
  • Tim's answer should be fine for you, but you may want to limit the error handler to providing that response for that specific error number. – dennythecoder Sep 05 '13 at 18:13
6

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
Gary's Student
  • 95,722
  • 10
  • 59
  • 99
  • If you are truly interested in checking if a single cell has validation, this is the most efficient way to do it. If you're checking all cells in a worksheet, this could be modified to get the range r once, then cycle through the Intersections of each cell and range r to add the validation to cells that do not have it. – GlennFromIowa Jun 14 '17 at 15:10
2

Looking for a way to handle this avoiding the error resume next. This is the way I implemented it:

Option Explicit
' https://stackoverflow.com/questions/18642930/determine-if-cell-contains-data-validation
' Use this if you want to omit doing something to the cell added: http://dailydoseofexcel.com/archives/2007/08/17/two-new-range-functions-union-and-subtract/
Sub ValidationCells()

    Dim theSheet As Worksheet
    Dim lastCell As Range
    Dim validationRange As Range
    Dim validationCell As Range
    
    Application.EnableEvents = False ' optional
    
    Set theSheet = ThisWorkbook.Worksheets(1)
    
    theSheet.Unprotect ' optional
    
    ' Add a cell with a value and some validation to bypass specialcells error
    Set lastCell = theSheet.Cells(1, theSheet.Cells.Columns.Count)
    With lastCell
        .Value2 = 1
        .Validation.Add xlValidateWholeNumber, xlValidAlertInformation, xlEqual, "1"
    End With
    
    ' If usedrange is greater than 1 (as we added a single cell previously)
    If theSheet.UsedRange.Rows.Count > 1 Or theSheet.UsedRange.Columns.Count > 1 Then
    
        Set validationRange = theSheet.UsedRange.SpecialCells(xlCellTypeAllValidation)
        
        MsgBox validationRange.Address
        
        For Each validationCell In validationRange
            If validationCell.Address <> lastCell.Address Then
                MsgBox validationCell.Address
            End If
        Next validationCell
        
    End If
    
    lastCell.Clear
    
    Set validationRange = Nothing
    Set lastCell = Nothing
    
    theSheet.Protect ' optional
    
    Application.EnableEvents = True ' optional
    

End Sub
Ricardo Diaz
  • 5,658
  • 2
  • 19
  • 30
1

Also, if you'd like to get the validation Source you can use the following...

Dim cell as Range
Dim rng as Range
Set rng = Range("A1:A10") 'enter your range

On Error Resume Next 'will skip over the cells with no validation

For Each cell In rng
    msgbox cell.Validation.Formula1
Next cell
CRUTER
  • 866
  • 4
  • 12
1

About 4 years later, I am looking for cell validation as well. Combining a few from the answers here, this is what I came up with:

Option Explicit

Public Sub ShowValidationInfo()

    Dim rngCell             As Range
    Dim lngValidation       As Long

    For Each rngCell In ActiveSheet.UsedRange

        lngValidation = 0

        On Error Resume Next
        lngValidation = rngCell.SpecialCells(xlCellTypeSameValidation).Count
        On Error GoTo 0

        If lngValidation <> 0 Then
            Debug.Print rngCell.Address
            Debug.Print rngCell.Validation.Formula1
            Debug.Print rngCell.Validation.InCellDropdown
        End If
    Next

End Sub
Vityata
  • 42,633
  • 8
  • 55
  • 100
1
Function isValidated(ByVal Cell as Range) as Boolean
    On Error Resume Next
    isValidated = Not isEmpty(Cell.Validation.Type)
End Function
Vitalizzare
  • 4,496
  • 7
  • 13
  • 32
0

This works for me

Sub test()
    On Error Resume Next
        If ActiveCell.SpecialCells(xlCellTypeSameValidation).Cells.Count < 1 Then
            MsgBox "validation"
        Else
            MsgBox "no Validation"
        End If
    On Error GoTo 0
End Sub
Sandro Wiggers
  • 4,440
  • 3
  • 20
  • 25