View Single Post
  #4   Report Post  
David McRitchie
 
Posts: n/a
Default

Hi Julian,
The following macro will format based on the text value of cell
so it should work on cells that have text, numbers, formatted
numbers with leading zeros. It will make no difference that
the first character is a letter. If it say commas or decimal
points they would just be characters. the only thing this does
is insert hyphens based on your pattern.

Sub Format_with_hyphens(Optional formatSTR As String)
'-- The parameter here can only be used if invoked from another macro
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim i As Long, j As Long, result As String
Dim cell As Range, newStr As String, tstLen As Long
On Error Resume Next 'In case no cells in selection
If formatSTR = "" Then
newStr = "Hxxx-xxx-xx-xxx-x"
Else
newStr = formatSTR
End If
tstLen = Len(Replace(newStr, "-", ""))
For Each cell In Intersect(Selection, ActiveSheet.UsedRange)
If Len(cell.Text) = tstLen Then
j = 1
result = ""
For i = 1 To Len(newStr)
If Mid(newStr, i, 1) = "-" Then
result = result & "-"
Else
result = result & Mid(cell.Text, j, 1)
j = j + 1
End If
Next i
cell.Value = "'" & result
End If
Next cell
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub

---
HTH,
David McRitchie, Microsoft MVP - Excel [site changed Nov. 2001]
My Excel Pages: http://www.mvps.org/dmcritchie/excel/excel.htm
Search Page: http://www.mvps.org/dmcritchie/excel/search.htm

"Julian Ganoudis" <Julian wrote in message
...
How do you format a drivers license number? For example:
Hxxx-xxx-xx-xxx-x, where x represents any number, but "H" represents any text?
Do you have to use a macro? If so, does anyone have the code for this
sequence?