domingo, 13 de fevereiro de 2011

Função para validar Título Eleitoral

Public Function ValidarTEleitor(valor As String)
    Dim OnlyNumber, SomaDV1, SomaDV2 As String
    Dim FatorMult As String
    For i = 1 To Len(valor)
        OnlyNumber = Mid(valor, i, 1)
        If OnlyNumber >= "0" And OnlyNumber <= "9" Then numero = numero & OnlyNumber
    Next
    FatorMult = "23456789" '
    For i = 1 To Len(FatorMult)
          SomaDV1 = SomaDV1 + val(Mid(numero, i, 1)) * val(Mid(FatorMult, i, 1))
    Next
    SomaDV1 = SomaDV1 Mod 11
    If SomaDV1 = 10 Then SomaDV1 = 0
    SomaDV2 = val(Mid(numero, 9, 1)) * 7
    SomaDV2 = SomaDV2 + val(Mid(numero, 10, 1)) * 8
    SomaDV2 = SomaDV2 + val(SomaDV1) * 9
    SomaDV2 = SomaDV2 Mod 11
    If SomaDV2 = 10 Then SomaDV2 = 0
   
'--------------------- Teste de validação dos título digitado -----------------   
    If val(Mid(numero, 11, 1)) = SomaDV1 And val(Mid(numero, 12, 1)) = SomaDV2 Then
        Dim D9, D10 As String
        D9 = Mid(numero, 9, 1)
        D10 = Mid(numero, 10, 1)
        If CInt(D9 & D10) > 0 And CInt(D9 & D10) < 29 Then
           MsgBox "Número do Título Eleitoral correto."
        Else
           MsgBox "Número do Título Eleitoral Inválido.", vbCritical, "Atenção !"
        End If
    Else
        MsgBox "Número do Título Eleitoral Inválido.", vbCritical, "Atenção !"
    End If
End Function

Vídeo para demonstrar a função:

sábado, 12 de fevereiro de 2011

Máscara de Entrada no TextBox - EVENTO KEYPRESS

Segue o código para gerar máscara de entrada para CNPJ e CPF dos dados digitados em um textbox, utilizando evento KeyPress.
 

Código Vba CNPJ:
Private Sub CNPJ_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Select Case KeyAscii
        Case 8, 48 To 57
                Me.CNPJ.MaxLength = 18 ' Quantidade máxima de caracteres no textbox CNPJ                If Len(CNPJ) = 2 Then CNPJ = CNPJ + "."
                If Len(CNPJ) = 6 Then CNPJ = CNPJ + "."
                If Len(CNPJ) = 10 Then CNPJ = CNPJ + "/"
                If Len(CNPJ) = 15 Then CNPJ = CNPJ + "-"
        Case Else
            KeyAscii = 0
    End Select   
End Sub
Código Vba CPF:
 Private Sub Cpf_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Select Case KeyAscii
        Case 8, 48 To 57
                Me.cpf.MaxLength = 14  ' Quantidade máxima de caracteres no textbox Cpf
                If Len(cpf) = 3 Then cpf = cpf + "."
                If Len(cpf) = 7 Then cpf = cpf + "."
                If Len(cpf) = 11 Then cpf = cpf + "-"
        Case Else
            KeyAscii = 0
    End Select
End Sub