Hi ...,
Is everything text to begin with, or are some already numbers with
formatted dashes. From you sample will assume everything is
text so any digits/numbers extracted will be text and can have leading
zeros and your dashes.
You will need programming to accomplish this, either a subroutine,
or a user defined function (UDF). Instructions to install/use a macro or UDF
http://www.mvps.org/dmcritchie/excel/getstarted.htm
Warning this will extract digits and dashes no matter where they occur
so "Model 302, catalog 32-0005, twin-housings" will produce
"30232-005-" based on what you asked for, since not additional criteria is provided.
Function ExtractDigits_andDashes(aa As String) As String
ExtractDigits_andDashes = "" '2003-10-18 dmcritchie, misc
Dim i As Long
For i = 1 To Len(aa)
If Mid(aa, i, 1) <= "9" And Mid(aa, i, 1) >= "0" _
Or Mid(aa, i, 1) = "-" Then
ExtractDigits_andDashes = ExtractDigits_andDashes & Mid(aa, i, 1)
End If
Next i
End Function
usage:
=ExtractDigits_andDashes(A1)
=personal.xls!ExtractDigits_andDashes(A1)
------ alternative solution ----
Subroutine to make the changes where the content resides:
To remove the parts you don't want from where they sit, you would use a
subroutine, which you can invoke from Alt+F8 after installing. Will
assume that everything is text to start with to greatly speed up execution,
SpecialCells additionally always restricts scope to the used range.
Warning: if you have only digits in a cell and is a number rather than
text the cell will not be processed, but shouldn't make a difference,
except for presentation (alignment), and for sorting.
Sub LeaveDigits_andDashes()
Dim i As Long '2003-10-18 D.McRitchie, misc
Dim aa As String, bb As String, cell As Range
If Intersect(Selection, Selection.SpecialCells(xlConstants, _
xlTextValues)) Is Nothing Then Exit Sub
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual 'pre XL97 xlManual
For Each cell In Intersect(Selection, _
Selection.SpecialCells(xlConstants, xlTextValues))
aa = cell.Text
bb = ""
For i = 1 To Len(aa)
If Mid(aa, i, 1) <= "9" And Mid(aa, i, 1) >= "0" _
Or Mid(aa, i, 1) = "-" Then
bb = bb & Mid(aa, i, 1)
End If
Next i
cell.Value = "'" & bb
Next cell
Application.Calculation = xlCalculationAutomatic 'pre XL97 xlAutomatic
Application.ScreenUpdating = True
End Sub
Usage: Select a column where data is to be changed
Alt+F8, select and run the macro
---
BTW, Your question is answered in the newsgroups, not in ExcelTips
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