I have data which has a post code field. Some of the post codes do not have a space where they should ie. AB123CD instead of AB12 3CD. Is there code which I could run to update this to the correct format!
Thanks in advance!
Yes, normally but my post code, as an example, is G46 and is only 6 digits in length. Those in Aberdeen can be eight digits.
The problem is further hampered by the fact that some post codes can be five characters long. (i.e. G1 25A)
I suppose you will need, then, to use the Right() function to get the last three characters so that you can begin with the Sector.
Then, knowing the Sector, you can get the Area and District by using the InStr() function to find at what position the Sector begins. Knowing this position, you can use the Left() function to get the Area and District.
i.e.
Code:
Public Function SplitCode(strPostal As String, booPart As Boolean) As String
Dim intPosition As Integer
Dim strCode As String
strCode = Right(strPostal, 3)
If booPart Then
SplitCode = Left(strPostal, InStr(1, strPostal, strCode) - 1)
Else
SplitCode = strCode
End If
End Function
To get the Area and District call the function like this:
Public Function SplitCode(strPostal As String, booPart As Boolean) As String
SplitCode = Right(strPostal, 3)
If booPart Then
SplitCode = Left(strPostal, InStr(1, strPostal, SplitCode) - 1)
End If
End Function