Home » excel » excel – How to format specific mail value in VBA

excel – How to format specific mail value in VBA

Posted by: admin May 14, 2020 Leave a comment

Questions:

I want to validate specific mail value with RegEx using vba.

Example: [email protected]

User input email must exactly include @desktopfromanoth.com. Can I validate that with RegEx?

My code is here:

Private Function ValidateEmailAddress(ByRef strEmailAddress As String) As Boolean  
    Dim objRegExp As New RegExp

    objRegExp.IgnoreCase = True
    objRegExp.pattern = "^[\w-\.][email protected]([a-z]{16}+\.)+[A-Za-z]{2,3}$"
    ValidateEmailAddress = objRegExp.Test(strEmailAddress)
End Function
How to&Answers:

The following will validate for any email that ends with @desktopfromanoth.com

^[_a-z0-9-]+(.[a-z0-9-]+)@desktopfromanoth\.com$

For further information see Email Address Regular Expression That 99.99% Works.

If you want to match multiple email addresses use the following instead:

[_a-z0-9-]+(.[a-z0-9-]+)@desktopfromanoth\.com

See https://regex101.com/r/PjQKDx/1

Answer:

Normally I use this one and it works in the most of cases:

^\[email protected][a-zA-Z_]+?\.[a-zA-Z]{2,3}$