Search code examples
vbaexcelexcel-2010

Determine if cell contains data validation


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


Solution

  • 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