简体   繁体   中英

Remove or add text after a specific character in Excel

I have lot of data in my excel sheet , i want to remove all text after Last (-) character . Here is sample data like in my sheet

"This-is-car-44"
"This-is-my-school-ok"

I want look like this

 "This-is-car"
 "This-is-my-school"

i want to remove all text after lats - , so is their any formula to do this. and one thing more if possible can i do like this in excel

 "This-is-car-44"
    "This-is-my-school-ok"

to look like this

  "This-is-car/"
    "This-is-my-school/"

i mean after last - remove all text and add this / in end . thanks.

If you are OK with excel formulas, please try this,

=SUBSTITUTE(A1,"-"&TRIM(RIGHT(SUBSTITUTE(A1,"-",REPT(" ",LEN(A1))),LEN(A1))),"/")

在此输入图像描述

You can do something like this.

Sub RemoveLastStingPart()
Dim rng As Range
Dim intLastRow As Long
Dim strTemp As String
Dim aryTemp() As String




With ActiveSheet

    intLastRow = .UsedRange.Rows.Count
    Set rng = .Range(Cells(1, 1), Cells(intLastRow, 1))

    For Each cell In rng
        strTemp = cell.Value
        aryTemp = Split(strTemp, "-")
        strTemp = ""
        For i = 0 To UBound(aryTemp) - 1
            strTemp = strTemp & aryTemp(i) & "-"
        Next i
        strTemp = Left(strTemp, Len(strTemp) - 1)
        cell.Offset(0, 1).Value = strTemp
    Next cell


End With

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM