Validar Inscrição Estadual Excel

Validar Inscrição Estadual no Excel

É comum as empresas terem problemas na hora da validação da inscrição estadual. Neste artigo mostramos como fazer isso no Excel. Veja também Como consultar o Sintegra Automaticamente.

A inconsistência da inscrição estadual dados gera problemas na entrega do Sintegra, e por conseguinte você terá muito retrabalho para corrigir estas informações, regerar e reenviar o arquivo.

Para realizar estes cálculos você pode verificar nesta página: http://www.sintegra.gov.br/insc_est.html o cálculo de todas as Inscrições Estaduais.

Neste artigo você terá o código fonte liberado e a implementação da validação da Inscrição Estadual através de uma UDF, função própria global no Excel feita em VBA.

O que é Inscrição Estadual e como Validar Inscrição Estadual

No Brasil, cada estado tem um formato único para o número de registro estadual, também conhecido como “Inscrição Estadual”. Este número é usado para identificar uma empresa ou indivíduo para fins tributários e é obrigatório para empresas que operam dentro do estado.

Para validar uma Inscrição Estadual, é necessário verificar se o número segue o formato correto para o estado em que a empresa ou indivíduo está registrado. O formato do número pode variar dependendo do estado, com alguns estados tendo um formato numérico simples enquanto outros têm formatos alfanuméricos que incluem letras e caracteres especiais.

Uma maneira de validar a Inscrição Estadual é usar uma expressão regular, que é uma sequência de caracteres que define um padrão de busca. Cada estado tem um padrão específico que pode ser usado para combinar a Inscrição Estadual, e você pode usar este padrão para garantir que o número esteja no formato correto.

Outra maneira é usar uma biblioteca de validação que já tenha uma validação pré-construída para cada estado, essas bibliotecas podem ser encontradas em diferentes linguagens, como Java, Python e PHP, e dessa forma você não precisa se preocupar com o padrão específico para cada estado.

É importante lembrar que a validação da Inscrição Estadual não se trata apenas do formato, mas também das regras fiscais do estado, alguns estados usam um dígito ou um algoritmo de módulo para validar o número e para isso é necessário verificar as regras fiscais do estado ou consultar um software especializado.

Também é importante mencionar que alguns estados possuem mais de um formato, dependendo do tipo da empresa ou do período de registro. Então, sempre é recomendável verificar o formato e as regras com o Departamento Fiscal do estado ou consultar um software especializado.

Em resumo, para validar a Inscrição Estadual no Brasil, é necessário verificar se o número segue o formato correto para o estado em que a empresa ou indivíduo está registrado, é possível usar uma expressão regular ou uma biblioteca de validação e também verificar as regras fiscais do estado ou consultar um software

Como implementar a função de validação de inscrição estadual no Excel

Para instalar a função pressione as teclas ALT + F11, depois clique em Inserir->Módulo

Validar Inscrição Estadual Excel 2

No módulo criado cole o código VBA:

Function ChecaInscrE(ByVal pUF As String, ByVal pInscr As String)
    Dim strBase     As String
    Dim strBase2    As String
    Dim strOrigem   As String
    Dim strDigito1  As String
    Dim strDigito2  As String
    Dim intPos      As Long
    Dim intValor    As Long
    Dim intSoma     As Long
    Dim intResto    As Long
    Dim intNumero   As Long
    Dim intPeso     As Long
    Dim intDig      As Long

    ChecaInscrE = False

    strBase = ""
    strBase2 = ""
    strOrigem = ""

    If Trim(pInscr) = "ISENTO" Or Trim(pInscr) = "EX" Then
        ChecaInscrE = True
        Exit Function
    End If

    For intPos = 1 To Len(Trim(pInscr))
        If InStr(1, "0123456789P", Mid(pInscr, intPos, 1), vbTextCompare) > 0 Then
            strOrigem = strOrigem & Mid(pInscr, intPos, 1)
        End If
    Next
    
    Select Case pUF
        Case "AC" 'Acre
            strBase = Left(Trim(strOrigem) & "000000000", 13)
            d01 = CInt(Mid(strBase, 1, 1))
            d02 = CInt(Mid(strBase, 2, 1))
            d03 = CInt(Mid(strBase, 3, 1))
            d04 = CInt(Mid(strBase, 4, 1))
            d05 = CInt(Mid(strBase, 5, 1))
            d06 = CInt(Mid(strBase, 6, 1))
            d07 = CInt(Mid(strBase, 7, 1))
            d08 = CInt(Mid(strBase, 8, 1))
            d09 = CInt(Mid(strBase, 9, 1))
            d10 = CInt(Mid(strBase, 10, 1))
            d11 = CInt(Mid(strBase, 11, 1))
            dv01 = CInt(Mid(strBase, 12, 1))
            dv02 = CInt(Mid(strBase, 13, 1))
            
            If d01 <> 0 Or d02 <> 1 Then
                ChecaInscrE = False
                Exit Function
            End If
            ds = 4 * d01 + 3 * d02 + 2 * d03 + 9 * d04 + 8 * d05 + 7 * d06 + 6 * d07 + 5 * d08 + _
                               4 * d09 + 3 * d10 + 2 * d11
                          aux1 = Fix(ds / 11)
            aux1 = aux1 * 11
            aux2 = ds - aux1 ' aux2 é o resto, ou mod
            digverificador1 = 11 - aux2
            
            If digverificador1 = 10 Or digverificador1 = 11 Then
                digverificador1 = 0 'primeiro digito
            End If
            
            ds = 5 * d01 + 4 * d02 + 3 * d03 + 2 * d04 + 9 * d05 + 8 * d06 + 7 * d07 + 6 * d08 + _
                               5 * d09 + 4 * d10 + 3 * d11 + 2 * digverificador1
            aux1 = Fix(ds / 11)
            aux1 = aux1 * 11
            aux2 = ds - aux1 ' aux2 é o resto, ou mod
            digverificador2 = 11 - aux2
            
            If digverificador2 = 10 Or digverificador2 = 11 Then
                digverificador2 = 0 'primeiro digito
            End If
            
            If digverificador1 = dv01 And digverificador2 = dv02 Then
                ChecaInscrE = True
            End If
            
        Case "AL" ' Alagoas
            strBase = Left(Trim(strOrigem) & "000000000", 9)
            
            If Left(strBase, 2) = "24" Then
                intSoma = 0
                For intPos = 1 To 8
                    intValor = CInt(Mid(strBase, intPos, 1))
                    intValor = intValor * (10 - intPos)
                    intSoma = intSoma + intValor
                Next
            
                intSoma = intSoma * 10
                intResto = intSoma Mod 11
                strDigito1 = Right(IIf(intResto = 10, "0", CStr(intResto)), 1)
                strBase2 = Left(strBase, 8) & strDigito1
                If strBase2 = strOrigem Then
                    ChecaInscrE = True
                End If
            End If
            
        Case "AM" ' Amazonas
            strBase = Left(Trim(strOrigem) & "000000000", 9)
            intSoma = 0
            
            For intPos = 1 To 8
                intValor = CInt(Mid(strBase, intPos, 1))
                intValor = intValor * (10 - intPos)
                intSoma = intSoma + intValor
            Next
            
            If intSoma < 11 Then
                strDigito1 = Right(CStr(11 - intSoma), 1)
            Else
                intResto = intSoma Mod 11
                strDigito1 = Right(IIf(intResto < 2, "0", CStr(11 - intResto)), 1)
            End If
            
            strBase2 = Left(strBase, 8) & strDigito1
            
            If strBase2 = strOrigem Then
                ChecaInscrE = True
            End If
        Case "AP" ' Amapa
            strBase = Left(Trim(strOrigem) & "000000000", 9)
            intPeso = 0
            intDig = 0
            
            If Left(strBase, 2) = "03" Then
                intNumero = Fix(Left(strBase, 8))
                
                If intNumero >= 3000001 And intNumero <= 3017000 Then
                    intPeso = 5
                    intDig = 0
                ElseIf intNumero >= 3017001 And intNumero <= 3019022 Then
                    intPeso = 9
                    intDig = 1
                ElseIf intNumero >= 3019023 Then
                    intPeso = 0
                    intDig = 0
                End If
                
                intSoma = intPeso
                
                For intPos = 1 To 8
                    intValor = CInt(Mid(strBase, intPos, 1))
                    intValor = intValor * (10 - intPos)
                    intSoma = intSoma + intValor
                Next
            
                intResto = intSoma Mod 11
                intValor = 11 - intResto
            
                If intValor = 10 Then
                    intValor = 0
                ElseIf intValor = 11 Then
                    intValor = intDig
                End If
                
                strDigito1 = Right(CStr(intValor), 1)
                strBase2 = Left(strBase, 8) & strDigito1
            
                If strBase2 = strOrigem Then
                    ChecaInscrE = True
                End If
            End If
            
        Case "BA" ' Bahia
            strBase = Left(Trim(strOrigem) & "00000000", 8)
            
            If InStr(1, "0123458", Left(strBase, 1), vbTextCompare) > 0 Then
                intSoma = 0
                
                For intPos = 1 To 6
                    intValor = CInt(Mid(strBase, intPos, 1))
                    intValor = intValor * (8 - intPos)
                    intSoma = intSoma + intValor
                Next
                
                intResto = intSoma Mod 10
                strDigito2 = Right(IIf(intResto = 0, "0", CStr(10 - intResto)), 1)
                strBase2 = Left(strBase, 6) & strDigito2
                intSoma = 0
            
                For intPos = 1 To 7
                    intValor = CInt(Mid(strBase2, intPos, 1))
                    intValor = intValor * (9 - intPos)
                    intSoma = intSoma + intValor
                Next
                
                intResto = intSoma Mod 10
                strDigito1 = Right(IIf(intResto = 0, "0", CStr(10 - intResto)), 1)
            Else
                intSoma = 0
                
                For intPos = 1 To 6
                    intValor = CInt(Mid(strBase, intPos, 1))
                    intValor = intValor * (8 - intPos)
                    intSoma = intSoma + intValor
                Next
                
                intResto = intSoma Mod 11
                strDigito2 = Right(IIf(intResto < 2, "0", CStr(11 - intResto)), 1)
                strBase2 = Left(strBase, 6) & strDigito2
                intSoma = 0
                
                For intPos = 1 To 7
                    intValor = CInt(Mid(strBase2, intPos, 1))
                    intValor = intValor * (9 - intPos)
                    intSoma = intSoma + intValor
                Next
                
                intResto = intSoma Mod 11
                strDigito1 = Right(IIf(intResto < 2, "0", CStr(11 - intResto)), 1)
            End If
            
            strBase2 = Left(strBase, 6) & strDigito1 & strDigito2
            
            If strBase2 = strOrigem Then
                ChecaInscrE = True
            End If
      
        Case "CE" ' Ceara
            strBase = Left(Trim(strOrigem) & "000000000", 9)
            
            intSoma = 0
            
            For intPos = 1 To 8
                intValor = CInt(Mid(strBase, intPos, 1))
                intValor = intValor * (10 - intPos)
                intSoma = intSoma + intValor
            Next
            
            intResto = intSoma Mod 11
            intValor = 11 - intResto
            
            If intValor > 9 Then
                intValor = 0
            End If
            
            strDigito1 = Right(CStr(intValor), 1)
            strBase2 = Left(strBase, 8) & strDigito1
            
            If strBase2 = strOrigem Then
                ChecaInscrE = True
            End If
            
        Case "DF" ' Distrito Federal
            strBase = Left(Trim(strOrigem) & "0000000000000", 13)
            
            If Left(strBase, 2) = "07" Then
                intSoma = 0
                intPeso = 2
                
                For intPos = 11 To 1 Step -1
                    intValor = CInt(Mid(strBase, intPos, 1))
                    intValor = intValor * intPeso
                    intSoma = intSoma + intValor
                    intPeso = intPeso + 1
                    
                    If intPeso > 9 Then
                        intPeso = 2
                    End If
                Next
                intResto = intSoma Mod 11
                strDigito1 = Right(IIf(intResto < 2, "0", CStr(11 - intResto)), 1)
                strBase2 = Left(strBase, 11) & strDigito1
                intSoma = 0
                intPeso = 2
                
                For intPos = 12 To 1 Step -1
                    intValor = CInt(Mid(strBase, intPos, 1))
                    intValor = intValor * intPeso
                    intSoma = intSoma + intValor
                    intPeso = intPeso + 1
                    
                    If intPeso > 9 Then
                        intPeso = 2
                    End If
                Next
                
                intResto = intSoma Mod 11
                strDigito2 = Right(IIf(intResto < 2, "0", CStr(11 - intResto)), 1)
                strBase2 = Left(strBase, 12) & strDigito2
                
                If strBase2 = strOrigem Then
                    ChecaInscrE = True
                End If
            End If
            
        Case "ES" ' Espirito Santo
            strBase = Left(Trim(strOrigem) & "000000000", 9)
            intSoma = 0
            
            For intPos = 1 To 8
                intValor = CInt(Mid(strBase, intPos, 1))
                intValor = intValor * (10 - intPos)
                intSoma = intSoma + intValor
            Next
            
            intResto = intSoma Mod 11
            strDigito1 = Right(IIf(intResto < 2, "0", CStr(11 - intResto)), 1)
            strBase2 = Left(strBase, 8) & strDigito1
            
            If strBase2 = strOrigem Then
                ChecaInscrE = True
            End If
            
        Case "GO" ' Goias
            strBase = Left(Trim(strOrigem) & "000000000", 9)
            
            If InStr(1, "10,11,15", Left(strBase, 2), vbTextCompare) > 0 Then
                intSoma = 0
                For intPos = 1 To 8
                    intValor = CInt(Mid(strBase, intPos, 1))
                    intValor = intValor * (10 - intPos)
                    intSoma = intSoma + intValor
                Next
                
                intResto = intSoma Mod 11
                If intResto = 0 Then
                    strDigito1 = "0"
                ElseIf intResto = 1 Then
                        intNumero = CInt(Left(strBase, 8))
                        strDigito1 = Right(IIf(intNumero >= 10103105 And intNumero <= 10119997, "1", "0"), 1)
                    Else
                        strDigito1 = Right(CStr(11 - intResto), 1)
                    End If
                    
                    strBase2 = Left(strBase, 8) & strDigito1
                If strBase2 = strOrigem Then
                    ChecaInscrE = True
                End If
            End If
            
        Case "MA" ' Maranhão
            strBase = Left(Trim(strOrigem) & "000000000", 9)
            
            If Left(strBase, 2) = "12" Then
                intSoma = 0
                For intPos = 1 To 8
                    intValor = CInt(Mid(strBase, intPos, 1))
                    intValor = intValor * (10 - intPos)
                    intSoma = intSoma + intValor
                Next
            
                intResto = intSoma Mod 11
                strDigito1 = Right(IIf(intResto < 2, "0", CStr(11 - intResto)), 1)
                strBase2 = Left(strBase, 8) & strDigito1
                
                If strBase2 = strOrigem Then
                    ChecaInscrE = True
                End If
            End If
            
        Case "MT" ' Mato Grosso
            While Len(strOrigem) < 11
                strOrigem = "0" & strOrigem
            Wend
            
            strBase = Left(Trim(strOrigem) & "000000000", 11)
            d01 = CInt(Mid(strBase, 1, 1))
            d02 = CInt(Mid(strBase, 2, 1))
            d03 = CInt(Mid(strBase, 3, 1))
            d04 = CInt(Mid(strBase, 4, 1))
            d05 = CInt(Mid(strBase, 5, 1))
            d06 = CInt(Mid(strBase, 6, 1))
            d07 = CInt(Mid(strBase, 7, 1))
            d08 = CInt(Mid(strBase, 8, 1))
            d09 = CInt(Mid(strBase, 9, 1))
            d10 = CInt(Mid(strBase, 10, 1))
            dfinal = CInt(Mid(strBase, 11, 1))
            
            ds = 3 * d01 + 2 * d02 + 9 * d03 + 8 * d04 + 7 * d05 + 6 * d06 + 5 * d07 + 4 * d08 + _
                 3 * d09 + 2 * d10
            
            aux1 = Fix(ds / 11)
            aux1 = aux1 * 11
            aux2 = ds - aux1
            
            If aux2 = 0 Or aux2 = 1 Then
                digVerificador = 0
            Else
                digVerificador = 11 - aux2
            End If
            
            If dfinal = digVerificador Then
                ChecaInscrE = True
            End If
            
        Case "MS" ' Mato Grosso do Sul
            strBase = Left(Trim(strOrigem) & "000000000", 9)
            
            If Left(strBase, 2) = "28" Then
                intSoma = 0
            
                For intPos = 1 To 8
                    intValor = CInt(Mid(strBase, intPos, 1))
                    intValor = intValor * (10 - intPos)
                    intSoma = intSoma + intValor
                Next
                
                intResto = intSoma Mod 11
                strDigito1 = Right(IIf(intResto < 2, "0", CStr(11 - intResto)), 1)
                strBase2 = Left(strBase, 8) & strDigito1
                
                If strBase2 = strOrigem Then
                    ChecaInscrE = True
                End If
            End If
            
        Case "MG" ' Minas Gerais
            strBase = Left(Trim(strOrigem) & "0000000000000", 13)
            strBase2 = Left(strBase, 3) & "0" & Mid(strBase, 4, 8)
            intNumero = 2
            
            For intPos = 1 To 12
                intValor = CInt(Mid(strBase2, intPos, 1))
                intNumero = IIf(intNumero = 2, 1, 2)
                intValor = intValor * intNumero
                
                If intValor > 9 Then
                    strDigito1 = FormatNumber(intValor, "00")
                    intValor = CInt(Left(strDigito1, 1)) + CInt(Right(strDigito1, 1))
                End If
                
                intSoma = intSoma + intValor
            Next
            
            intValor = intSoma
            
            While Right(FormatNumber(intValor, "000"), 1) <> "0"
                intValor = intValor + 1
            Wend
            
            strDigito1 = Right(FormatNumber(intValor - intSoma, "00"), 1)
            strBase2 = Left(strBase, 11) & strDigito1
            intSoma = 0
            intPeso = 2
            
            For intPos = 12 To 1 Step -1
                intValor = CInt(Mid(strBase2, intPos, 1))
                intValor = intValor * intPeso
                intSoma = intSoma + intValor
                intPeso = intPeso + 1
                
                If intPeso > 11 Then
                    intPeso = 2
                End If
            Next
            
            intResto = intSoma Mod 11
            strDigito2 = Right(IIf(intResto < 2, "0", CStr(11 - intResto)), 1)
            strBase2 = strBase2 & strDigito2
            
            If strBase2 = strOrigem Then
                ChecaInscrE = True
            End If
            
        Case "PA" ' Para
            strBase = Left(Trim(strOrigem) & "000000000", 9)
            
            If Left(strBase, 2) = "15" Then
                intSoma = 0
                For intPos = 1 To 8
                    intValor = CInt(Mid(strBase, intPos, 1))
                    intValor = intValor * (10 - intPos)
                intSoma = intSoma + intValor
                Next
                
                intResto = intSoma Mod 11
                strDigito1 = Right(IIf(intResto < 2, "0", CStr(11 - intResto)), 1)
                strBase2 = Left(strBase, 8) & strDigito1
            
                If strBase2 = strOrigem Then
                    ChecaInscrE = True
                End If
            End If
            
        Case "PB" ' Paraiba
            strBase = Left(Trim(strOrigem) & "000000000", 9)
            intSoma = 0
            
            For intPos = 1 To 8
                intValor = CInt(Mid(strBase, intPos, 1))
                intValor = intValor * (10 - intPos)
                intSoma = intSoma + intValor
            Next
            
            intResto = intSoma Mod 11
            intValor = 11 - intResto
            
            If intValor > 9 Then
                intValor = 0
            End If
            
            strDigito1 = Right(CStr(intValor), 1)
            strBase2 = Left(strBase, 8) & strDigito1
            
            If strBase2 = strOrigem Then
                ChecaInscrE = True
            End If
            
        Case "PE" ' Pernambuco
            strBase = Left(Trim(strOrigem) & "000000000", 9)
            intSoma = 0
            intPeso = 2
            
            For intPos = 7 To 1 Step -1
                intValor = CInt(Mid(strBase, intPos, 1))
                intValor = intValor * intPeso
                intSoma = intSoma + intValor
                intPeso = intPeso + 1
            Next
        
            intResto = intSoma Mod 11
            
            If intResto = 0 Or intResto = 1 Then
                strDigito1 = 0
            Else
                strDigito1 = 11 - intResto
            End If
        
            intSoma = 0
            intPeso = 2
        
            For intPos = 8 To 1 Step -1
                intValor = CInt(Mid(strBase, intPos, 1))
                intValor = intValor * intPeso
                intSoma = intSoma + intValor
                intPeso = intPeso + 1
            Next
        
            intResto = intSoma Mod 11
            
            If intResto = 0 Or intResto = 1 Then
                strDigito2 = 0
            Else
                strDigito2 = 11 - intResto
            End If
            
            strBase2 = Left(strBase, 7) & strDigito1 & strDigito2
            
            If strBase2 = strOrigem Then
                ChecaInscrE = True
            End If
        
        Case "PI" ' Piaui
            strBase = Left(Trim(strOrigem) & "000000000", 9)
            intSoma = 0
            
            For intPos = 1 To 8
                intValor = CInt(Mid(strBase, intPos, 1))
                intValor = intValor * (10 - intPos)
                intSoma = intSoma + intValor
            Next
            
            intResto = intSoma Mod 11
            strDigito1 = Right(IIf(intResto < 2, "0", CStr(11 - intResto)), 1)
            strBase2 = Left(strBase, 8) & strDigito1
            
            If strBase2 = strOrigem Then
                ChecaInscrE = True
            End If
            
        Case "PR" ' Parana
            strBase = Left(Trim(strOrigem) & "0000000000", 10)
            intSoma = 0
            intPeso = 2
            
            For intPos = 8 To 1 Step -1
                intValor = CInt(Mid(strBase, intPos, 1))
                intValor = intValor * intPeso
                intSoma = intSoma + intValor
                intPeso = intPeso + 1
                If intPeso > 7 Then
                intPeso = 2
                End If
            Next
            
            intResto = intSoma Mod 11
            strDigito1 = Right(IIf(intResto < 2, "0", CStr(11 - intResto)), 1)
            strBase2 = Left(strBase, 8) & strDigito1
            intSoma = 0
            intPeso = 2
            
            For intPos = 9 To 1 Step -1
                intValor = CInt(Mid(strBase2, intPos, 1))
                intValor = intValor * intPeso
                intSoma = intSoma + intValor
                intPeso = intPeso + 1
                
                If intPeso > 7 Then
                    intPeso = 2
                End If
            Next
            
            intResto = intSoma Mod 11
            strDigito2 = Right(IIf(intResto < 2, "0", CStr(11 - intResto)), 1)
            strBase2 = strBase2 & strDigito2
            
            If strBase2 = strOrigem Then
                ChecaInscrE = True
            End If
            
        Case "RJ" ' Rio de Janeiro
            strBase = Left(Trim(strOrigem) & "00000000", 8)
            intSoma = 0
            intPeso = 2
            
            For intPos = 7 To 1 Step -1
                intValor = CInt(Mid(strBase, intPos, 1))
                intValor = intValor * intPeso
                intSoma = intSoma + intValor
                intPeso = intPeso + 1
                
                If intPeso > 7 Then
                    intPeso = 2
                End If
            Next
            
            intResto = intSoma Mod 11
            strDigito1 = Right(IIf(intResto < 2, "0", CStr(11 - intResto)), 1)
            strBase2 = Left(strBase, 7) & strDigito1
            
            If strBase2 = strOrigem Then
                ChecaInscrE = True
            End If
        
        Case "RN" ' Rio Grande do Norte
            strBase = Left(Trim(strOrigem) & "000000000", 9)
            
            If Left(strBase, 2) = "20" Then
                intSoma = 0
                
                For intPos = 1 To 8
                    intValor = CInt(Mid(strBase, intPos, 1))
                    intValor = intValor * (10 - intPos)
                    intSoma = intSoma + intValor
                Next
            
                intSoma = intSoma * 10
                intResto = intSoma Mod 11
                strDigito1 = Right(IIf(intResto > 9, "0", CStr(intResto)), 1)
                strBase2 = Left(strBase, 8) & strDigito1
                
                If strBase2 = strOrigem Then
                    ChecaInscrE = True
                End If
            End If
        
        Case "RO" ' Rondonia
            strBase = Left(Trim(strOrigem) & "000000000", 14)
            d01 = CInt(Mid(strBase, 1, 1))
            d02 = CInt(Mid(strBase, 2, 1))
            d03 = CInt(Mid(strBase, 3, 1))
            d04 = CInt(Mid(strBase, 4, 1))
            d05 = CInt(Mid(strBase, 5, 1))
            d06 = CInt(Mid(strBase, 6, 1))
            d07 = CInt(Mid(strBase, 7, 1))
            d08 = CInt(Mid(strBase, 8, 1))
            d09 = CInt(Mid(strBase, 9, 1))
            d10 = CInt(Mid(strBase, 10, 1))
            d11 = CInt(Mid(strBase, 11, 1))
            d12 = CInt(Mid(strBase, 12, 1))
            d13 = CInt(Mid(strBase, 13, 1))
            dfinal = CInt(Mid(strBase, 14, 1))
            ds = 6 * d01 + 5 * d02 + 4 * d03 + 3 * d04 + 2 * d05 + 9 * d06 + 8 * d07 + 7 * d08 + _
                              6 * d09 + 5 * d10 + 4 * d11 + 3 * d12 + 2 * d13
            aux1 = Fix(ds / 11)
            aux1 = aux1 * 11
            aux2 = ds - aux1
            digVerificador = 11 - aux2
            
            If digVerificador > 9 Then
                resto_do_calculo = digVerificador - 10
            Else
                resto_do_calculo = digVerificador
            End If
            
            If dfinal <> resto_do_calculo Then
                ChecaInscrE = False
            Else
                ChecaInscrE = True
            End If
            
        Case "RR" ' Roraima
            strBase = Left(Trim(strOrigem) & "000000000", 9)
        
            If Left(strBase, 2) = "24" Then
                intSoma = 0
                
                For intPos = 1 To 8
                    intValor = CInt(Mid(strBase, intPos, 1))
                    intValor = intValor * intPos
                    intSoma = intSoma + intValor
                Next
                
                intResto = intSoma Mod 9
                strDigito1 = Right(CStr(intResto), 1)
                strBase2 = Left(strBase, 8) & strDigito1
                
                If strBase2 = strOrigem Then
                    ChecaInscrE = True
                End If
            End If
            
        Case "RS" ' Rio Grande do Sul
            strBase = Left(Trim(strOrigem) & "0000000000", 10)
            intNumero = CInt(Left(strBase, 3))
            
            If intNumero > 0 And intNumero < 468 Then
                intSoma = 0
                intPeso = 2
                For intPos = 9 To 1 Step -1
                    intValor = CInt(Mid(strBase, intPos, 1))
                    intValor = intValor * intPeso
                    intSoma = intSoma + intValor
                    intPeso = intPeso + 1
                    If intPeso > 9 Then
                        intPeso = 2
                    End If
                Next
                
                intResto = intSoma Mod 11
                intValor = 11 - intResto
                If intValor > 9 Then
                    intValor = 0
                End If
                
                strDigito1 = Right(CStr(intValor), 1)
                strBase2 = Left(strBase, 9) & strDigito1
                
                If strBase2 = strOrigem Then
                    ChecaInscrE = True
                End If
            End If
            
        Case "SC" ' Santa Catarina
            strBase = Left(Trim(strOrigem) & "000000000", 9)
            intSoma = 0
            
            For intPos = 1 To 8
                intValor = CInt(Mid(strBase, intPos, 1))
                intValor = intValor * (10 - intPos)
                intSoma = intSoma + intValor
            Next
            
            intResto = intSoma Mod 11
            strDigito1 = Right(IIf(intResto < 2, "0", CStr(11 - intResto)), 1)
            strBase2 = Left(strBase, 8) & strDigito1
            
            If strBase2 = strOrigem Then
                ChecaInscrE = True
            End If
            
        Case "SE" ' Sergipe
            strBase = Left(Trim(strOrigem) & "000000000", 9)
            intSoma = 0
            
            For intPos = 1 To 8
                intValor = CInt(Mid(strBase, intPos, 1))
                intValor = intValor * (10 - intPos)
                intSoma = intSoma + intValor
            Next
            
            intResto = intSoma Mod 11
            intValor = 11 - intResto
            
            If intValor > 9 Then
                intValor = 0
            End If
            
            strDigito1 = Right(CStr(intValor), 1)
            strBase2 = Left(strBase, 8) & strDigito1
            
            If strBase2 = strOrigem Then
                ChecaInscrE = True
            End If
            
        Case "SP" ' São Paulo
            If Left(strOrigem, 1) = "P" Then
                strBase = Left(Trim(strOrigem) & "0000000000000", 13)
                strBase2 = Mid(strBase, 2, 8)
                intSoma = 0
                intPeso = 1
                
                For intPos = 1 To 8
                    intValor = CInt(Mid(strBase, intPos, 1))
                    intValor = intValor * intPeso
                    intSoma = intSoma + intValor
                    intPeso = intPeso + 1
                    
                    If intPeso = 2 Then
                        intPeso = 3
                    End If
                        
                    If intPeso = 9 Then
                        intPeso = 10
                    End If
                Next
                
                intResto = intSoma Mod 11
                strDigito1 = Right(CStr(intResto), 1)
                strBase2 = Left(strBase, 8) & strDigito1 & Mid(strBase, 11, 3)
            Else
                strBase = Left(Trim(strOrigem) & "000000000000", 12)
                intSoma = 0
                intPeso = 1
                
                For intPos = 1 To 8
                    intValor = CInt(Mid(strBase, intPos, 1))
                    intValor = intValor * intPeso
                    intSoma = intSoma + intValor
                    intPeso = intPeso + 1
                    
                    If intPeso = 2 Then
                        intPeso = 3
                    End If
                
                    If intPeso = 9 Then
                        intPeso = 10
                    End If
                Next
                
                intResto = intSoma Mod 11
                strDigito1 = Right(CStr(intResto), 1)
                strBase2 = Left(strBase, 8) & strDigito1 & Mid(strBase, 10, 2)
                intSoma = 0
                intPeso = 2
                
                For intPos = 11 To 1 Step -1
                    intValor = CInt(Mid(strBase, intPos, 1))
                    intValor = intValor * intPeso
                    intSoma = intSoma + intValor
                    intPeso = intPeso + 1
                
                    If intPeso > 10 Then
                        intPeso = 2
                    End If
                Next
            
                intResto = intSoma Mod 11
                strDigito2 = Right(CStr(intResto), 1)
                strBase2 = strBase2 & strDigito2
            End If
            
            If strBase2 = strOrigem Then
                ChecaInscrE = True
            End If
            
        Case "TO" ' Tocantins
            strBase = Left(Trim(strOrigem) & "00000000000", 11)
            If InStr(1, "01,02,03,99", Mid(strBase, 3, 2), vbTextCompare) > 0 Then
                strBase2 = Left(strBase, 2) & Mid(strBase, 5, 6)
                intSoma = 0
                
                For intPos = 1 To 8
                    intValor = CInt(Mid(strBase2, intPos, 1))
                    intValor = intValor * (10 - intPos)
                    intSoma = intSoma + intValor
                Next
        
                intResto = intSoma Mod 11
                strDigito1 = Right(IIf(intResto < 2, "0", CStr(11 - intResto)), 1)
                strBase2 = Left(strBase, 10) & strDigito1
                
                If strBase2 = strOrigem Then
                    ChecaInscrE = True
                End If
            End If
    End Select
End Function

Sub lsShowFrmIE()
    frmValidaIE.Show
End Sub

Para usar a função clique em qualquer lugar na sua planilha do Excel e digite ChecaInscrE, e passe o número da inscrição estadual, conforme abaixo:

Validar Inscrição Estadual Excel

Você também pode implementar a função para que fique disponível sempre que abrir o Excel, criando assim um suplemento no Excel, veja neste artigo: https://www.guiadoexcel.com.br/como-criar-funcoes-proprias-no-excel-com-vba/

Clicando no botão “Exemplo de formulário” você tem também um exemplo do uso da função de validação de inscrição estadual em um formulário VBA, basta escolher a UF e digitar a Inscrição estadual para que seja retornado se o mesmo é válido.

Validar Inscrição Estadual Excel 3

Para testar inscrições estaduais você também pode usar este site: https://www.4devs.com.br/gerador_de_inscricao_estadual

O código fonte utilizado para a validação das inscrições estaduais foi traduzido para VBA, atualizado no caso do estado de Pernambuco e feita a indentação, baseado no fonte de: https://www.codigofonte.com.br/codigos/validar-inscricao-estadual-para-qualquer-estado

Para baixar a planilha exemplo com o código implementado gratuitamente clique no botão abaixo.

Baixe a planilha

Abraço

Marcos Rieper


Marcos Rieper

Pai, marido, professor e consultor em Excel.

Obrigado por ler este artigo, este blog foi criado para difundir o conhecimento em Excel à todos.

Divulgamos novos artigos nas redes sociais, basta clicar nos ícones abaixo.

Excel não precisa ser complicado

Assine nossa newsletter e receba dicas práticas para dominar o excel