Function CheckPostcode(ByVal postcode) Dim OutCode As String Dim InCode As String 'Set case and remove embedded spaces postcode = UCase(Replace(postcode, " ", "")) 'Check the length of the postcode If (Len(postcode) < 5) Or (Len(postcode) > 7) Then 'Invalid length (must be between 5 and 7 characters long) PostcodeOk = False Else 'Length OK, so split the postcode and proceed with the validation OutCode = Left(postcode, Len(postcode) - 3) InCode = Right(postcode, 3) If (Not InCode Like "#[A-Z][A-Z]") Then 'Invalid incode (must be of the format "NAA") PostcodeOk = False ElseIf (Mid(InCode, 2, 1) Like "[C,I,K,M,O,V]") Or (Mid(InCode, 3, 1) Like "[C,I,K,M,O,V]") Then 'Invalid incode (2nd or 3rd characters cannot be C, I, K, M, O or V) PostcodeOk = False ElseIf (Left(OutCode, 1) Like "[Q,V,X]") Or (Mid(OutCode, 2, 1) Like "[J,I,Z]") Then 'Invalid outcode (1st character cannot be Q, V or X, second cannot be J, I or Z) PostcodeOk = False ElseIf (Len(OutCode) = 2) And (Not OutCode Like "[A-Z]#") Then 'Invalid outcode (2 character outcode must be of the format "AN") PostcodeOk = False ElseIf (Len(OutCode) = 3) And (Not OutCode Like "[A-Z]##") And (Not OutCode Like "[A-Z]#[A-Z]") And (Not OutCode Like "[A-Z][A-Z]#") Then 'Invalid outcode (3 character outcode must be of the format "ANN", "ANA" or "AAN") PostcodeOk = False ElseIf (Len(OutCode) = 4) And (Not OutCode Like "[A-Z][A-Z]##") And (Not OutCode Like "[A-Z][A-Z]#[A-Z]") Then 'Invalid outcode (4 character outcode must be of the format "AANN" or "AANA") PostcodeOk = False Else 'Valid postcode PostcodeOk = True End If End If Return PostcodeOk End Function