Hi Mike,
This should remove doubled hyphens in place (macro solution),
as well as prefixed and suffixed hyphens for TEXT value
within a selected range. Will ignore numbers like -7.
TRIM which removes spaces will be performed on all text cells
that contain at least one hyphen within the selected area.
Prior to XL97 xlCellTypeConstants might be xlConstants
Option Explicit
Sub REMXHYPS()
'David McRitchie 2000-02-27 excel.programming
'hyphen removals of dups, prefixed, suffixed, and trim results
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual 'pre XL97 xlManual
Dim newstr As String
Dim oldstr As String
Dim lst As String
Dim ix As Integer
Dim x As String
Dim cell As Range
For Each cell In Selection.SpecialCells(xlCellTypeConstants, 2)
'above limits to constants which are TEXT
If InStr(1, cell.Value, "-") Then
lst = "-"
newstr = ""
oldstr = Trim(cell.Value)
For ix = 1 To Len(oldstr)
x = Mid(oldstr, ix, 1)
If x = "-" Then
If lst <> "-" Then newstr = newstr & x
Else
newstr = newstr & x
End If
lst = Mid(oldstr, ix, 1)
Next
If Right(newstr, 1) = "-" Then newstr = Left(newstr, Len(newstr) - 1)
cell.Value = Trim(newstr)
End If
Next
Application.Calculation = xlCalculationAutomatic 'pre XL97 xlAutomatic
Application.ScreenUpdating = True
End Sub
HTH,
David McRitchie, Microsoft MVP - Excel
My Excel Pages: http://members.aol.com/dmcritchie/excel/excel.htm
tim williams <saxif
...@pacbell.net> wrote in message
news:F7mu4.453$Fl3.13264@news.pacbell.net...
> Function formatMe(stuff As String)
> stuff = Trim(stuff)
> 'remove trailing -'s
> Do While Right(stuff, 1) = "-"
> stuff = Left(stuff, Len(stuff) - 1)
> Loop
> strTemp = ""
> bFlag = False
> For n = 1 To Len(stuff)
> If Mid(stuff, n, 1) = "-" And bFlag = False Then
> strTemp = strTemp & Mid(stuff, n, 1)
> bFlag = True
> ElseIf Mid(stuff, n, 1) <> "-" Then
> strTemp = strTemp & Mid(stuff, n, 1)
> bFlag = False
> End If
> Next n
> formatMe = strTemp
> End Function
> Mike Van Sydenborgh <poin...@mergetel.com> wrote in message
> news:SJku4.22993$bz2.4285542@tw12.nn.bcandid.com...
> > A file that I import into Excel, leaves hypens around the data I'm
> > interested in.
> > Some examples are:
> > DCK-43V---
> > FS--4824--G3---
> > SWS---7224--S
> > I want one hyphen between letters like SWS-7224-S and the hypens beyond
> > letters to be removed like this: FS-4824-G3
> > The final product should appear as: DCK-43V, FS-4824-G3,
> > SWS-7224-S