![]() |
Formating a number in a textbox
At work we use the 24 time format.
What I would like is something that will format time being inputted to a textbox (TextBox1) to 00:00. So if they enter 1347 it will be converted to 13:47. |
Formating a number in a textbox
Read the data from the text box into a varuiable and then use FORMAT()
function to get the 24 hour time and then put it back into the box Mytime = textbox1.valuse Mytime = format(Mytime,"H:MM") "Patrick C. Simonds" wrote: At work we use the 24 time format. What I would like is something that will format time being inputted to a textbox (TextBox1) to 00:00. So if they enter 1347 it will be converted to 13:47. |
Formating a number in a textbox
I have tried 3 varients, but still not working:
Private Sub TextBox1_Change() Mytime = TextBox1.Value Mytime = Format(Mytime, "H:MM") End Sub Private Sub TextBox1_Change() Dim Mytime Mytime = TextBox1.Value Mytime = Format(Mytime, "H:MM") End Sub and last was: Private Sub TextBox1_Change() 'TextBox1.Value = Format("HH:MM") End Sub "Joel" wrote in message ... Read the data from the text box into a varuiable and then use FORMAT() function to get the 24 hour time and then put it back into the box Mytime = textbox1.valuse Mytime = format(Mytime,"H:MM") "Patrick C. Simonds" wrote: At work we use the 24 time format. What I would like is something that will format time being inputted to a textbox (TextBox1) to 00:00. So if they enter 1347 it will be converted to 13:47. |
Formating a number in a textbox
The time is text so you 1st have to convert it to a serial time using
TimeValue. then format it in the desired format. My mistake. Mytime = TimeValue(TextBox1.Value) Mytime = Format(Mytime, "H:MM") "Patrick C. Simonds" wrote: I have tried 3 varients, but still not working: Private Sub TextBox1_Change() Mytime = TextBox1.Value Mytime = Format(Mytime, "H:MM") End Sub Private Sub TextBox1_Change() Dim Mytime Mytime = TextBox1.Value Mytime = Format(Mytime, "H:MM") End Sub and last was: Private Sub TextBox1_Change() 'TextBox1.Value = Format("HH:MM") End Sub "Joel" wrote in message ... Read the data from the text box into a varuiable and then use FORMAT() function to get the 24 hour time and then put it back into the box Mytime = textbox1.valuse Mytime = format(Mytime,"H:MM") "Patrick C. Simonds" wrote: At work we use the 24 time format. What I would like is something that will format time being inputted to a textbox (TextBox1) to 00:00. So if they enter 1347 it will be converted to 13:47. |
Formating a number in a textbox
I created a small userform with a textbox, a label (for error messages) and two
commandbuttons (ok/cancel). This seemed to work ok: Option Explicit Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean) Dim myTime As Variant Dim myStr As String Dim okTime As Boolean myStr = Me.TextBox1.Value okTime = True If IsNumeric(myStr) = False Then 'check for a time entered by the user 'like 12:34 myTime = "" On Error Resume Next myTime = TimeValue(myStr) On Error GoTo 0 If myTime = "" Then 'not a time okTime = False Else myStr = Format(myTime, "hhmm") End If End If If okTime = False Then 'skip this check Else myTime = "" On Error Resume Next myTime = TimeValue(Format(myStr, "00:00")) On Error GoTo 0 If Format(myTime, "hhmm") = Format(myStr, "0000") Then 'ok Else okTime = False End If End If If okTime Then Me.TextBox1.Value = Format(myTime, "hh:mm") Me.Label1.Caption = "" Else Cancel = True Beep Me.Label1.Caption = "Please enter a time" End If End Sub Private Sub UserForm_Initialize() Me.Label1.Caption = "" With Me.CommandButton1 .Caption = "Ok" .Default = True End With With Me.CommandButton2 .Caption = "Cancel" .Cancel = True .TakeFocusOnClick = False End With End Sub Notice that this uses the _exit event. This event fires when you try to leave the textbox--not after each character is entered. "Patrick C. Simonds" wrote: At work we use the 24 time format. What I would like is something that will format time being inputted to a textbox (TextBox1) to 00:00. So if they enter 1347 it will be converted to 13:47. -- Dave Peterson |
All times are GMT +1. The time now is 02:47 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com