【问题标题】:Textbox restriction error Excel VBA文本框限制错误 Excel VBA
【发布时间】:2016-02-10 15:35:39
【问题描述】:

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

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

结束子

这是一个文本框,其中“,”被限制。

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    使用下面的子。

    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
    

    【讨论】:

    • TextBox1 替换为TB1,以防您的Textbox
    • 仍然收到相同的错误 INVALID PROCEDURE CALL 或 Argument RUN TIME 错误 5 并且同一行突出显示
    • 但是我测试了它并且它的工作。能否分享一个示例工作簿,以便我们实时检查。
    • 只写ABC,它会给你消息点击确定使用退格,它会崩溃
    猜你喜欢
    • 2019-09-13
    • 1970-01-01
    • 1970-01-01
    • 2014-07-24
    • 1970-01-01
    • 2013-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多