Postcode validation VBA

Mr.Daines

Board Regular
Joined
May 31, 2011
Messages
106
Hello all,

I have created a postcode validation script for my userform but it always throws up the message saying Invalid postcode regardless of what postcode i enter [valid or not].

Can anyone see where i have gone wrong?

Code:
' Validates the postcode entered - Format Check
Private Function validPostcode(postCode) ' Format check
Dim valid As Boolean
Dim first As String
Dim number As String
Dim last As String
Dim poCode As String
Dim space As String
Dim i As String
Dim cha As String
' Splits the postcode into sections.
poCode = tbpostcode.Text
first = Left(poCode, 1)
number = Mid(poCode, 2, 2)
space = Mid(poCode, 4, 1)
last = Right(poCode, 3)
valid = True
' If there is data
If Not poCode = "" Then
valid = True
End If
' Checking the value of first digit
If Not frontLetter(first) Then
valid = False
End If
' Checking the second and third characters
If Not isNumeric(number) <> 7 Then
valid = False
End If
' Checks to see if there is a space
' After the fourth digit
If Not space = "" Then
valid = False
End If
' Checks that last three digits
If Not lastThree(last) Then
valid = False
End If
validPostcode = valid
' Display error if invalid postcode
If valid = False Then
MsgBox "Please enter a valid postcode", vbOKOnly
End If
End Function
 
' Function to validate the front letter of the postcode
Private Function frontLetter(s As String)
Dim ing As Integer
Dim ch As String
Dim valid As Boolean
valid = True
For ing = 1 To Len(s)
ch = Mid(s, ing, 1)
If ch < "A" Or ch > "Z" Then
valid = False
End If
Next ing
frontLetter = valid
End Function
 
' Function to validate numeric value of
' The postcode
Private Function isNumeric(s As String)
Dim ing As Integer
Dim ch As String
Dim valid As Boolean
valid = True
For ing = 1 To Len(s)
ch = Mid(s, ing, 1)
If ch < "0" Or ch > "9" Then
valid = False
End If
Next ing
isNumeric = valid
End Function
 
' Validate last three characters of postcode
Private Function lastThree(s As String)
Dim ing As Integer
Dim ch As String
Dim valid As Boolean
valid = True
For ing = 1 To Len(s)
ch = Mid(s, ing, 1)
If ch < "A" Or ch > "Z" Then
valid = False
End If
Next ing
lastThree = valid
End Function

Any help much appreciated.
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
What is this supposed to mean:
Code:
If Not isNumeric(number) <> 7 Then
??
 
Upvote 0
isNumeric returns a Boolean (both your version and the native VBA function), Not then negates that, and you then compare it to 7 because...?
 
Upvote 0
isNumeric returns a Boolean (both your version and the native VBA function), Not then negates that, and you then compare it to 7 because...?

That is a very good question..

It was very late at night when i wrote the function and i do not recall why i have put it in.

Although even if i remove that part it still says that every postcode i enter is incorrect.

Really sorry if i do not know what certain parts of my code do as i have taken parts from the internet, i am a newbie to VBA.

I appreciate your patience.
 
Upvote 0
Are you entering the postcodes in upper case? Your code assumes so.
 
Upvote 0
Are you entering the postcodes in upper case? Your code assumes so.

Yes, the postcode example i am using is SK4 2HD.

I would like to include a function that corrects lower case to upper case instead of throwing "invalid postcode"
 
Upvote 0
To give you an idea (I'm ignoring the fact that you pass the postcode to the function but never use it - instead the function refers to a textbox directly):

Input: SK4 2HD

first = Left(poCode, 1) - this is "S"
number = Mid(poCode, 2, 2) - this is "K4" so fails isNumeric test, but isNumeric returns True anyway!
space = Mid(poCode, 4, 1) - this is " "
last = Right(poCode, 3) - this is "2HD"

you then test:
Code:
If Not space = "" Then
which will always fail since space is " " and not "".
 
Upvote 0

Forum statistics

Threads
1,221,566
Messages
6,160,525
Members
451,655
Latest member
rugubara

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top