MS Access | Detectando e Extraindo Caracteres Invisíveis - UDF for Excel and Access that Extracts all Invisible and Special Characters

MS Access | Detectando e Extraindo Caracteres Invisíveis - UDF for Excel and Access that Extracts all Invisible and Special Characters

Não é raro precisarmos extrair caracteres invisíveis de uma string. E isso pode tomar um tempo enorme até descobrirmos qual é o real problema.


Conheça e faça download de uma planilha pronta do Excel que calcula o TEMPO e a DISTÂNCIA de viagem usando a API do Google Maps


Alguns caracteres especiais não podem ser vistos, pois atrapalham no momento em que agrupamos dados. 

Como retirá-los?

Essa função abaixo foi melhorada, tem o escopo ampliado para pegar vários desses caracteres:

Public Function ExtractInviChars (strCk As String, Optional strRepWith As String = "") As String

    '   co-Author: André Bernardes

    '        Date: 16.08.22

    ' Description: Extrai caracters invisíveis.

    ' Caracteres de nome de arquivo ilegais incluídos numa string padrão, entre outros, são: ? [ ] /  = + < > :; * " , '

    ' MS EXCEL: A função TIRAR foi desenvolvida para remover os

    ' 32 primeiros caracteres não imprimíveis no código ASCII

    ' de 7 bits (valores de 0 a 31) do texto. O conjunto de

    ' caracteres Unicode contém outros caracteres não imprimíveis

    ' (valores 127, 129, 141, 143, 144 e 157). Por si própria, a

    ' função TIRAR NÃO remove esses caracteres adicionais não

    ' imprimíveis.

    'From http://www.utteraccess.com/wiki/index.php?title=Strip_Illegal_Characters&diff=4158&oldid=4156

    On Error GoTo StripIllErr 

    Dim intI As Integer

    Dim intPassedString As Integer

    Dim intCheckString As Integer

    Dim strChar As String

    Dim strIllegalChars As String

    Dim intReplaceLen As Integer

    If IsNull(strCk) Then Exit Function

    'Adicione ou remova todos os caracteres que precisar na string abaixo que servirá de base comparativa:

    Let strIllegalChars = "?[]/=+<>:;,*-" & Chr(34) & Chr(39) & Chr(32) _

                        & Chr(0) & Chr(1) & Chr(2) & Chr(3) & Chr(4) & Chr(5) & Chr(6) & Chr(7) & Chr(8) _

                        & Chr(9) & Chr(10) & Chr(11) & Chr(12) & Chr(13) & Chr(14) & Chr(15) & Chr(16) _

                        & Chr(17) & Chr(18) & Chr(19) & Chr(20) & Chr(21) & Chr(22) & Chr(23) & Chr(24) _

                        & Chr(25) & Chr(26) & Chr(27) & Chr(28) & Chr(29) & Chr(30) & Chr(31) & Chr(127) _

                        & Chr(129) & Chr(141) & Chr(143) & Chr(144) & Chr(157)

                        '& Chr() & Chr() & Chr() & Chr() & Chr() & Chr() & Chr() & Chr() _

                        '& Chr() & Chr() & Chr() & Chr() & Chr() & Chr() & Chr() & Chr() _

                        '& Chr() & Chr() & Chr() & Chr() & Chr() & Chr() & Chr() & Chr() _

                        '& Chr() & Chr() & Chr() & Chr() & Chr() & Chr() & Chr() & Chr() _

                        '& Chr() & Chr() & Chr() & Chr() & Chr() & Chr() & Chr() & Chr() _

                        '& Chr() & Chr() & Chr() & Chr() & Chr() & Chr() & Chr() & Chr() _

                        '& Chr() & Chr() & Chr() & Chr() & Chr() & Chr() & Chr() & Chr() _

                        '& Chr() & Chr() & Chr() & Chr() & Chr() & Chr() & Chr() & Chr() _

                        '& Chr() & Chr() & Chr() & Chr() & Chr() & Chr() & Chr() & Chr() _

                        '& Chr() & Chr() & Chr() & Chr() & Chr() & Chr() & Chr() & Chr() _

                        '& Chr() & Chr() & Chr() & Chr() & Chr() & Chr() & Chr() & Chr() _

                        '& Chr() & Chr() & Chr() & Chr() & Chr() & Chr() & Chr() & Chr() _

                        '& Chr() & Chr() & Chr() & Chr() & Chr() & Chr() & Chr() & Chr()

                                               

    Let intPassedString = Len(strCk)

    Let intCheckString = Len(strIllegalChars)

    Let intReplaceLen = Len(strRepWith)

    

    If intReplaceLen > 0 Then   'Caractere foi inserido para ser usado como caractere de substituição

    

        If intReplaceLen = 1 Then   'verifique se o caractere em si não é um caractere ilegal

        

            If InStr(strIllegalChars, strRepWith) > 0 Then

                MsgBox "Você não pode substituir um caractere ilegal por outro caractere ilegal", _

                       vbOKOnly + vbExclamation, "Caractere inválido"

                       

                Let fStripIllegal = strCk

                

                Exit Function

            End If

        Else   'apenas um caractere de substituição permitido

            MsgBox "Apenas um caractere é permitido como caractere de substituição", _

                   vbOKOnly + vbExclamation, "String de substituição inválida"

                   

            Let fStripIllegal = strCk

            

            Exit Function

            

        End If

    End If


    If intPassedString < intCheckString Then

        For intI = 1 To intCheckString

        

            Let strChar = Mid(strIllegalChars, intI, 1)

            

            If InStr(strCk, strChar) > 0 Then

                Let strCk = Replace(strCk, strChar, strRepWith)

            End If

            

        Next intI

    Else

        For intI = 1 To intPassedString

        

            Let strChar = Mid(strIllegalChars, intI, 1)

            

            If InStr(strCk, strChar) > 0 Then

                Let strCk = Replace(strCk, strChar, strRepWith)

            End If

            

        Next intI

    End If

    

    Let ExtractInviChars = Trim(strCk)

    

StripIllErrExit:

    Exit Function

    

StripIllErr:

    MsgBox "Ocorreu o seguinte erro: " & Err.Number & vbCrLf _

         & Err.Description, vbOKOnly + vbExclamation, "Erro inesperado..."

         

    Let fStripIllegal = strCk

    

    Resume StripIllErrExit

End Function


Comente e compartilhe este artigo!


brazilsalesforceeffectiveness@gmail.com

Nenhum comentário:

Postar um comentário

diHITT - Notícias