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

时间:2019-06-25 17:33:37

标签: excel vba userform

我已经在用户表单中为教科书创建了一个最大值,该表单可以从SQL中提取表格。此用户窗体链接到工作簿的单独部分中的SQL查询。

在用户表单中,用户可以在文本框(txtrownum)中指定要返回多少行。他们最多可以返回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”并按退格键,则会再次出现该消息。有什么建议吗?

1 个答案:

答案 0 :(得分:4)

尝试先检查文本框的值是否为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