Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 343
Default 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.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default 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.


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 343
Default 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.



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default 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.




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default 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
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Number Formating JW Excel Discussion (Misc queries) 1 April 5th 07 03:39 PM
How to tell textbox to treat number as number, not text, and notshow zero John Smith Excel Programming 1 November 16th 06 02:29 AM
Formating textbox on a chart Roger B. Charts and Charting in Excel 10 November 10th 06 02:17 AM
Number formating croozer Excel Discussion (Misc queries) 4 November 29th 05 07:36 PM
Number Formating Jordan Excel Programming 5 March 5th 05 09:08 PM


All times are GMT +1. The time now is 10:42 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"