Results 1 to 37 of 37

Format Cells for UK Postcode

Threaded View

  1. #34
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Format Cells for UK Postcode

    You are correct TM it also applies to

    BIRMINGHAM, LONDON, GLASGOW, LIVERPOOL, MANCHESTER, SHEFFIELD


    All of which have 1 letter & 1 digit codes

    It gets a bit more interesting as it grows
    Option Explicit
    
    Function FormatPostCode(Target)
        Dim strChk As String
    
        With WorksheetFunction
            strChk = .Trim(Target)
            strChk = .Substitute(strChk, " ", "")
        End With
    
        Target.Interior.ColorIndex = xlNone
        If IsNumeric(Left(strChk, 1)) Then
            FormatPostCode = Target
            Target.Interior.ColorIndex = 3
        Else
            Select Case Len(strChk)
                Case Is < 2
                    FormatPostCode = Target
                    Target.Interior.ColorIndex = 3
                Case 2, 3
                    If IsNumeric(Right(strChk, 1)) Then
                        FormatPostCode = UCase(WorksheetFunction.Trim(strChk))
                    Else
                        FormatPostCode = Target
                        Target.Interior.ColorIndex = 3
                    End If
                Case 4
                    If IsNumeric(Right(strChk, 2)) Then
                        If Not IsNumeric(Right(strChk, 3)) Then
                            FormatPostCode = UCase(WorksheetFunction.Trim(strChk))
                        Else
                            FormatPostCode = Target
                            Target.Interior.ColorIndex = 3
                        End If
                    End If
                Case Else
                    strChk = Left(strChk, Len(strChk) - 3) & " " & Right(strChk, 3)
                    FormatPostCode = UCase(WorksheetFunction.Trim(strChk))
            End Select
        End If
    End Function
    
    Sub CheckPostCodes()
        Dim LastRow As Long, RowNo As Long
    
        LastRow = Range("J" & Rows.Count).End(xlUp).Row
        For RowNo = 2 To LastRow
            If Range("K" & RowNo) = "United Kingdom" Then
                Range("J" & RowNo) = FormatPostCode(Range("J" & RowNo))
            End If
        Next
    End Sub
    This needs refining , and a little more error checking built into the function to make it bullet proof, but got to go for a while.

    Give the attached a try.
    Attached Files Attached Files
    Last edited by Marcol; 11-26-2010 at 03:29 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1