Notifications
Clear all

Bloquear registro duplicado pelo CPF

6 Posts
1 Usuários
0 Reactions
1,000 Visualizações
(@fernandofernandes)
Posts: 43750
Illustrious Member
Topic starter
 

Boa tarde pessoal, acho que meu problema é simples pra que manja,
estou montando um cadastro e preciso validar o registro pelo CPF, onde, quando eu digitar um CPF já cadastrado ele me retorne um MsgBox "Desculpe, Esse CPF já está cadastrado para o nome: Fulano de tal"

Quando coloco o Código a seguir no evento exit, da TextBox2, ele bloqueia qualquer sequência de nºs que tento cadastrar,

Dim rang As Range
Set rang = Range("A1:A2001")
Dim rangCel As Range
Dim regExiste As Boolean
regExiste = False
For Each rangCel In rang.Cells
        If rangCel.Text = TextBox2.Text Then
                regExiste = True
        End If
Next
If regExiste = True Then
        MsgBox "o registo já existe"
        Exit Sub
Else
        MsgBox "Desculpe, Esse CPF já está cadastrado para o nome: Fulano de tal"
End If

Gente se alguém puder me ajudar,
Desde já agradeço,

ABÇ!

 
Postado : 11/09/2015 4:49 pm
(@fernandofernandes)
Posts: 43750
Illustrious Member
Topic starter
 

Ops! enviando a plan!

 
Postado : 11/09/2015 4:50 pm
(@fernandofernandes)
Posts: 43750
Illustrious Member
Topic starter
 

Teste

Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim rang As Range, rangCel As Range
Dim regExiste As Boolean
Dim strNome As String
Set rang = Range("C2:C" & Cells(Cells.Rows.Count, "A").End(xlUp).Row)
regExiste = False
TextBox2 = Format(TextBox2, "000"".""000"".""000""-""00") ' formata como CPF

For Each rangCel In rang.Cells
    If rangCel.Text = TextBox2.Text Then
        regExiste = True
        strNome = Cells(rangCel.Row, 1).Value
    End If
Next
If regExiste = True Then
        'MsgBox "o registo já existe"
        MsgBox "Desculpe, Esse CPF já está cadastrado para o nome: " & strNome
'        Exit Sub
TextBox2.Text = ""
TextBox1.Text = ""
TextBox1.SetFocus
End If
End Sub
 
Postado : 12/09/2015 8:10 am
(@fernandofernandes)
Posts: 43750
Illustrious Member
Topic starter
 

Reinaldo, Boa tarde, Funcionou perfeitamente até a MsgBox, mas a parte de limpar o form e focar no textbox1 não funcionou, ele apenas limpa o textbox2 e pula pro próximo prosseguindo o preenchimento do form, a alternativa que fiz aqui foi colocar um Unload Me depois do MsgBox e excluí as linha de limpeza, Ficou assim:

Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim rang As Range, rangCel As Range
Dim regExiste As Boolean
Dim strNome As String
Set rang = Range("C2:C" & Cells(Cells.Rows.Count, "A").End(xlUp).Row)
regExiste = False
TextBox2 = Format(TextBox2, "000"".""000"".""000""-""00") ' formata como CPF

For Each rangCel In rang.Cells
    If rangCel.Text = TextBox2.Text Then
        regExiste = True
        strNome = Cells(rangCel.Row, 1).Value
    End If
Next
If regExiste = True Then
        MsgBox "Desculpe, Esse CPF já está cadastrado para o nome: " & strNome
        Unload Me
 Exit Sub

End If
End Sub

Se por acaso tiver uma solução melhor...
Se não me avise que marco o tópico como resolvido,

ABÇ!

 
Postado : 14/09/2015 9:32 am
(@fernandofernandes)
Posts: 43750
Illustrious Member
Topic starter
 

Para funcionar conforme propus, deve suprimir a linha exit sub, inclusive na sua solução a mesma tambem não é necessaria

 
Postado : 14/09/2015 9:45 am
(@fernandofernandes)
Posts: 43750
Illustrious Member
Topic starter
 

Boa noite Reinaldo,
Pô na plan teste que eu te mandei funcionou mas na minha plan não tá funcionando, vou te mandar a mesma pra vc ver se tem algo errado,

Abç!

 
Postado : 19/09/2015 10:35 pm