将用户表单文本框的输入限制为[H]:MM

时间:2019-04-24 12:56:57

标签: excel vba

我在用于时间分配的多个用户表单上有多个文本框。为了简单起见,请说userform1&userform2,每个都带有textbox1&textbox2。 Userform1用于用户输入,将值放入表中,而userform2从该表中提取值并显示在相关的文本框中。我需要将这些框的输入和显示都限制为[H]:mm格式,其中分钟不能超过59,而小时可以是25+,即125:59而不是4:67

我尝试将这些线程以及其他线程的代码组合在一起,但是似乎无法使其正常工作。

Excel VBA Textbox time validation to [h]:mm

Time format of text box in excel user form

最终我只是试图通过消息框来操纵用户输入,但这仍然使条目容易出错

Sub FormatHHMM(textbox As Object)

Dim timeStr As String

With textbox

'Check if user put in a colon or not
     If InStr(1, .Value, ":", vbTextCompare) = 0 And Len(.Value) > 1 Then

        MsgBox "Please use HH:mm Format"

        textbox.Value = ""
        textbox.SetFocus
    Else
        If Right(.Value, 2) > 60 Then

        MsgBox "Minutes cannot be more than 59"

        textbox.Value = ""
        textbox.SetFocus

        End If

End If
End With


End Sub

这允许用户放入字母字符,即使从表中调用时正确输入也显示为值,即5.234 ...而不是125:59

4 个答案:

答案 0 :(得分:2)

如何在同一输入框上将小时和分钟分为两个单独的输入字段。 因此,用户必须输入小时数,然后在接下来的字段中输入分钟。这样,您可以检查输入的数字和秒数,大于60的秒。 我知道这不是理想的方法,但这将是规避给定问题的一种方法。

答案 1 :(得分:1)

您是否尝试过使用Like运算符?这样可以检查每个字符位置的数值。我会这样:

Function FormatCheck(ByVal strEntered As String)

Dim correctformat As Boolean

If strEntered Like "*#:##" And IsNumeric(Mid(strEntered, 1, InStr(1, strEntered, ":", 1) - 1)) Then
    If Mid(strEntered, InStr(1, strEntered, ":", 1) + 1, 999) <= 59 Then
        correctformat = True
    End If
End If

If Not correctformat Then FormatCheck = "Incorrect format"

End Function

这需要在“:”之前至少有一个数字

Testing

编辑:下面是Sub版本,而不是使用Function。将会像您最初使用的那样弹出一个MsgBox。您可能会用它替换整个FormatHHMM子,而不会产生任何不利影响。

Sub FormatCheck(ByVal strEntered As String)

Dim correctformat As Boolean

If strEntered Like "*#:##" And IsNumeric(Mid(strEntered, 1, InStr(1, strEntered, ":", 1) - 1)) Then
    If Mid(strEntered, InStr(1, strEntered, ":", 1) + 1, 999) <= 59 Then
        correctformat = True
    End If
End If

If Not correctformat Then MsgBox "Incorrect format"

End Sub

答案 2 :(得分:0)

我认为这可能会有所帮助:

Option Explicit

Sub test()

    Dim str As String

    str = TextBox.Value

    'Test string lenght. Maximun lenght number 4
    If Len(str) <> 4 Then
        MsgBox "Enter a valid time. Proper number of digits are 4."
        Exit Sub
    End If

    'Test if string includes only one ":"
    If (Len(str) - Len(Replace(str, ":", ""))) / Len(":") <> 1 Then
        MsgBox "Use only one "":"" to separate time."
        Exit Sub
    End If

    'Test how many digits are before and after ":"
    If InStr(1, str, ":") <> 2 Then
        MsgBox """:"" position should be place 2."
        Exit Sub
    End If

    'Test if number 1,3 & 4 are number
    If IsNumeric(Mid(str, 1, 1)) = False Or IsNumeric(Mid(str, 1, 1)) = False Or IsNumeric(Mid(str, 1, 1)) = False Then
        MsgBox "Enter number in position 1,3 and 4."
        Exit Sub
    End If

   'Test 2 last to digits
    If Right(str, 2) <= 60 Then
        MsgBox "Second limit is 60."
        Exit Sub
    End If


End Sub

答案 3 :(得分:0)

您可以使用正则表达式:

Sub inputTimeFormat()
    Dim userInput As String
    Dim strPattern As String
    Dim msgBoxText As String
    Dim regEx As New RegExp
    Dim objRegex As Object

    strPattern = "(^[0-9]+):([0-5])([0-9])$"
    msgBoxText = "Insert time in HH:mm, or hit Cancel to escape"
    Set objRegex = CreateObject("vbscript.regexp")

    With regEx
        .ignorecase = True
        .Pattern = strPattern
        Do
            If userInput <> vbNullString Then msgBoxText = "PLEASE RETRY" & Chr(13) & msgBoxText
            userInput = Application.InputBox(msgBoxText, Default:="17:01")
            If userInput = "False" Then
                MsgBox "User hit cancel, exiting code", vbCritical
                Exit Sub
            End If
        Loop Until .Test(userInput)
    End With

    MsgBox "Format OK"

End Sub

(您需要激活正则表达式:在VBA中,“工具”>“参考”>选中“ Microsoft VBScript正则表达式5.5”>“确定”) 有关How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops

的更多详细信息
相关问题