Tenho um cadastro com campos CNPJ e CPF, ao iniciar o setfocus vai para o campo CNPJ, se o digitar o valor o campo CPF precisa ficar desabilitado, se digitar no campo CPF o CNPJ fica desabilitado. Porem não estou conseguindo, esses campos CNPJ e CFP tem uma mascara.
Código: Selecionar todos
Private Sub txt_clientecnpj_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
txt_clientecnpj.MaxLength = 18 '07.454.325/0001-41 - MaxLength define o tamanho do campo
Select Case KeyAscii
Case 8 'Aceita o BACK SPACE
Case 13: SendKeys "{TAB}" 'Emula o TAB
Case 48 To 57
If txt_clientecnpj.SelStart = 2 Then txt_clientecnpj.SelText = "."
If txt_clientecnpj.SelStart = 6 Then txt_clientecnpj.SelText = "."
If txt_clientecnpj.SelStart = 10 Then txt_clientecnpj.SelText = "/"
If txt_clientecnpj.SelStart = 15 Then txt_clientecnpj.SelText = "-"
Case Else: KeyAscii = 0 'Ignora os outros caracteres
End Select
End Sub
Código: Selecionar todos
Private Sub txt_clientecpf_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
txt_clientecpf.MaxLength = 14 '032.656.054-71 - MaxLength define o tamanho do campo
Select Case KeyAscii
Case 8 'Aceita o BACK SPACE
Case 13: SendKeys "{TAB}" 'Emula o TAB
Case 48 To 57
If txt_clientecpf.SelStart = 3 Then txt_clientecpf.SelText = "."
If txt_clientecpf.SelStart = 7 Then txt_clientecpf.SelText = "."
If txt_clientecpf.SelStart = 11 Then txt_clientecpf.SelText = "-"
Case Else: KeyAscii = 0 'Ignora os outros caracteres
End Select
End Sub