mikke3141
Board Regular
- Joined
- Nov 11, 2008
- Messages
- 88
Hello,
When running the following macro, I get error message: Method 'Undo' of object ' _Application' failed, and then excel closes down.
What could be the cause? Thank you for your help.
When running the following macro, I get error message: Method 'Undo' of object ' _Application' failed, and then excel closes down.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$2" Then
Dim RE As Object
Set RE = CreateObject("vbscript.RegExp")
RE.Pattern = "^(0[1-9]|[12]\d|3[01])(0[1-9]|1[0-2])\d\d[+\-A]\d{3}[0-9ABCDEFHJKLMNPRSTUVWXY]"
Cells(2, 2) = UCase(Cells(2, 2))
If RE.test(Cells(2, 2)) = False Then
MsgBox "The cell value is wrong format. Correct format is 311299-1166A"
Application.Undo
Exit Sub
End If
End If
End Sub