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!
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.
The current postal codes in the United States range from 00001 – 99950.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With