Home » excel » excel – How to create a regex VBA macro for GIIN format validation

excel – How to create a regex VBA macro for GIIN format validation

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m trying to create a macro that will verify data in one column and then let me know if they are correctly formatted in the next column. I am very new to VBA so I apologize if my code is messy.

The format I am trying to verify is ABC123.AB123.AB.123 — The first two sections can contain letters/numbers, the third section only letters, and the last section only numbers.

Any guidance would be greatly appreciated!

Function ValidGIIN(myGIIN As String) As String
    Dim regExp As Object

    Set regExp = CreateObject("VBScript.Regexp")

    If Len(myGIIN) Then
            .Global = True
            .IgnoreCase = True
            .Pattern = "[a-zA-Z0-9_][a-zA-Z0-9_][a-zA-Z0-9_][a-zA-Z0-9_][a-zA-Z0-9_][a-zA-Z0-9_][.][a-zA-Z0-9_][a-zA-Z0-9_][a-zA-Z0-9_][a-zA-Z0-9_][a-zA-Z0-9_][.][a-zA-z_][a-zA-z_][.][0-9][0-9][0-9]"

        End With

        If regExp.Test(myGIIN) = True Then
            ValidGIIN = "Valid"
        Else
            ValidGIIN = "Invalid"

        End If
    End If
    Set regExp = Nothing

End Function
How to&Answers:

Try the following pattern

[a-zA-Z0-9]{6}\.[a-zA-Z0-9]{5}\.[A-Za-z]{2}\.\d{3}

You could call your function in a loop over cells in a column and use offset(0,1) to write result to next column to right.