文本框限制错误Excel VBA

如果文本框是空的,我按退格键崩溃,突出显示行是

TB1 =左(TB1,Len(TB1)-1)

我可以改变什么来阻止这个崩溃

Private Sub TB1_Change() Dim strStrings As String, LastLetter As String Application.EnableEvents = False LastLetter = Right(TB1, 1) strStrings = "," If InStr(1, strStrings, LastLetter) > 0 Then MsgBox LastLetter & " not allowed" TB1 = Left(TB1, Len(TB1) - 1) End If Application.EnableEvents = True 

结束小组

这是一个限制“,”的文本框。

使用下面的子。

 Private Sub TB1_Change() On Error GoTo HarunErrHandler Dim strStrings As String, LastLetter As String If TB1 = "" Then Exit Sub End If Application.EnableEvents = False LastLetter = Right(TB1, 1) strStrings = "," If Val(InStr(1, strStrings, LastLetter)) > 0 Then MsgBox LastLetter & " not allowed" TB1 = Left(TB1, Len(TB1) - 1) End If Application.EnableEvents = True Exit Sub HarunErrHandler: MessageBox = MsgBox("Error Number: " & Err.Number & vbCrLf & Err.Description, vbCritical, "Error") End Sub