I have
Column A
Red-US
Blue-INT
Purple-INT
White-US-CA
Trying remove -us, int, ca, etc.
So it's just Red, Blue, Purple, etc.
Can't use Trim or Substitute formula because I want it to change directly in Column A (replace)
Thank you!
If the "-" is a consistent separator then it should be pretty simple.
Here are some commands you could use:
Edit: Added simple code
Sub textuptodash()
i = 1 'start on row 1
Do While Not IsEmpty(Cells(i, 1)) 'do until cell is empty
If Not InStr(1, Cells(i, 1), "-") = 0 Then 'if dash in cell
Cells(i, 1) = Left(Cells(i, 1), InStr(1, Cells(i, 1), "-") - 1) 'change cell contents
End If
i = i + 1 'increment row
Loop
End Sub
Try using Split as follows:
Sub MySplit()
Dim rngMyRange As Range, rngCell As Range
Dim strTemp() As String, strDel As String, strTest As String
Dim lngCnt As Long
Set rngMyRange = Range("A1:A4")
For Each rngCell In rngMyRange
' Split the test based on the delimiter
' Store entries in a vector of strings
strTemp = Split(rngCell.Text, "-")
' Reset cell value and intermmediate delimiter
rngCell.Value = vbNullString
strDel = vbNullString
' Scan all entries. store all of them but not the last -* part
For lngCnt = LBound(strTemp) To UBound(strTemp) - 1
rngCell = rngCell & strDel & strTemp(lngCnt)
' If we enter the loop again we will need to apend a "-"
strDel = "-"
Next lngCnt
Next rngCell
End Sub
Output:
Red
Blue
Purple
White-US
It is not entirely clear how you want the last entry to be split: assumed that you want to remove the last "-*" bit only. To keep the first part only, comment out the For lngCnt loop.
I hope this helps!