I would do it like this - considerably less efficient than your code
for real postcodes, but much more tolerant of invalid postcodes.
As long as the first part of the Postcode is valid, the following two
functions will return (respectively) its alpha and integer components.
Most invalid codes will return the Null string and -1 (again
respectively). I think that I have handled all possible bad arguments.
I have gone for clarity rather than speed, but the Functions should
not be too inefficient. You can do the same sort of thing for the
second half of the postcode. Note that I am passing the PostCode as a
Variant. If you are _100%_ sure that you will always be passing a
string, you can save a bit of code and time by declaring it as such
and deleting the two lines with the '** comments.
Function AlphaPart(PostCode As Variant) As String
' Returns the alpha part of the first "block" of a UK Postcode
' a null string is the error return
Dim strTemp As String
Dim strReturn As String
Dim blDone As Boolean
AlphaPart = vbNullString
blDone = False
If IsNull(PostCode) Then Exit Function '**
If VarType(PostCode) <> vbString Then Exit Function '**
If Len(PostCode) < 2 Then Exit Function
' Deal with likely bad arguments
strTemp = UCase(PostCode) ' just in case it wasn't
strReturn = Mid$(strTemp, 1, 1)
Select Case strReturn
Case "A" To "Z"
' that's OK, continue
Case Else
Exit Function ' not a valid postcode
End Select
Select Case Mid$(strTemp, 2, 1)
Case "A" To "Z"
strReturn = strReturn & Mid$(strTemp, 2, 1)
Case "0" To "9"
blDone = True
Case Else
Exit Function ' not a valid postcode
End Select
If blDone Then
AlphaPart = strReturn
Else
Select Case Mid$(strTemp, 3, 1)
Case "0" To "9"
AlphaPart = strReturn
Case Else
' not a valid postcode
End Select
End If
End Function
Function NumPart(PostCode As Variant) As Integer
' Returns the numeric part of the first "block" of a UK Postcode as an
integer
' -1 is the error return
Dim strTemp As String
Dim intReturn As Integer
Dim blDone As Boolean
NumPart = -1
blDone = False
If IsNull(PostCode) Then Exit Function '**
If VarType(PostCode) <> vbString Then Exit Function '**
If Len(PostCode) < 2 Then Exit Function
strTemp = UCase(PostCode)
Select Case Mid$(strTemp, 1, 1)
Case "0" To "9"
Exit Function ' invalid postcode
Case "A" To "Z"
strTemp = Mid$(strTemp, 2)
Case Else
Exit Function ' invalid postcode
End Select
Select Case Mid$(strTemp, 1, 1)
Case "0" To "9"
' start of the numeric part
Case "A" To "Z"
strTemp = Mid$(strTemp, 2)
Case Else
Exit Function ' invalid postcode
End Select
' now we should be at the start of the numeric part
Select Case Mid$(strTemp, 1, 1)
Case "0" To "9"
intReturn = CInt(Mid$(strTemp, 1, 1))
strTemp = Mid$(strTemp, 2)
Case Else
Exit Function ' invalid postcode
End Select
' is there another digit?
Select Case Mid$(strTemp, 1, 1)
Case "0" To "9"
intReturn = intReturn * 10 + CInt(Mid$(strTemp, 1, 1))
Case Else
blDone = True
End Select
If blDone Then
NumPart = intReturn
Else
Select Case Mid$(strTemp, 2, 1)
Case "0" To "9" ' there shouldn't be another!
' not a valid postcode
Case Else
NumPart = intReturn
End Select
End If
End Function
End Function
Please respond to the Newsgroup, so that others may benefit from the exchange.
Peter R. Fletcher
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption