简体   繁体   中英

how get the word after last slash in a multi lined cell using excel vba

I have a lot data in the mulitlined cell.I want the word after last slash in each line in a mutilined cell.I used this code Worksheets("Sheet1").Columns("A").Replace What:="*/", Replacement:="", SearchOrder:=xlByColumns, MatchCase:=True But it didn't worked.I want the format in the below image.Please help me out. 多行单元格中最后一个斜杠后的单词

PSI am a beginner in excel vba

多行单元格中的行由CHAR(10)分隔。您可能希望基于该行进行拆分,然后对于每行,都需要INSTRREV函数- 请参见此处的示例

I believe the following code will do what you expect it to:

Sub foo()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set you worksheet
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
'get the last row with data on column A

For i = 1 To LastRow 'loop from row 1 to last
    ValueList = Split(ws.Cells(i, 1).Value, vbLf) 'split next line values into array
    For x = LBound(ValueList) To UBound(ValueList) 'loop through array
        pos = InStrRev(ValueList(x), "/") 'get the position of the last /
        ValueList(x) = Right(ValueList(x), Len(ValueList(x)) - pos) 'remove everything before the last /
        ws.Cells(i, 2).Value = ws.Cells(i, 2).Value & vbLf & ValueList(x) 'pass the newly created values into Column B
    Next x
Next i
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