用户窗体文本框的最大值问题

Issue with Maximum Value for Userform Textbox

我在从 SQL 中提取表格的用户表单中为教科书创建了最大值。此用户表单链接到工作簿单独部分中的 SQL 查询。

在用户表单中,用户可以在文本框 (txtrownum) 中指定他们想要 returned 的行数。他们可以 return 的最大值是 100,000。

如果超过 100,000,我会出现 MsgBox 表示最大金额为 100,000,然后文本框值自动填充为 100,000。

该代码效果很好,在下面。

 Private Sub txtrownum_Change()

    'Here needs to be code where you can erase after it autocorrects,
    'the problem is when you try to erase it pulls up message box again and 
    'won't let you change it

    If txtrownum.Value > 100000 Then
        MsgBox "Maximum Number of Rows is 100,000"
        txtrownum.Value = "100000"
    End If
End Sub

但是,一旦 100,000 自动填充,如果用户想要完全删除该值并输入 100,000 以下的行号,则 Msgbox 会重新出现并重新自动填充 100,000。如果用户突出显示 100,000,他们可以毫无问题地覆盖它。但如果他们突出显示“100,000”并按退格键,则该消息会重新出现。有什么建议吗?

首先尝试检查文本框值 isNumeric

正如@MathieuGuindon 指出的那样,最好先检查用户输入的是不是数字,然后再在其中进行额外检查。

如果不是,则您可以在 Else 部分执行其他步骤。

 Private Sub txtrownum_Change()
    'Here needs to be code where you can erase after it autocorrects,
    'the problem is when you try to erase it pulls up the message box again and
    'won't let you change it

    If IsNumeric(txtrownum.Value) Then

        'Now that we know it is a number, now we can do the check.
        If txtrownum.Value > 100000 Then
            MsgBox "Maximum Number of Rows is 100,000"
            txtrownum.Value = "100000"
        End If
    Else
        'do something if not a number
    End If
End Sub