Olá!
Uso a seguinte rotina para formatar um CPF num TextBox chamada CPF:
Private Sub CPF_Change()
Select Case Len(CPF.Text)
Case Is = 3
CPF.Text = CPF.Text & "."
Case Is = 7
CPF.Text = CPF.Text & "."
Case Is = 11
CPF.Text = CPF.Text & "-"
End Select
End Sub
Conforme vou inserindo dígitos ela vai sendo formatada. Quando quero ir apagando o conteúdo da caixa com Backspace ele para num ponto ou no hífen, ou seja, só apago se selecionar tudo de uma vez.
Tem como inibir a formatação quando vou apagar?
Vídeo recomendado
https://youtu.be/diWPPPhW-9E
https://youtu.be/diWPPPhW-9E
FORMATAR CPF [RESOLVIDO]
-
- Colaborador
- Mensagens: 59
- Registrado em: Sáb Mar 15, 2014 5:48 pm
FORMATAR CPF [RESOLVIDO]
Editado pela última vez por HUGO SOUZA em Qua Out 15, 2014 6:44 pm, em um total de 1 vez.
Re: FORMATAR CPF
HUGO SOUZA, bom dia!
Se você quiser apagar o conteúdo da caixa, pode utilizar o código:
CPF.text=""
Se quiser voltar apagando utilize:
Application.SendKeys {BS}
Espero ter ajudado!
Magushi
Se você quiser apagar o conteúdo da caixa, pode utilizar o código:
CPF.text=""
Se quiser voltar apagando utilize:
Application.SendKeys {BS}
Espero ter ajudado!
Magushi
-
- Colaborador
- Mensagens: 59
- Registrado em: Sáb Mar 15, 2014 5:48 pm
Re: FORMATAR CPF
Olá Magushi! A intenção é ir apagando mesmo...
Não consegui entender como utilizo... Deu falha acho que por causa do {BS}.
Eu devo por esse comando no evento CHANGE da textbox?
Não consegui entender como utilizo... Deu falha acho que por causa do {BS}.
Eu devo por esse comando no evento CHANGE da textbox?
-
- Colaborador
- Mensagens: 59
- Registrado em: Sáb Mar 15, 2014 5:48 pm
Re: FORMATAR CPF
Olá!
Desenvolvi pegando coisas na net:
Private Sub CPF_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
strValid As String
If KeyCode = 8 Then
'foi apertada a tecla Backspace
Else
'não foi apertada a tecla Backspace
strValid = "0123456789" 'caracteres permitidos
If InStr(strValid, Chr(KeyAscii)) = 0 Then
KeyAscii = 0
Else
Select Case Len(CPF.Text)
Case Is = 3
CPF.Text = CPF.Text & "."
Case Is = 7
CPF.Text = CPF.Text & "."
Case Is = 11
CPF.Text = CPF.Text & "-"
End Select
End If
End If
End Sub
Private Sub CPF_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Len(CPF.Text) < 14 And CPF.Text <> "" Then
MsgBox "A informação não é válida.", vbCritical, "Atenção"
CPF = ""
CPF.SetFocus
Else
End If
End Sub
Desenvolvi pegando coisas na net:
Private Sub CPF_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
strValid As String
If KeyCode = 8 Then
'foi apertada a tecla Backspace
Else
'não foi apertada a tecla Backspace
strValid = "0123456789" 'caracteres permitidos
If InStr(strValid, Chr(KeyAscii)) = 0 Then
KeyAscii = 0
Else
Select Case Len(CPF.Text)
Case Is = 3
CPF.Text = CPF.Text & "."
Case Is = 7
CPF.Text = CPF.Text & "."
Case Is = 11
CPF.Text = CPF.Text & "-"
End Select
End If
End If
End Sub
Private Sub CPF_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Len(CPF.Text) < 14 And CPF.Text <> "" Then
MsgBox "A informação não é válida.", vbCritical, "Atenção"
CPF = ""
CPF.SetFocus
Else
End If
End Sub