簡體   English   中英

如何使用excel vba在多行單元格中的最后一個斜杠后得到單詞

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

我在多行單元格中有很多數據。我想要一個多用途單元格中每行最后一個斜杠之后的單詞。我使用了此代碼Worksheets(“ Sheet1”)。Columns(“ A”)。替換內容:=“ * / “,Replacement:=”“,SearchOrder:= xlByColumns,MatchCase:= True但是它沒有用。我想要下圖的格式。請幫幫我。 多行單元格中最后一個斜杠后的單詞

PSI是Excel VBA的初學者

多行單元格中的行由CHAR(10)分隔。您可能希望基於該行進行拆分,然后對於每行,都需要INSTRREV函數- 請參見此處的示例

我相信以下代碼將完成您期望的工作:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM