PDA

View Full Version : Delete the dash from first and end of cell



parscon
03-28-2013, 09:51 AM
I have a VBA code that delete Dash - from end of cell now I want also delete the dash of first of my data


like :

-12121121
When run VBA code delete the first - .

also if have -12121121- when run code delete dash and will be 12121121





Sub Trim()
For Each c In Worksheets("Sheet1").Range("A1:A3").Cells
If Right(c.Value, 1) = "-" Then
c.Value = Mid(c.Value, 1, Len(c) - 1)
End If
Next
End Sub

enrand22
03-28-2013, 10:30 AM
you can fusion your code with this one

Sub Parscon()

Dim Counter As Integer
Dim MyString As String
Dim c As String
Dim data1 As String

MyString = "-Automa-teExc-el" 'define string, here should be the line to unify your code

For Counter = 1 To Len(MyString)

c = Mid(MyString, Counter, 1)

If c <> "-" Then
data1 = data1 & c 'loop to reintegrate one by one the characters exlcluiding -
End If
Next

MsgBox data1

End Sub

snb
03-28-2013, 02:58 PM
or

sub M_snb()
Sheets("Sheet1").Range("A1:A3").replace "-",""
end sub