Where did all the replies come from!!
Try this in a standard module
Option Explicit
Function FormatPostCode(Target)
Target = WorksheetFunction.Substitute(Target, " ", "")
Target = Left(Target, Len(Target) - 3) & " " & Right(Target, 3)
FormatPostCode = Target
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 will "clean" any existing codes
If you then add this to the worksheet module
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim isect As Range
If Target.Cells.Count > 1 Or Target.HasFormula Then Exit Sub
On Error GoTo ResetApplication
Application.EnableEvents = False
Set isect = Intersect(Target, Range("J:J"))
If Not isect Is Nothing Then
If Target.Offset(0, 1) = "United Kingdom" Then FormatPostCode Target
End If
Set isect = Intersect(Target, Range("K:K"))
If Not isect Is Nothing Then
If Target = "United Kingdom" Then
Target.Offset(0, -1) = FormatPostCode(Target.Offset(0, -1))
End If
End If
ResetApplication:
Err.Clear
On Error GoTo 0
Application.EnableEvents = True
Set isect = Nothing
End Sub
This will check codes as you go.
Basically this is a VBa answer to TMShucks' formula
Hope this helps
Bookmarks