Excel用户窗体中文本框的时间格式 [英] Time format of text box in excel user form

查看:385
本文介绍了Excel用户窗体中文本框的时间格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码可以在用户窗体中设置文本框"txtA"的时间格式.

I have below code to set time format of atext box "txtA" in userform.

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问. 1-我应该将上面的代码分别应用于每个文本框,还是有一个代码可以将所有文本框的用户名放在其中? 2-输入数据高于23:59,它给出了错误,我将格式更改为[h]:mm,但是没有用,我想如果用户输入35:45,则显示时间是因为它不像d:hh:毫米

Assume I have say 20 text boxes,(A - E) make one group (F- H)make another group and so on. now I have 2 Q. 1-Should i apply above code to each textbox individually or there's a code that i just can put all text boxes name of userform in it? 2- with input data higher than 23:59 it gives error i changed the format to [h]:mm but didn't work,I want if user enters 35:45 the time be shown as it is not like d:hh:mm

推荐答案

问题1的答案是您需要将代码应用于每个文本框,但是首先您应该获取已有的代码并使之成为您所需要的函数可以从每个txt $ _Afterupdate()子对象中调用.

The answer to question 1 is that you need to apply the code to every textbox but first you should take the code that you have and make it a function that you can call from each txt$_Afterupdate() sub.

该功能将需要知道要格式化的文本框,因此它必须接收文本框对象

The function will need to know which textbox to format so it will have to receive the textbox object

function formatTime(objTXT As Object)

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

then in the code replace txtA with objTXT

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

然后,您将在更新后的每个文本框中调用formatTime并传递文本框对象.

then you will call formatTime from each textbox Afterupdate and pass the textbox object.

Private Sub txtA_Afterupdate()

    formatTime me.txtA

end sub

我需要考虑第二个问题,但是我很确定如果要接受24小时以上,则不能使用hh:mm格式.

I need to think about your second question but I'm pretty sure that you can't use the format hh:mm if you want to accept more than 24 hours.

这篇关于Excel用户窗体中文本框的时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆