Format Cell for UK Postcode

andbee

New Member
Joined
Aug 26, 2014
Messages
4
Hello Folks,

I have been creating an enquiry form for use by our Sales Teams but I have problem in that I can not format a cell in the UK Postcode format.

The format would be 2 capital letters/1 or 2 numbers/space 1 or 2 numbers/2 capital letters.

Does anyone know if this can be done?

I have seen several VB Codes which will change the format after it has been entered, but I want to force the users to enter the Text in the correct format to begin with.

Thanks
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Can you post a couple of examples of what they should look like
 
Upvote 0
Assuming the user types the data into column "A"
Try this once the data is been inserted....it will do the whole column
Code:
Sub MM1() 'code by StephenR
Dim r As Long
For r = 1 To Cells(Rows.Count, "A").End(xlUp).Row
        With CreateObject("vbscript.regexp")
            .Global = True
            .Pattern = "[A-Z]{1,2}[0-9]{1,2}\s[0-9][A-Z]{2}"
            If Not .test(Cells(r, "A")) Then
                Cells(r, "A") = UCase(Left(Cells(r, "A"), Len(Cells(r, "A")) - 3) & " " & Right(Cells(r, "A"), 3))
           End If
       End With
Next r
End Sub
 
Upvote 0
Hi Michael,

Many thanks for this, but looking at this now I can see where I have possibly phrased my problem badly!

I have a single cell that I want to force users to enter the postcode in the correct format. I don;t have a column of data just a single cell. Is there no way of formatting that one cell without having to resort to coding?

Thanks for your help, it is much appreciated.

Andrew
 
Upvote 0
I don't believe so...I'm no Custom Format expert, but I believe with the chance of users inputting lowercase, propercase, extra spaces, etc....it would be too difficult to control
this will adjust cell A1 only, and automatically, once a user has input the data
Paste into the sheet module
Code:
Private Sub worksheet_change(ByVal target As Range)
 If Intersect(target, Range("A1")) Is Nothing Then Exit Sub
 With CreateObject("vbscript.regexp")
     .Global = True
     .Pattern = "[A-Z]{1,2}[0-9]{1,2}\s[0-9][A-Z]{2}"
     If Not .test(Cells(1, 1)) Then
         Cells(1, 1) = UCase(Left(Cells(1, 1), Len(Cells(1, 1)) - 3) & " " & Right(Cells(1, 1), 3))
    End If
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,087
Messages
6,123,050
Members
449,092
Latest member
ikke

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