vbscript help change lower case to upper

trafficzombie

Board Regular
Joined
Sep 9, 2011
Messages
63
Hi

I have the following code to check for valid post code formats and it works perfectly

But I need it to change any letter that is in lower case to upper...any help please?

Thanks

Public Function IsUKPostCode(strInput As String)
'Uses a regular expression to validate the format of a postcode.
'May require WindowsScripting 5.6 - downloadable from microsoft.com
Dim RgExp As Variant
'Create the regular expression object
Set RgExp = CreateObject("VBScript.RegExp")
'Clear the function value
IsUKPostCode = ""
'Check we have value to test
If strInput = "" Then
IsUKPostCode = "Not Supplied"
Exit Function
End If
strInput = UCase(strInput)
'This is the ridiculously complex expression that validates the postcode
RgExp.Pattern = "(?:(?:A[BL]|B[ABDHLNRST]?|" _
& "C[ABFHMORTVW]|D[ADEGHLNTY]|E[CHNX]?|F[KY]|G[LUY]?|" _
& "H[ADGPRSUX]|I[GMPV]|JE|K[ATWY]|L[ADELNSU]?|M[EKL]?|" _
& "N[EGNPRW]?|O[LX]|P[AEHLOR]|R[GHM]|S[AEGKLMNOPRSTWY]?|" _
& "T[ADFNQRSW]|UB|W[ACDFNRSV]?|YO|ZE)" _
& "\d(?:\d|[A-Z])? \d[A-Z]{2})"
'Does the fed in string match the pattern?
If RgExp.test(strInput) = True And Len(strInput) <= 8 Then
IsUKPostCode = "Valid"
Else
'------------------------------
'Try to make a correct postcode
'------------------------------
'Despace & uppercase
strInput = UCase(Replace(strInput, " ", ""))
'Clean out any redundant characters - whilst most of these don't make sense
'I've seen them all in postcodes before!
strInput = Replace(strInput, "_", "")
strInput = Replace(strInput, ",", "")
strInput = Replace(strInput, "+", "")
strInput = Replace(strInput, "-", "")
strInput = Replace(strInput, ":", "")
strInput = Replace(strInput, "=", "")
strInput = Replace(strInput, "/", "")
strInput = Replace(strInput, "*", "")
strInput = Replace(strInput, "?", "")
strInput = Replace(strInput, ".", "")
'---------------------------------------------------------------------------
'Check the string length again to make sure we've not got a "???" type entry
'---------------------------------------------------------------------------
If Len(strInput) = 0 Then
IsUKPostCode = "Not Supplied"
Exit Function
ElseIf IsNumeric(strInput) Then
IsUKPostCode = "All Numbers"
Exit Function
ElseIf Len(strInput) < 5 Then
IsUKPostCode = "Too Short"
Exit Function
ElseIf Len(strInput) > 8 Then
IsUKPostCode = "Too Long"
Exit Function
End If
'Check for and correct substituted O (alpha) for 0 (numeric) at position len - 2
If Mid(strInput, Len(strInput) - 2, 1) = "O" Then strInput = _
Left(strInput, Len(strInput) - 3) & "0" & Right(strInput, 2)
'Check for and correct substituted 0 (numeric) for O (alpha) at position 1 or 2
If Mid(strInput, 2, 1) = "0" Then strInput = _
Left(strInput, 1) & "O" & Right(strInput, Len(strInput) - 2)
If Left(strInput, 1) = "0" Then strInput = _
"O" & Right(strInput, Len(strInput) - 1)
'Check for and correct substituted lowercase l for 1 at position len - 2
If Mid(strInput, Len(strInput) - 2, 1) = "l" Then strInput = _
Left(strInput, Len(strInput) - 3) & "1" & Right(strInput, 2)
'Check for and correct substituted lowercase l for 1 at position 3
If Mid(strInput, 3, 1) = "l" Then strInput = _
Left(strInput, 2) & "1" & Right(strInput, Len(strInput) - 3)
'Check for and correct substituted S for 5 at position len - 3
If Mid(strInput, Len(strInput) - 3, 1) = "S" Then strInput = _
Left(strInput, Len(strInput) - 3) & "5" & Right(strInput, 2)
'Two possible lengths for a valid UK postcode
Select Case Len(strInput)
Case 6
If RgExp.test(Left(strInput, 3) & " " & Right(strInput, 3)) = True Then
'Format should be ?## #?? or ??# #??
IsUKPostCode = Left(strInput, 3) & " " & Right(strInput, 3)
Else
IsUKPostCode = "Invalid"
End If
Case 7
If RgExp.test(Left(strInput, 4) & " " & Right(strInput, 3)) = True Then
'Format is ??## #?? or ?#?# #??
IsUKPostCode = Left(strInput, 4) & " " & Right(strInput, 3)
Else
IsUKPostCode = "Invalid"
End If
Case Else
IsUKPostCode = "Invalid"
End Select
End If
End Function
 
Last edited:
Your requirements clash:
this: * change all lower case to upper
indicates that you want a postcode returned

but this:
* If the post code format is valid (always has the last part as 3 digits, and the first part as either 2, 3 or 4, it shows as VALID (it currently does this)
indicates that you want the word VALID returned.

You can't have both things happening.

Ah, I thought this may be the case, can you think of any other options?

Lower case post codes, and the 5 digit codes (M1 3RT) with another symbol at the end (M1 3RT* - which says is VALID) are the glitch at the moment
 
Last edited:
Upvote 0

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Ah, I thought this may be the case, can you think of any other options?

Lower case post codes, and the 5 digit codes (M1 3RT) with another symbol at the end (M1 3RT* - which says is VALID) are the glitch at the moment

Use your function to check the validity, and have another cell to convert to uppercase, simply using the UPPER worksheet function. As for the glitch, I'll see if I can alter the function on Monday.
 
Upvote 0
Why don't you put another row in the "replaces" section of code ... like this:
Code:
strInput = Replace(strInput, "*", "")
( it's where you are getting rid of other spurious characters with similar code )?
 
Upvote 0
Why don't you put another row in the "replaces" section of code ... like this:
Code:
strInput = Replace(strInput, "*", "")
( it's where you are getting rid of other spurious characters with similar code )?

I can't believe I missed that, thank you

But it still not changing the 5 digit codes

for example if I have one that says M1 3RT.

It shows this as VALID
 
Upvote 0
...
Any other format of post code (SK15 5RT*) it changes it to SK15 5RT and removed the symbol but on the 5 digit ones it doesn't

You said that the function only returns Valid or Invalid. What do you mean by the bold bit of the quote?
 
Upvote 0
You said that the function only returns Valid or Invalid. What do you mean by the bold bit of the quote?

If a postcode has something that shouldn't be there... and sumbol like . * & % ^. Or has no spaces in between the two sections or more than one space...it chanegs the post code to the correct format for example:

SK15 3RT. changes to SK15 3RT
SK153RT changes to SK15 3RT
SK15 3RT changes to SK15 3RT

If however is says SK15 3RT which is correct it says VALID.

Again it is says SK15 3R which is not valid is says INVALID

Hope that explains well
 
Upvote 0
You say:

SK15 3RT. changes to SK15 3RT
( which is valid ), but also:

if I have one that says M1 3RT.

It shows this as VALID
which you say is wrong?!?!?

So, no, I don't understand your explanations.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,191
Members
452,616
Latest member
intern444

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