Planilha Excel para validar e-mail

Planilha Excel para validar e-mail

Neste artigo você aprenderá a criar uma função para validar e-mail no Excel e também terá uma planilha Excel pronta para esta validação.

Esta função não foi criada pelo nosso site, mas é amplamente difundida na internet, assim como a função Excel que escreve número por extenso.

Mostraremos neste artigo como aplicar esta função de duas formas. Em uma única planilha ou em todas as planilhas abertas no Excel.

Implantando a função:

Validar email no Excel VBA
Validar email no Excel VBA 2

Pra instalar a função somente na planilha habilite a guia Desenvolvedor da seguinte forma:

  1. Clique no botão Arquivo->Opções->Personalizar Faixa de Opções, marque a faixa Desenvolvedor.
Validar email no Excel VBA 3

2. Clique na guia Desenvolvedor->Visual Basic

3. Clique no menu->Inserir->Módulo

Validar email no Excel VBA 4

4. Clique no módulo inserido e cole o seguinte código:

Function IsEmailValid(strEmail)
    Dim strArray As Variant
    Dim strItem As Variant
    Dim i As Long, c As String, blnIsItValid As Boolean
    blnIsItValid = True
     
    i = Len(strEmail) - Len(Application.Substitute(strEmail, "@", ""))
    If i  1 Then IsEmailValid = False: Exit Function
    ReDim strArray(1 To 2)
    strArray(1) = Left(strEmail, InStr(1, strEmail, "@", 1) - 1)
    strArray(2) = Application.Substitute(Right(strEmail, Len(strEmail) - Len(strArray(1))), "@", "")
    For Each strItem In strArray
        If Len(strItem) <= 0 Then
            blnIsItValid = False
            IsEmailValid = blnIsItValid
            Exit Function
        End If
        For i = 1 To Len(strItem)
            c = LCase(Mid(strItem, i, 1))
            If InStr("abcdefghijklmnopqrstuvwxyz_-.", c) <= 0 And Not IsNumeric(c) Then
                blnIsItValid = False
                IsEmailValid = blnIsItValid
                Exit Function
            End If
        Next i
        If Left(strItem, 1) = "." Or Right(strItem, 1) = "." Then
            blnIsItValid = False
            IsEmailValid = blnIsItValid
            Exit Function
        End If
    Next strItem
    If InStr(strArray(2), ".") <= 0 Then
        blnIsItValid = False
        IsEmailValid = blnIsItValid
        Exit Function
    End If
    i = Len(strArray(2)) - InStrRev(strArray(2), ".")
    If i  2 And i  3 Then
        blnIsItValid = False
        IsEmailValid = blnIsItValid
        Exit Function
    End If
    If InStr(strEmail, "..") > 0 Then
        blnIsItValid = False
        IsEmailValid = blnIsItValid
        Exit Function
    End If
    IsEmailValid = blnIsItValid
End Function

5. Digite o e-mail na sua planilha

6. Digite em outra célula a função =IsEmailValid(A1), sendo A1 a célula aonde está o e-mail

Pra instalar a função em todas as planilhas habilite a guia Desenvolvedor da seguinte forma:

  1. Abra um novo documento no Excel e clique em salvar, nomeie com um nome sugestivo como Minhas funções, no campo Salvar como tipo: altere o tipo para Suplemento do Excel(*.xlam).
  2. Clique em Guia desenvolvedor->Visual Basic.
  3. Neste local selecione na guia de projetos VBAProject(Minhas funções.xlam) que acabou de ser criado.
Function IsEmailValid(strEmail)
    Dim strArray As Variant
    Dim strItem As Variant
    Dim i As Long, c As String, blnIsItValid As Boolean
    blnIsItValid = True
     
    i = Len(strEmail) - Len(Application.Substitute(strEmail, "@", ""))
    If i  1 Then IsEmailValid = False: Exit Function
    ReDim strArray(1 To 2)
    strArray(1) = Left(strEmail, InStr(1, strEmail, "@", 1) - 1)
    strArray(2) = Application.Substitute(Right(strEmail, Len(strEmail) - Len(strArray(1))), "@", "")
    For Each strItem In strArray
        If Len(strItem) <= 0 Then
            blnIsItValid = False
            IsEmailValid = blnIsItValid
            Exit Function
        End If
        For i = 1 To Len(strItem)
            c = LCase(Mid(strItem, i, 1))
            If InStr("abcdefghijklmnopqrstuvwxyz_-.", c) <= 0 And Not IsNumeric(c) Then
                blnIsItValid = False
                IsEmailValid = blnIsItValid
                Exit Function
            End If
        Next i
        If Left(strItem, 1) = "." Or Right(strItem, 1) = "." Then
            blnIsItValid = False
            IsEmailValid = blnIsItValid
            Exit Function
        End If
    Next strItem
    If InStr(strArray(2), ".") <= 0 Then
        blnIsItValid = False
        IsEmailValid = blnIsItValid
        Exit Function
    End If
    i = Len(strArray(2)) - InStrRev(strArray(2), ".")
    If i  2 And i  3 Then
        blnIsItValid = False
        IsEmailValid = blnIsItValid
        Exit Function
    End If
    If InStr(strEmail, "..") > 0 Then
        blnIsItValid = False
        IsEmailValid = blnIsItValid
        Exit Function
    End If
    IsEmailValid = blnIsItValid
End Function
Validar email no Excel VBA 5

4. Clique no botão Salvar e feche a janela do Visual Basic.
5. Clique no ícone do Excel e depois no botão Opções.
6. Clique no botão Suplementos.
7. Selecione no botão Gerenciar o tipo Suplementos do Excel e clique no botão Ir.
8. Irá abrir uma tela para inserir o suplemento criado.

Validar email no Excel VBA 6

9. Clique no botão Procurar e selecione o arquivo de suplemento que você criou.

10. Pronto! O suplemento estará com as funções disponíveis para uso.

11. Digite o e-mail na sua planilha

12. Digite em outra célula a função =IsEmailValid(A1), sendo A1 a célula aonde está o e-mail

Abraço

Marcos Rieper

Curso Excel Completo – Do Básico ao VBA

Quer aprender Excel do Básico, passando pela Avançado e chegando no VBA? Clique na imagem abaixo:


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