Post Code UK Structure Verification

J

jacky Kenna

Hi

I am new to VBA (have gone through MS training 30 minutes). My
requirement is to check that UK post codes are in the correct format.


I want to create a macro for this and need help.

Below is what I have been doing. Notepad is used because copying from
Excel to word takes a very long time.

Quantity 50,000
I have been using a Heath Robinson Method as follows:
trim to remove any leading or training spaces then
Copy to Notepad
Copy to word
Remove all extraneous symbols ,./:;’\=-+ Etc
Replace double space with single space.
Copy to Notepad
Copy from Notepad to Excel
split using space as the delimiter into two columns 1st part 2nd Part
Take the first part
Count characters
Sort to bring to the top records with too many characters
Check high number of characters There should be no more than 4
Check the low number of characters as this could indicate a space was
in the wrong place
Manually check as this could indicate a space.
Copy to Notepad
Copy 1st part to word
Remove digits
Copy to Notepad
Copy from Notepad to Excel
Count characters
Sort to bring to the top records with too many characters
Check high number of characters There should be no more than 2 / 3
In the case of 3 manually look at the original Post Code and leave
Check the low number of characters as this could indicate a space was
in the wrong place
Manually check as this could indicate a space.
Revert to word1st part to word
Ctrl Z till the whole first part is visible.
Remove Letters Copy result to Notepad
Copy from Notepad to Excel
Trim
Count characters.
Sort
Manually check if characters are over 2 against initial code.
Repair those which are SW2X to SW2X
Using = & in excel create new column
Copy new column to itself using special paste and value to remove
formula.
ETC

Repeat the process on the 2ns part with slightly different criteria but
basically the same.

System prone to give bad results because so many manual actions leave
bits out etc.


Here are the post code variations:
UK Structure
• A1 2BC
• D34 5EF
• GH6 7IJ
• KL8M 9NO
I want to make a macro which does this for me.

I have located a java solution which is almost correct but not quite

The total length must be 6,7, or 8 characters, a gap (space character)
must be included
The inward code, the part to the right of the gap, must always be 3
characters
The first character of the inward code must be numeric
The second and third characters of the inward code must be alpha
The outward code, the part to the left of the gap, can be 2,3, or 4
characters
The first character of the outward code must be alpha
If the outward code is 3 characters then the last character must be
numeric (code not written).
If the outward code is 4 characters then the last character must be
alpha (code not written).

<SCRIPT LANGUAGE="JavaScript">

<!-- Begin
function postit(){ //check postcode format is valid
test = document.details.pcode.value; size = test.length
test = test.toUpperCase(); //Change to uppercase
while (test.slice(0,1) == " ") //Strip leading spaces
{test = test.substr(1,size-1);size = test.length
}
while(test.slice(size-1,size)== " ") //Strip trailing spaces
{test = test.substr(0,size-1);size = test.length
}
document.details.pcode.value = test; //write back to form field
if (size < 6 || size > 8){ //Code length rule
alert(test + " is not a valid postcode - wrong length");
document.details.pcode.focus();
return false;
}
if (!(isNaN(test.charAt(0)))){ //leftmost character must be alpha
character rule
alert(test + " is not a valid postcode - cannot start with a
number");
document.details.pcode.focus();
return false;
}
if (isNaN(test.charAt(size-3))){ //first character of inward code must
be numeric rule
alert(test + " is not a valid postcode - alpha character in wrong
position");
document.details.pcode.focus();
return false;
}
if (!(isNaN(test.charAt(size-2)))){ //second character of inward code
must be alpha rule
alert(test + " is not a valid postcode - number in wrong
position");
document.details.pcode.focus();
return false;
}
if (!(isNaN(test.charAt(size-1)))){ //third character of inward code
must be alpha rule
alert(test + " is not a valid postcode - number in wrong
position");
document.details.pcode.focus();
return false;
}
if (!(test.charAt(size-4) == " ")){//space in position length-3 rule
alert(test + " is not a valid postcode - no space or space in wrong
position");
document.details.pcode.focus();
return false;
}
count1 = test.indexOf(" ");count2 = test.lastIndexOf(" ");
if (count1 != count2){//only one space rule
alert(test + " is not a valid postcode - only one space allowed");
document.details.pcode.focus();
return false;
}
alert("Postcode Format OK");
return true;
}
// End -->
</script>

Am I asking too much particularly as I am a new member and a newbie
generally

All the Best
Jacky Kenna
 
G

Guest

Jacky,
Try this: "MyTest" calls the validation macro and passes a Post
code as a parameter. It only checks if there is (at least) one blank between
the two code elements; if you specifically want to check for one only that
can be done.

(FYI, I am UK-based).

HTH

Sub MyTest()
ValidatePostCode "WX2 1BA"
End Sub


Sub ValidatePostCode(Byval PostCode as String)
'
' Validate UK Post Code
'
Dim v As Variant
Dim Outer As String, Inner As String
Dim invalid As Boolean

v = Split(PostCode, " ")
If UBound(v) = 0 Then
MsgBox "Post code " & PostCode & " does not contain a blank"
Exit Sub
End If

Outer = UCase(Trim(v(0)))
Inner = UCase(Trim(v(1)))
'
' Validate inner code .....
'
invalid = False
If Len(Inner) <> 3 Then
invalid = True
Else
If Left(Inner, 1) Like "[0-9]" Then
If Mid(Inner, 2, 1) Like "[A-Z]" And Mid(Inner, 3, 1) Like "[A-Z]" Then
Else
invalid = True
End If
Else
invalid = True
End If
End If
If invalid Then
MsgBox "Inner code " & Inner & " is invalid"
Exit Sub
End If
'
' Validate Outer code .....
'
invalid = False
Select Case Len(Outer)

Case Is = 2
If Left(Outer, 1) Like "[A-Z]" And Right(Outer, 1) Like "[0-9]" Then
Else
invalid = True
End If
Case Is = 3
If Left(Outer, 1) Like "[A-Z]" And Right(Outer, 1) Like "[0-9]" _
And (Mid(Outer, 2, 1) Like "[A-Z]" Or Mid(Outer, 2, 1) Like "[0-9]") Then
Else
invalid = True
End If
Case Is = 4
If Left(Outer, 1) Like "[A-Z]" And Right(Outer, 1) Like "[A-Z]" _
And (Mid(Outer, 2, 1) Like "[A-Z]" Or Mid(Outer, 2, 1) Like "[0-9]") _
And (Mid(Outer, 3, 1) Like "[A-Z]" Or Mid(Outer, 3, 1) Like "[0-9]") Then
Else
invalid = True
End If
Case Else
invalid = True
End Select

If invalid Then
MsgBox "Outer code " & Outer & " is invalid"
Exit Sub
End If

MsgBox "Post Code " & PostCode & " is valid"

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top