1

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!

2 Answers 2

1

If the "-" is a consistent separator then it should be pretty simple.

Here are some commands you could use:

Strings and Manipulations

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
Sign up to request clarification or add additional context in comments.

Comments

0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.