如何使用excel vba在多行单元格中的最后一次斜线后获取单词

时间:2018-05-16 11:01:45

标签: excel-vba vba excel

我在mulitlined单元格中有很多数据。我希望在mutilined单元格的每一行中最后一次斜线后面的单词。我使用了这段代码工作表(" Sheet1")。列(" A")。替换内容:=" * /",替换:="",SearchOrder:= xlByColumns,MatchCase:= True 但它没有用。我想要下图中的格式。请帮帮我。 word after last slash in a multilined cell

P.S.我是excel vba的初学者

2 个答案:

答案 0 :(得分:2)

多行单元格中的行由CHAR(10)分隔 您可能希望基于此分割,然后对于每一行,您需要INSTRREV函数 - see examples here

答案 1 :(得分:1)

我相信以下代码会按照您的预期执行:

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