I have a sheet for data entry in cols B and C. Values in col C must be larger than in B, I have the below code to check this.
But when I return to B, and I make a change there, I can still enter an invalid number. I could trap this through similar code for col B, but then I drive in circles...
Any suggestions ?
But when I return to B, and I make a change there, I can still enter an invalid number. I could trap this through similar code for col B, but then I drive in circles...
Any suggestions ?
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim col_B_rng As Range
Dim col_C_rng As Range
Set col_B_rng = Range("B2:B10")
Set col_C_rng = Range("C2:C10")
'check that value in col C is larger than in col B
If Not Application.Intersect(Target, col_B_rng) Is Nothing Then
Target.Offset(0, 1).Select
End If
If Target.Count = 1 And Not Application.Intersect(Target, col_C_rng) Is Nothing Then
If Target.Value <= Target.Offset(0, -1).Value Then
Application.EnableEvents = False
Target.Select
MsgBox ("values in col C must be larger than in col B"), vbCritical
Application.Undo
Application.EnableEvents = True
Else
Target.Offset(1, -1).Select
End If
End If
End Sub