Máscara de Texto Para CNPJ Pelo Auto-Preenchimento

Os comandos abaixo coloca a máscara de CNPJ pelo auto-preenchimento.


Private Sub txtCnpj_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

   txtCnpj.MaxLength = 18  'Limita a Quantidade de Caracteres da caixa de texto CNPJ
 
  Select Case KeyAscii
 
      Case 8       'Aceita apagar o texto, ou seja, Back Space
      Case 13: SendKeys "{TAB}"    'Emula o TAB
      Case 48 To 57
 
         If txtCnpj.SelStart = 2 Then txtCnpj.SelText = "."
         If txtCnpj.SelStart = 6 Then txtCnpj.SelText = "."
         If txtCnpj.SelStart = 10 Then txtCnpj.SelText = "/"
         If txtCnpj.SelStart = 15 Then txtCnpj.SelText = "-"

         Case Else: KeyAscii = 0     'Ignora os outros caracteres
 
    End Select

End Sub

Comentários

Postagens mais visitadas deste blog

Função VBA - Validador de CNPJ

Autocorreção no Excel - Eu Escrevo e o Excel Corrige.

Função VBA - Busca Distância Entre Cidades