VBA Select Case – Check/Match and Format data in worksheet (cell)

wcm69

Board Regular
Joined
Dec 25, 2016
Messages
112
I’m hoping someone will have the patience and be kind enough to help me with a small VBA Select Case issue I’m having.

I have a spreadsheet where I will be asking the user to enter their (UK) address including their Postcode / Zip code.

I want to use a Case Statement to check and validate the post code entered by the user. There are 6 (postcode) format types (between 5 and 7 digits long) that I need to be checked and compared - then IF the correct format is entered the postcode (entry) is capitalised and set out in one of the below postcode format types:

A9 9AA,
A99 9AA,
AA9 9AA,
AA99 9AA,
A9A 9AA,
AA9A 9AA,

The code I’m using has been modified from an existing one in my worksheet 1 and it works fine – but for only 1 “Postcode format type”.

I’m using the “LEN” function to first check the inputted data matches the length of one of the (postcode) format types. I’m then using, “IF and LIKE” to check if the APLHA and Numeric entry also matches one of the 6 formats types. All criteria being met then the UCase code is executed, or else the appropriate Msgbox should pop up.

The unfortunate problem is having limited VBA knowledge I cannot work out how to include the other 5 (postcode) formats in my code to be tested. I’m using the following code for Cell: D11:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim PostCode As String

Target.Cells(11, 4) = PostCode
PostCode = Range("D11").Value
If Len(PostCode) = 0 Then Exit Sub

'remove all spaces for common starting point

PostCode = Trim(Replace(PostCode, " ", ""))
Select Case Len(PostCode)

Case Is < 5

MsgBox "The Postcode entered does not appear to be " & _
"a valid UK Postcode." & _
Chr(13) & "Please check you've entered the correct numbers. You do not need to enter the dashes (-).", vbApplicationModal + _
vbInformation + vbOKOnly, "Invalid Postcode"

Cancel = True
With Range("D11")
End With
On Error GoTo 0
Exit Sub

Case Is = 5

PostCode = UCase(PostCode)
If PostCode Like "[A-Z][0-9][0-9][A-Z][A-Z]" Then
PostCode = Left(PostCode, 2) & " " & Mid(PostCode, 3, 2) & "" & _
Mid(PostCode, 5, 2)
Else
MsgBox "The Postcode entered does not appear to be " & _
"a valid Postcode. " & _
Chr(13) & "Please check you've entered the correct numbers. You do not need to enter the spaces.", vbApplicationModal + _
vbInformation + vbOKOnly, "Invalid Postcode"

Cancel = True
With Range("D11")
End With
Exit Sub
End If

Case Is > 7

MsgBox "The Postode entered does not appear to be " & _
"a valid Postcode. You have entered too many digits " & _
Chr(13) & "Please check you've entered the correct numbers. You do not need to enter the dashes (-).", vbApplicationModal + _
vbInformation + vbOKOnly, "Invalid Postcode"

Cancel = True
With Range("D11")
End With
Exit Sub

End Select

Range("D11").Value = PostCode

End Sub

I’ve tried looking online and at tutorials for a fix, so after a number of attempts with no success this may be my last hope. As usual any help is always appreciated.

Many thanks in advance :) .
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.

Forum statistics

Threads
1,214,812
Messages
6,121,704
Members
449,048
Latest member
81jamesacct

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