Hi,
I have set up quite a long formatting procedure to enter time quickly (12 is 12:00) and correcting my common keyboard mistakes (7,45 for 07:45). It works fine but I have many boxes in different userforms and copy pasting every time get procedure huge and unreadable. For some reasons I don't succeed in having a macro called
Sub ForceTimeFormatting () which would be the blue lines of following code that I could recall from mytextbox_Beforeupdate and aligning myInput and myOutput to textbox.value. Here is my code
My problem is to set up the red lines for each TextBox. Do I have to declare myinput as global variant or something like that?
I have set up quite a long formatting procedure to enter time quickly (12 is 12:00) and correcting my common keyboard mistakes (7,45 for 07:45). It works fine but I have many boxes in different userforms and copy pasting every time get procedure huge and unreadable. For some reasons I don't succeed in having a macro called
Sub ForceTimeFormatting () which would be the blue lines of following code that I could recall from mytextbox_Beforeupdate and aligning myInput and myOutput to textbox.value. Here is my code
Code:
Private Sub TxtObsStart_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
'Correct wrong format
[COLOR=#0000cd]Dim myInput As String
Dim myOutput As Variant
[/COLOR][COLOR=#ff0000]myInput = TxtObsStart.Value[/COLOR][COLOR=#0000cd]
myInput = Replace(myInput, ",5", ":30")
myInput = Replace(myInput, ",25", ":15")
myInput = Replace(myInput, ",75", ":45")
myInput = Replace(myInput, ".5", ":30")
myInput = Replace(myInput, ".25", ":15")
myInput = Replace(myInput, ".75", ":45")
myInput = Replace(myInput, ",", ":")
myInput = Replace(myInput, ".", ":")
myInput = Replace(myInput, "?", ":")
myInput = Replace(myInput, "/", ":")
myInput = Replace(myInput, "=", ":")
myInput = Replace(myInput, "+", ":")
Select Case Len(myInput)
Case 1
myOutput = "0" & myInput & ":00"
Case 2
Select Case InStr(myInput, ":")
Case 0
myOutput = myInput & ":00"
Case 1
myOutput = "00" & myInput & "0"
Case 2
myOutput = "0" & myInput & "00"
Case 3
Select Case InStr(myInput, ":")
Case 0
myOutput = "0" & Left(myInput, 1) & ":" & Right(myInput, 2)
Case 1
myOutput = "00" & myInput
Case 2
myOutput = "0" & myInput & "0"
Case 3
myOutput = myInput & "00"
End Select
Case 4
Select Case InStr(myInput, ":")
Case 0
myOutput = Left(myInput, 2) & ":" & Right(myInput, 2)
Case 1
GoTo NoChange
Case 2
myOutput = "0" & myInput
Case 3
myOutput = myInput & "0"
Case 4
GoTo NoChange
End Select
Case Else
NoChange:
myOutput = myInput
End Select
[/COLOR][COLOR=#ff0000]Me.TxtObsStart = myOutput[/COLOR]
'Force time format
If Not Me.TxtObsStart Like "??:??" Then
MsgBox "Please use format 'hh:mm'"
TxtObsStart.SetFocus
Cancel = True
Exit Sub
End If
ObsStart = Application.WorksheetFunction.Text(Me.TxtObsStart, "hh:mm")
Me.TxtObsStart = ObsStart
End Sub
My problem is to set up the red lines for each TextBox. Do I have to declare myinput as global variant or something like that?
Last edited: