Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to identify between range of postal codes

I'm pretty new to vba and I'm trying to figure out how to identify a postal code that falls within a range where one character changes.

For example, I want to see if a postal code falls between 2G8 and 2P8.

In other words, the range of what I'm looking for would be 2_8 where the underscore can be G to P, but I don't know how to identify that in code.

I'd really appreciate some help to figure out how to approach this problem. Thanks!

like image 388
user960358 Avatar asked Apr 20 '12 01:04

user960358


People also ask

Is there a space between postcode?

Postcodes should always be in BLOCK CAPITALS as the last line of an address. Do not underline the postcode or use any punctuation. Leave a clear space of one character between the two parts of the postcode and do not join the characters in any way.

What is the range of US zip codes?

The current postal codes in the United States range from 00001 – 99950.

What is the difference between a ZIP Code and a postal code?

Unlike postal codes, which can contain a mixture of numbers and letters, zip codes only contain a combination of numeric values. Regardless of their name or structure, most countries leverage a coding system—whether a zip code, postal code, or other structure—to ensure the rapid and efficient delivery of mail to users.


2 Answers

LIKE avoids repeated string testing

1. Simple Test

Function ValidString(strIn As String) As Boolean
ValidString = strIn Like "2[G-P]8"
End Function

2. Case Insensitive version

Function ValidString(strIn As String) As Boolean
ValidString = LCase$(strIn) Like LCase$("2[G-P]8")
End Function
like image 100
brettdj Avatar answered Oct 17 '22 06:10

brettdj


You can make use of the VBA Mid function to pull out each character and validate it according to your business rules. Here's an example of how it might look:

Function ValidatePostalCode(code As String)
    ValidatePostalCode = _
        (Mid(code, 1, 1) = "2") And _
        (Mid(code, 2, 1) >= "G" And Mid(code, 2, 1) <= "P") And _
        (Mid(code, 3, 1) = "8")
End Function

This Function will return a value of True or False indicating whether the input is valid.

like image 21
mellamokb Avatar answered Oct 17 '22 05:10

mellamokb