Home » excel » Cell validation – checking if date [Excel VBA]

Cell validation – checking if date [Excel VBA]

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have some code that is running on the background. Part of it, I would like to check if cells in Col13 (sheet = ‘Project_Name’) have values that are in date format. If not, I would like to notify the users.

Col13 will be a date if flagged cells in Admin sheet are marked as ‘True’ – just so you know understand what is first part of my code is for.

CODE

'Checking date of activity
For i = 7 To LastRow
    With Worksheets("Admin")
        If .Cells(i, 13).Value = "True" Or .Cells(i, 11).Value = "True" Then
            With Worksheets("Project_Name")
                If Not IsDate(.Cells(i, 13)) Then
                    MsgBox ("Please ensure that Date of Activity is in the correct Date format.")
                    Exit Function
                End If
            End With
        End If
    End With
Next i

Albeit a very simple loop check, it does not work. I am not sure if my statement:

If Not IsDate()

is correct?

Any advise would be appreciated.

How to&Answers:

If columns K and M on the Admin worksheet contain truly boolean values (center aligned TRUE or FALSE on the worksheet) then you shouldn’t be comparing them to strings that look like "True". Their underlying raw boolean value is sufficient to evaluate.

If .Cells(i, 13).Value Or .Cells(i, 11).Value Then
    ...
end if