View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.misc
Dave O Dave O is offline
external usenet poster
 
Posts: 427
Default Excel and zip codes

Someone always comes up with a simple solution while I'm off figuring
out the complicated one! But this took me a few minutes so I'm going
to post it anyway.

Alternatively, highlight the appropriate range of cells and run this
macro. It formats each cell individually, based on the number of
characters in each cell.

Sub Zip_Format()
Dim rCell As Range
Dim Zip As Variant

For Each rCell In Selection.Cells
Zip = Trim(rCell.Value)
rCell.ClearContents
If Len(Zip) 5 Then
rCell.NumberFormat = "00000-0000"
Else
rCell.NumberFormat = "00000"
End If
rCell.Value = Zip
Next rCell

End Sub