【问题标题】:Restrict Userform Textbox input to [H]:MM将用户表单文本框输入限制为 [H]:MM
【发布时间】:2019-04-24 12:56:57
【问题描述】:

我在多个用于时间分配的用户窗体上有多个文本框。为简单起见,说 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

【问题讨论】:

  • 您是否希望根据这些输入在表中存储字符串或小数?
  • 您可以检查数字输入
  • @MichaelMurphy 我会说字符串。表格上的信息稍后将被打印到需要小时和分钟的 word 文档中。
  • @JvdV 我试过if not Isnumeric 但它在冒号上触发并告诉我这不是数字。
  • @RappaportXXX,你可以先检查冒号,然后检查左右两边的数值吗?

标签: excel vba


【解决方案1】:

如何将小时和分钟拆分为同一个输入框上的两个单独的输入字段。 所以用户必须输入小时和下一个字段分钟。这样,您可以检查输入的 isnumeric 和 >60 的秒数。 我知道这并不理想,但这是一种规避给定问题的方法。

【讨论】:

    【解决方案2】:

    您是否尝试过使用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
    

    这要求“:”前至少有一个数字

    编辑:下面是Sub 版本,而不是使用Function。这将弹出一个MsgBox,就像您最初使用的那样。你可以用这个替换你的整个FormatHHMM sub而不会有任何不利影响。

    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
    

    【讨论】:

    • 我的功能不是很好,我会把它放在哪里?在我的潜艇内还是完全更换潜艇?
    • 函数真的很有价值,我相信你已经在理解它们的工作原理了,因为你的代码中有Sub FormatHHMM(textbox As Object)。我会尽快编辑,为您提供我的方法的Sub 版本。
    • 抱歉延迟返回这个,玩弄这个并让它根据你的代码工作。正好及时被告知不再需要我的项目,但无论如何我都学到了一些东西,哈哈。谢谢。
    【解决方案3】:

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

    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
    

    【讨论】:

      【解决方案4】:

      你可以使用正则表达式:

      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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-20
        • 1970-01-01
        • 2017-03-09
        • 2020-05-21
        • 1970-01-01
        相关资源
        最近更新 更多