【问题标题】:Time format of text box in excel user formexcel用户表单中文本框的时间格式
【发布时间】:2017-07-11 12:37:23
【问题描述】:

我有以下代码在用户窗体中设置文本框“txtA”的时间格式。

Private Sub txtA_Afterupdate()
Dim tString As String
 With txtA

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

'If not, make string 4 digits and insert colon
 tString = Format(.Value, "0000")
 tString = Left(tString, 2) & ":" & Right(tString, 2)
 txtA.Value = Format(TimeValue(tString), "hh:mm")
Else

'Otherwise, take value as given
 .Value = Format(.Value, "hh:mm")
End If
End With

假设我有 20 个文本框,(A - E) 组成一个组 (F-H) 组成另一个组,依此类推。现在我有2个Q。 1-我应该将上述代码单独应用于每个文本框还是有一个代码可以将用户窗体的所有文本框名称放入其中? 2-输入数据高于 23:59 它给出错误我将格式更改为 [h]:mm 但没有工作,如果用户输入 35:45,我希望显示时间,因为它不像 d:hh:毫米

【问题讨论】:

    标签: excel time-format


    【解决方案1】:

    问题 1 的答案是您需要将代码应用到每个文本框,但首先您应该获取您拥有的代码并将其设为可以从每个 txt$_Afterupdate() 子程序调用的函数。

    函数需要知道要格式化哪个文本框,因此它必须接收文本框对象

    function formatTime(objTXT As Object)
    

    然后在代码中将 txtA 替换为 objTXT

    objTXT.Value = Format(TimeValue(tString), "hh:mm")
    

    然后您将从每个文本框 Afterupdate 调用 formatTime 并传递文本框对象。

    Private Sub txtA_Afterupdate()
    
        formatTime me.txtA
    
    end sub
    

    我需要考虑你的第二个问题,但如果你想接受超过 24 小时,我很确定你不能使用 hh:mm 格式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-16
      • 1970-01-01
      • 2019-08-26
      • 2018-12-12
      • 1970-01-01
      • 1970-01-01
      • 2021-07-07
      • 1970-01-01
      相关资源
      最近更新 更多