View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.misc
dm dm is offline
external usenet poster
 
Posts: 23
Default Excel and zip codes

Thanks, Dave. dm

"Dave O" wrote:

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