Time textbox on userform

HighAndWilder

Active Member
Joined
Nov 4, 2006
Messages
391
Office Version
  1. 365
Platform
  1. Windows
Hi All

I am trying to implement a textbox on a form that enables a user to enter a time e.g. 070000. Once I can do that i will try to allow colons in the appropriate places.

In the first phase, if any other characters that 0-9 are entered they will be invalid and removed from the textbox.

I can detect the key that has been pressed using :

If KeyAscii < 48 Or KeyAscii > 57 Then ' 0-9
blnInvalidKey = True
End If

but I need to remove the invalid character from the textbox.

If I do this in the texttbox Change event then the Change event is triggered again which is not what I need.

How do others tackle such a requirement.

Thanks in anticipation.
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Hi,
Use the TextBox KeyPress event as it has the KeyAscii parameter which will allow you to limit which characters can be entered.

try this function:


Standard Module

Code:
Function TimeValuesOnly(ByVal KeyAscii As MSForms.ReturnInteger) As MSForms.ReturnInteger
    Select Case KeyAscii
    Case 48 To 57, 58
'valid time value entries [0-9] ":"
    Case Else
'cancel
        KeyAscii = 0
    End Select
    Set TimeValuesOnly = KeyAscii
End Function

Userform code page

Code:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    KeyAscii = TimeValuesOnly(KeyAscii)
End Sub


Note: Function will limit users to entering numeric 0-9 & colon characters only - function likely to fail if values which include other characters are pasted to textbox.


Suggest that you use the AfteUpdate event to format the textbox.

Hope Helpful

Dave
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,636
Messages
6,120,669
Members
448,977
Latest member
moonlight6

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
Back
Top