Henry-Systems
15-07-2012 19:52:06
Buenas a todos.
Aqui estou eu com mais uma dúvida!
Dei uma olha nos foruns do site e encontrei um tópico sobre validação de horas.
Testei o código abaixo com as devidas alteraçõies mas não funcionou. Abaixo o código:
Formato Horas, com validação se a Hora é valida, não tenho a fonte, ja faz um tempo que usei esta rotina :
Código: Selecionar todos Private Sub txtHoras_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Dim TimeStr As String
Set TextLength = txtHoras
On Error GoTo EndMacro
With txtHoras
If HasFormula = False Then
Select Case Len(TextLength)
Case 1
TimeStr = "00:0" & TextLength
Case 2
TimeStr = "00:" & TextLength
Case 3
TimeStr = Left(TextLength, 1) & ":" & Right(TextLength, 2)
Case 4
TimeStr = Left(TextLength, 2) & ":" & Right(TextLength, 2)
'Case 5 ' ex: 12345 = 01:23:45
' TimeStr = Left(TextLength, 1) & ":" & Mid(TextLength, 2, 2) & ":" & Right(TextLength, 2)
'Case 6 ' ex: 123456 = 12:34:56
' TimeStr = Left(TextLength, 2) & ":" & Mid(TextLength, 3, 2) & ":" & Right(TextLength, 2)
Case Else
MsgBox "HORA EM BRANCo !!!"
'With TextBox1
' .SetFocus
'.SelStart = 0
'.SelLength = Len(.Text)
' End With
Exit Sub
End Select
Application.EnableEvents = False
Formula = TimeValue(TimeStr)
txtHoras = TimeStr
sCancel = False
End If
End With
GoTo Fim
EndMacro:
MsgBox "HORA Inválida !!!"
With txtHoras
.SetFocus
.SelStart = 0
.SelLength = Len(.Text)
End With
sCancel = True
Fim:
Application.EnableEvents = True
End Sub
Private Sub txtHoras_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
'Limita a Qde de caracteres
txtHoras.MaxLength = 4
Select Case KeyAscii
Case 8, 48 To 57 ' BackSpace e numericos
'If Len(txtHoras) = 2 Or Len(txtHoras) = 6 Then
' txtHoras.Text = txtHoras.Text & ":"
SendKeys "{End}", False
' End If
Case Else ' o resto é travado
KeyAscii = 0
End Select
End Sub
Gostaria de saber o que estou fazendo de errado ou se está faltando ativar alguma referencia?
Desde já agradeço pela ajuda.
Att
Aqui estou eu com mais uma dúvida!
Dei uma olha nos foruns do site e encontrei um tópico sobre validação de horas.
Testei o código abaixo com as devidas alteraçõies mas não funcionou. Abaixo o código:
Formato Horas, com validação se a Hora é valida, não tenho a fonte, ja faz um tempo que usei esta rotina :
Código: Selecionar todos Private Sub txtHoras_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Dim TimeStr As String
Set TextLength = txtHoras
On Error GoTo EndMacro
With txtHoras
If HasFormula = False Then
Select Case Len(TextLength)
Case 1
TimeStr = "00:0" & TextLength
Case 2
TimeStr = "00:" & TextLength
Case 3
TimeStr = Left(TextLength, 1) & ":" & Right(TextLength, 2)
Case 4
TimeStr = Left(TextLength, 2) & ":" & Right(TextLength, 2)
'Case 5 ' ex: 12345 = 01:23:45
' TimeStr = Left(TextLength, 1) & ":" & Mid(TextLength, 2, 2) & ":" & Right(TextLength, 2)
'Case 6 ' ex: 123456 = 12:34:56
' TimeStr = Left(TextLength, 2) & ":" & Mid(TextLength, 3, 2) & ":" & Right(TextLength, 2)
Case Else
MsgBox "HORA EM BRANCo !!!"
'With TextBox1
' .SetFocus
'.SelStart = 0
'.SelLength = Len(.Text)
' End With
Exit Sub
End Select
Application.EnableEvents = False
Formula = TimeValue(TimeStr)
txtHoras = TimeStr
sCancel = False
End If
End With
GoTo Fim
EndMacro:
MsgBox "HORA Inválida !!!"
With txtHoras
.SetFocus
.SelStart = 0
.SelLength = Len(.Text)
End With
sCancel = True
Fim:
Application.EnableEvents = True
End Sub
Private Sub txtHoras_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
'Limita a Qde de caracteres
txtHoras.MaxLength = 4
Select Case KeyAscii
Case 8, 48 To 57 ' BackSpace e numericos
'If Len(txtHoras) = 2 Or Len(txtHoras) = 6 Then
' txtHoras.Text = txtHoras.Text & ":"
SendKeys "{End}", False
' End If
Case Else ' o resto é travado
KeyAscii = 0
End Select
End Sub
Gostaria de saber o que estou fazendo de errado ou se está faltando ativar alguma referencia?
Desde já agradeço pela ajuda.
Att