Thread: Simple SSN UDF
View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Rick Rothstein \(MVP - VB\) Rick Rothstein \(MVP - VB\) is offline
external usenet poster
 
Posts: 2,202
Default Simple SSN UDF

Hello. I am struggling with my VBA knowledge. I would like to convert
the
following formula into a UDF (=ssn(cell)), but I do not know what the code
will look like. Can anyone help?

Formula:
=text(cell, "000-00-0000")


Place this code in a Module (Insert/Module from the VBE menu)...

Function SSN(ByVal ValueIn As Variant) As String
Dim Temp As String
If TypeOf ValueIn Is Range Then
Temp = ValueIn.Value
Else
Temp = ValueIn
End If
Temp = Replace(Temp, "-", "")
Application.EnableEvents = False
If Temp Like "#########" Then
SSN = Format$(Temp, "@@@-@@-@@@@")
Else
' Not sure what you want to do
' if the value passed in is not
' a valid as a SSN "shape", so
' I am doing nothing here which
' means the original value will
' be returned unaltered
SSN = Temp
End If
Application.EnableEvents = True
End Function


Rick