Excel VBA文本框时间验证为[h]:mm [英] Excel VBA Textbox time validation to [h]:mm

查看:64
本文介绍了Excel VBA文本框时间验证为[h]:mm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发VBA Excel用户表单,需要以[h]:mm格式输入时间.这意味着小时数可以是无限的,并且不会像hh:mm格式那样在23:59之后循环回到0.我在网上搜索无济于事.

I am developing a VBA Excel Userform and need to enter a time in the [h]:mm format. This means that the hours can be unlimited and does not cycle back to 0 after 23:59 like the hh:mm format does. I have searched the web to no avail.

这是我当前正在使用的代码:

Here is the code I'm currently using:

Private Sub Txtbx_TimeAvailable_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If Me.Txtbx_TimeAvailable.Value <> Format(Me.Txtbx_TimeAvailable.Value, "[h]:mm") Then
        MsgBox "Please enter correct time format in hh:mm"
        Cancel = True
        Me.Txtbx_TimeAvailable.Value = vbNullString
    Else
        ThisWorkbook.Sheets(SELECTEDWORKSHEET).Range("C60").Value = Txtbx_TimeAvailable.Text
        Call UpdateDentistMainForm
    End If

End Sub

但是,在使用此代码时,如果我输入25:53,则会将其转换为01:53.我将不胜感激.

However, when using this code if I enter 25:53 it converts it to 01:53. I'd appreciate any help I could get on this.

推荐答案

您将需要手动解析有效性,这是一种方法:

You will need to parse the validity manually, here is one way:

Var = "59:59"

Dim IsValid As Boolean
Dim tok() As String: tok = Split(Var, ":")

If (UBound(tok) = 1) Then
    '// `like` to prevent lead +/-
    IsValid = tok(0) Like "#*" And IsNumeric(tok(0)) And tok(1) Like "#*" And IsNumeric(tok(1)) And tok(1) < 60
    '// optionally normalize by removing any lead 0
    Var = Val(tok(0)) & ":" & Val(tok(1))
End If

Debug.Print Var, IsValid

这篇关于Excel VBA文本框时间验证为[h]:mm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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