Using InStr() in VBA to flag emails

65 views Asked by At

I'm trying to make a macro that will go through a list of emails and flag potential problems by changing their color.

I was trying to do it by using the Like function, but I forgot that it's case sensitive and the resulting code looks kind of big and messy anyway. I know I could use UPPER() to solve it, but it's already a large messy looking code and I hoped to avoid that.

Is there a way I can use InStr() instead? I've seen people say it's a better option than Like and I've been trying to look up how this function works, but the explanations I've found online are difficult for me to follow.

To explain the code I have so far, it's designed to flag anything with multiple emails as Blue, anything that isn't an email as red, and anything within a list of popular public email domains (Gmail, yahoo, hotmail etc etc) as green. I've trimmed the list of domains down from several hundred to just three for simplification here.

Sub Email_Check()
    Do    
        If (ActiveCell Like "*@*@*") = True Then
            ActiveCell.Interior.ColorIndex = 8
        ElseIf (ActiveCell Like "*@*") = False Or (ActiveCell Like "*.*") = False = True _
        Then
            ActiveCell.Interior.ColorIndex = 3
        ElseIf ActiveCell Like "*gmail.com" = True Or ActiveCell Like "*hotmail.com" = True Or ActiveCell Like "*yahoo.com" = True _
        Then
            ActiveCell.Interior.ColorIndex = 4
        End If
        
        ActiveCell.Offset(1, 0).Range("A1").Select
    Loop Until Application.CountA(ActiveCell.EntireRow) = 0
End Sub
0

There are 0 answers