Time field on a Userform?

mattaustin01

Board Regular
Joined
Feb 17, 2009
Messages
184
I realise there's probably an obvious answer to this but I've googled and am unable to find anything useful :eek:

All I need to know is whether there is an easy way to add a field to a userform where a user can enter the time. The best I found so far was in the list of additional controls, but it's essentially a drop down box with every minute of every day in it - is there a better control, and if not is there a way to put an input mask on a standard textbox to force the entry to be hh:mm?

Thanks in advance
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
Try:

Code:
Private Sub TextBox1_Change()
    Dim Char As String
    Char = Right(TextBox1.Text, 1)
    Select Case Len(TextBox1.Text)
    Case 1 To 2, 4 To 5
        If Char Like "#" Then
            If Len(TextBox1) = 1 Then
                If Val(Char) <= 2 Then Exit Sub
            ElseIf Len(TextBox1) = 2 Then
                If Val(Left(TextBox1.Text, 1) & Char) <= 24 Then Exit Sub
            ElseIf Len(TextBox1) = 4 Then
                If Val(Char) <= 5 Then Exit Sub
            Else
                Exit Sub
            End If
        End If
    Case 3
        If Char Like ":" Then Exit Sub
    End Select
    Beep
    On Error Resume Next
    TextBox1.Text = Left(TextBox1.Text, Len(TextBox1.Text) - 1)
    TextBox1.SelStart = Len(TextBox1.Text)
End Sub
 
Upvote 0

mattaustin01

Board Regular
Joined
Feb 17, 2009
Messages
184
Hi Andrew,

That works great - although I guess it proves that there's no easy way to add a time chooser to a userform!

Many thanks,
 
Upvote 0

Colin Legg

MrExcel MVP
Joined
Feb 28, 2008
Messages
3,497
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I realise there's probably an obvious answer to this but I've googled and am unable to find anything useful :eek:

All I need to know is whether there is an easy way to add a field to a userform where a user can enter the time. The best I found so far was in the list of additional controls, but it's essentially a drop down box with every minute of every day in it - is there a better control, and if not is there a way to put an input mask on a standard textbox to force the entry to be hh:mm?

Thanks in advance

Which control was that?

A Microsoft Date and Time Picker control with the format set to either dtpTime or dtpCustom seems to be what you're looking for?
 
Upvote 0

mattaustin01

Board Regular
Joined
Feb 17, 2009
Messages
184
Hi Colin,

The MS one does sound like what I need but I don't seem to have it - the only controls I can see in the entire list that are time related are LANDesk Time and LANDeskStatic Time, neither of which are much good!

Where can I get the MS time from?
 
Upvote 0

Colin Legg

MrExcel MVP
Joined
Feb 28, 2008
Messages
3,497
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi,

If you don't already have it and you're going to be distributing your project around the office (they probably don't have it either) then using it may create more trouble than it's worth, but it can be downloaded from here:

http://support.microsoft.com/kb/297381

You'll then need to register it using the regsvr32 command.




Also see related discussions here:
http://www.mrexcel.com/forum/showpost.php?p=810136&postcount=8
http://www.mrexcel.com/forum/showthread.php?p=1751238


Hope that helps...
 
Upvote 0

Forum statistics

Threads
1,190,817
Messages
5,983,060
Members
439,818
Latest member
schizoid231

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top