Recently, I found a code on a book for data validation, which is:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = “$A$1” Then
If Not IsNumeric(Target) Then
MsgBox “Enter a number in cell A1.”
Range(“A1”).ClearContents
Range(“A1”).Activate
End If
End If
End Sub
I would like to change it to validate my custom format in column A which is XY & 6 number (XY123456) and modified the code. But the MsgBox will pop up continuously and I cannot close it when the format is wrong. Could someone give me some advice. Thanks
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 Then
If Left(Target.Value, 2) <> "XY" Or
Not IsNumeric(Right(Target.Value,6)) Or
Len(Target.Value) <> 8 Then
MsgBox “Wrong Format”
Target.ClearContents
Target.Activate
End If
End If
End Sub