RESOLVIDO - Ativar e Desativar TextBox com condição

carloseg

31-08-2017 15:24:53

Prezados amigos estou desenvolvendo um projeto em Excel VBA e me deparei com uma situação, preciso desabilitar um TextBox quando outro é preenchido.
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.

Imagem

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
Sem alguem tiver uma sugestão, agradeço.

Tov Elen Shau

31-08-2017 16:49:21

Adicione o seguinte código.

Private Sub txt_clientecpf_AfterUpdate()
If txt_clientecpf.Value <> Empty Then
Me.txt_clientecnpj.Enabled = False
End If

If txt_clientecnpj.Value <> Empty Then
Me.txt_clientecpf.Enabled = False
End If
End Sub
Private Sub txt_clientecnpj_AfterUpdate()

If txt_clientecnpj.Value <> Empty Then
Me.txt_clientecpf.Enabled = False
End If

If txt_clientecpf.Value <> Empty Then
Me.txt_clientecnpj.Enabled = False
End If

End Sub

Se não resolver nos avise

Atenciosamente

Tov Elen Shau

carloseg

31-08-2017 17:02:31

Olá Tov Elen Shau , agradeço pelo retorno, fiz os testes e acontece o seguinte, como o campo está setado no cnpj se eu digitar dados nele ele desabilita o cpf até ai tudo bem, mas vamos supor que na hora de digitar não é CNPJ e sim CPF que eu quero, se apagar os dados do CNPJ o campo CPF continua desabilitado.

Tov Elen Shau

31-08-2017 17:30:50

Private Sub txt_clientecnpj_AfterUpdate()
Call InabilitarTxt
End Sub

Private Sub txt_clientecpf_AfterUpdate()
Call InabilitarTxt
End Sub

Public Sub InabilitarTxt()
If txt_clientecpf.Value <> Empty Then
Me.txt_clientecnpj.Enabled = False
End If

If txt_clientecnpj.Value <> Empty Then
Me.txt_clientecpf.Enabled = False
End If

If txt_clientecnpj.Value = Empty And txt_clientecpf = Empty Then
Me.txt_clientecnpj.Enabled = True
Me.txt_clientecpf.Enabled = True
End If
End Sub

Tov Elen Shau

31-08-2017 17:31:59

Não é a melhor opção, mas acredito que ajuda.

Atenciosamente

Tov Elen Shau

carloseg

01-09-2017 01:50:01

Boa noite Tov Elen Shau, mais uma vez obrigado, vou fazer os testes.

carloseg

01-09-2017 02:23:54

Boa noite fiz os testes e não ficou como queria, porem testei uma outra forma e deu certo, segue o código que coloquei no evento Change.

Código: Selecionar todos

Private Sub txt_clientecnpj_Change()
    If txt_clientecnpj.Text = "" Then
    txt_clientecpf.Enabled = True
    Else
    txt_clientecpf.Enabled = False

End If
End Sub

Private Sub txt_clientecpf_Change()
    If txt_clientecpf.Text = "" Then
    txt_clientecnpj.Enabled = True
    Else
    txt_clientecnpj.Enabled = False

End If
End Sub
Agradeço pela ajuda Tov Elen Shau.

Tov Elen Shau

01-09-2017 10:44:40

Valeu colega

E que bom que encontrou uma solução melhor.

Atenciosamente

Tov Elen Shau