Máscaras de texto em VBA
Os códigos mostrados a seguir são úteis para colocar máscaras de auto-preenchimento em textbox em formulários VBA.
Para utilizar as seguintes máscaras deve-se somente substituir o nome de cada objeto para o mesmo existente em seu código... o procedimento para que as máscaras funcionem é o KeyPress. Para selecioná-lo basta dar um duplo-click sobre a caixa de texto e selecionar o procedimento, conforme a figura abaixo...
Figura 01. Selecionando o procedimento KeyPress em um TextBox |
- Máscara de texto para data
Private Sub txt_data_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
txt_data.MaxLength = 10 '10/10/2014
Select Case KeyAscii
Case 8 'Aceita o BACK SPACE
Case 13: SendKeys "{TAB}" 'Emula o TAB
Case 48 To 57
If txt_data.SelStart = 2 Then txt_data.SelText = "/"
If txt_data.SelStart = 5 Then txt_data.SelText = "/"
Case Else: KeyAscii = 0 'Ignora os outros caracteres
End Select
End Sub
- Máscara de texto para CPF
Private Sub txt_cpf_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
txt_cpf.MaxLength = 14 '032.656.054-71
Select Case KeyAscii
Case 8 'Aceita o BACK SPACE
Case 13: SendKeys "{TAB}" 'Emula o TAB
Case 48 To 57
If txt_cpf.SelStart = 3 Then txt_cpf.SelText = "."
If txt_cpf.SelStart = 7 Then txt_cpf.SelText = "."
If txt_cpf.SelStart = 11 Then txt_cpf.SelText = "-"
Case Else: KeyAscii = 0 'Ignora os outros caracteres
End Select
End Sub
- Máscara de texto para CNPJ
Private Sub txt_cnpj_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
txt_cnpj.MaxLength = 18 '07.454.325/0001-41
Select Case KeyAscii
Case 8 'Aceita o BACK SPACE
Case 13: SendKeys "{TAB}" 'Emula o TAB
Case 48 To 57
If txt_cnpj.SelStart = 2 Then txt_cnpj.SelText = "."
If txt_cnpj.SelStart = 6 Then txt_cnpj.SelText = "."
If txt_cnpj.SelStart = 10 Then txt_cnpj.SelText = "/"
If txt_cnpj.SelStart = 15 Then txt_cnpj.SelText = "-"
Case Else: KeyAscii = 0 'Ignora os outros caracteres
End Select
End Sub
txt_cnpj.MaxLength = 18 '07.454.325/0001-41
Select Case KeyAscii
Case 8 'Aceita o BACK SPACE
Case 13: SendKeys "{TAB}" 'Emula o TAB
Case 48 To 57
If txt_cnpj.SelStart = 2 Then txt_cnpj.SelText = "."
If txt_cnpj.SelStart = 6 Then txt_cnpj.SelText = "."
If txt_cnpj.SelStart = 10 Then txt_cnpj.SelText = "/"
If txt_cnpj.SelStart = 15 Then txt_cnpj.SelText = "-"
Case Else: KeyAscii = 0 'Ignora os outros caracteres
End Select
End Sub
- Máscara de texto para CEP
Private Sub txt_cep_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
txt_cep.MaxLength = 10 '88.888-110
Select Case KeyAscii
Case 8 'Aceita o BACK SPACE
Case 13: SendKeys "{TAB}" 'Emula o TAB
Case 48 To 57
If txt_cep.SelStart = 2 Then txt_cep.SelText = "."
If txt_cep.SelStart = 6 Then txt_cep.SelText = "-"
Case Else: KeyAscii = 0 'Ignora os outros caracteres
End Select
End Sub
txt_cep.MaxLength = 10 '88.888-110
Select Case KeyAscii
Case 8 'Aceita o BACK SPACE
Case 13: SendKeys "{TAB}" 'Emula o TAB
Case 48 To 57
If txt_cep.SelStart = 2 Then txt_cep.SelText = "."
If txt_cep.SelStart = 6 Then txt_cep.SelText = "-"
Case Else: KeyAscii = 0 'Ignora os outros caracteres
End Select
End Sub
- Máscara de texto para TELEFONE
txt_fone.MaxLength = 13 '(45)3332-3333
Select Case KeyAscii
Case 8 'Aceita o BACK SPACE
Case 13: SendKeys "{TAB}" 'Emula o TAB
Case 48 To 57
If txt_fone.SelStart = 0 Then txt_fone.SelText = "("
If txt_fone.SelStart = 3 Then txt_fone.SelText = ")"
If txt_fone.SelStart = 8 Then txt_fone.SelText = "-"
Case Else: KeyAscii = 0 'Ignora os outros caracteres
End Select
- Máscara de texto para MOEDA
Private Sub txt_moeda_AfterUpdate()
txt_moeda.Text = Format(txt_moeda.Text, "Currency")
End Sub
Private Sub txt_moeda2_AfterUpdate()
txt_moeda2 = Format(txt_moeda2, "R$ #,##0.00")
End Sub
Arquivo para download:
https://drive.google.com/file/d/0B2tBlpeZsUSZTl90NXVwOVZ3ZEU/edit?usp=sharing
Link para Vídeo sobre máscara de texto:
http://youtu.be/4sS5gC-pqTA
Att.
Renam
Nenhum comentário:
Postar um comentário