Hello,
in 2011 there was a question about filtering a postcode/zip-code out of a text (Extract postcode from text string)
Supermoderator Krishnakumar replied with a function that does the job perfectly.
He uses the code:
Code
- Function POSTCODE(ByVal InpStr As String) As String
- Dim w As String
- Dim j As Long
- Dim Ptrn1
- Dim Ptrn2 As String
- x = Split(Replace(InpStr, ",", " "), " ")
- Ptrn1 = Array("[A-Z][0-9]", "[A-Z][0-9][0-9]", "[A-Z][A-Z][0-9]", "[A-Z][A-Z][0-9][0-9]", _
- "[A-Z][0-9][A-Z]", "[A-Z][A-Z][0-9][A-Z]")
- Ptrn2 = "[0-9]*" '"[0-9][A-Z][A-Z]"
- On Error Resume Next
- For i = 0 To UBound(x)
- w = x(i)
- For j = LBound(Ptrn1) To UBound(Ptrn1)
- If Len(w) Then
- If w Like Ptrn1(j) And x(i + 1) Like Ptrn2 Then
- If Err.Number <> 0 Then
- Err.Clear
- If w Like Ptrn1(j) & Ptrn2 Then
- POSTCODE = w: Exit Function
- End If
- Else
- POSTCODE = w & Space(1) & x(i + 1)
- Exit Function
- End If
- ElseIf w Like Ptrn1(j) Then
- POSTCODE = w: Exit Function
- End If
- End If
- Next
- Next
- End Function
Ptrn1 is an Array. I don't understand this part. It has several options separated by comma. Does that mean that the text should have one of this combinations?
Why does he use Array() and why doesn't he use that bij Ptrn2.
Can someone give me some answers to this, so i learn from it?
Thanks,
HenkH