Macro_Nerd99
Board Regular
- Joined
- Nov 13, 2021
- Messages
- 61
- Office Version
- 365
I have a template where all the cells from A2:C10000... are formatted as a custom format: mm/dd/yyyy h:mm
However, if someone goes and messes with the formatting, it can ruin some calculations.
For example, like shown in this picture, someone can remove a space and cause the "AM" to show, which is bad. However, this change in format isn't detected in an if statement.
If .Range("A" & thisrow & ":B" & thisrow).NumberFormat = "mm/dd/yyyy h:mm" Then
Range("A" & thisrow & ":B" & thisrow).Interior.ColorIndex = 37
Else
Range("A" & thisrow & ":B" & thisrow).Interior.ColorIndex = 3
End If.
How do I add to this code below for cells to turn red if they're not displaying proper formatting (like the blue cell in the picture). ?
However, if someone goes and messes with the formatting, it can ruin some calculations.
For example, like shown in this picture, someone can remove a space and cause the "AM" to show, which is bad. However, this change in format isn't detected in an if statement.
If .Range("A" & thisrow & ":B" & thisrow).NumberFormat = "mm/dd/yyyy h:mm" Then
Range("A" & thisrow & ":B" & thisrow).Interior.ColorIndex = 37
Else
Range("A" & thisrow & ":B" & thisrow).Interior.ColorIndex = 3
End If.
How do I add to this code below for cells to turn red if they're not displaying proper formatting (like the blue cell in the picture). ?
VBA Code:
Sub Time_Card_Validation()
Dim lr As Long, i As Long
Dim rng As Range, cel As Range
Dim Stime As Date, Etime As Date, PrevEtime As Date
lr = Range("A" & Rows.Count).End(xlUp).Row
Set rng = Range("A2:A" & lr)
For Each cel In rng
' Next 2 lines for testing
'Stop ' Now use F8 key
'cel.Select
If IsDate(cel.Value) Then
' start time
Stime = cel.Value
' current end
Etime = cel.Offset(, 1).Value
' previous end
If cel.Row = 2 Then
PrevEtime = 1
Else
PrevEtime = cel.Offset(-1, 1).Value
End If
Else
'Exit Sub
End If
' deal with the times
' start times
If Stime <= PrevEtime Then
cel.Interior.ColorIndex = 3
cel.Offset(-1, 1).Interior.ColorIndex = 3
MsgBox "Warning: There is(Are) time Lapse(s) present" & vbCrLf & " Please fix red cells"
Else
If Stime > PrevEtime And cel.Offset(, 1) = "" Then
cel.Interior.ColorIndex = 37
ElseIf Stime > PrevEtime And Stime < Etime Then
cel.Interior.ColorIndex = 37
ElseIf Stime > PrevEtime And Stime >= Etime Then
cel.Interior.ColorIndex = 3
MsgBox "Note: End Time cannot be later than start time" & vbCrLf & " Please fix red Cells"
End If
End If
'end times
If cel.Offset(, 1) = "" Then
cel.Offset(, 1).Interior.ColorIndex = 37
ElseIf Etime > Stime Then
cel.Offset(, 1).Interior.ColorIndex = 37
ElseIf Etime <= Stime Then
cel.Offset(, 1).Interior.ColorIndex = 3
End If
Next cel
End Sub