Consulting

Results 1 to 6 of 6

Thread: Postcode Macro - Amending

  1. #1
    VBAX Contributor
    Joined
    Aug 2007
    Posts
    165
    Location

    Postcode Macro - Amending

    Hi Not sure on how to do this.....

    But need to amend the following code so if the Postcode is 6 (letters and number, do the split 3 and 3 i.e. B21 9HX) if the post code is 7 (then always 4 and three)..........and keeping the Upper case format.....


    here is the original code

    [VBA]
    Sub PostcodeMacro()
    Dim ColNr As Long
    ColNr = InputBox("Enter Column Number to be checked")
    RowsA = Cells(Rows.Count, ColNr).End(xlUp).Row
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    For i = 1 To RowsA
    If Len(Cells(i, ColNr)) = 7 And InStr(1, Cells(i, ColNr), " ") <> 3 Then
    TempVal = Replace(Cells(i, ColNr), " ", "")
    Cells(i, ColNr) = UCase(Left(TempVal, 3)) & " " & UCase(Right(TempVal, 3))
    Else
    Cells(i, ColNr) = UCase(Cells(i, ColNr))
    End If
    Next i
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
    End Sub
    [/VBA]

    thanks

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Sub PostcodeMacro()
    Dim ColNr As Long
    Dim RowsA As Long
    Dim i As Long
    Dim TempVal

    ColNr = InputBox("Enter Column Number to be checked")
    RowsA = Cells(Rows.Count, ColNr).End(xlUp).Row

    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    For i = 1 To RowsA
    TempVal = Replace(Cells(i, ColNr).Value, " ", "")
    If Len(TempVal) = 6 Then
    Cells(i, ColNr).Value = UCase(Left(TempVal, 3) & " " & Right(TempVal, 3))
    ElseIf Len(TempVal) = 7 Then
    Cells(i, ColNr).Value = UCase(Left(TempVal, 4) & " " & Right(TempVal, 3))
    Else
    Cells(i, ColNr).Value = UCase(TempVal)
    End If
    Next i

    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Contributor
    Joined
    Aug 2007
    Posts
    165
    Location
    thanks for the feedback......ok i see for future referrence.

  4. #4
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    UK Postcodes always end in a three-character inbound code in the format number-letter-letter. Wouldn't it make more sense to always put the space three characters from the right regardless of length? That way, for example, W14AA would be caught as well.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I don't recall them now, but I believe that there are a few that don't follow this rule. There have been many threads (interminably boring) on postcode validation.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Last time I worked with them (about 6 years ago) there was the one exception - GIR OAA - that had existed from day one. They've probably all changed now
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

Posting Permissions

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