[英]Split cell at last space excel VBA
我試圖在最后一個空格處拆分單元格內的值。 例如,我希望Abbey Road 4
拆分為Abbey Road
和4
我需要用 VBA 來完成這個,而不是一個公式,因為我需要在之后刪除原始列。
查看 INSTRREV 以查找字符串中最后一次出現的字符串。 https://msdn.microsoft.com/en-us/library/hsxyczeb(v=vs.84).aspx
Sub Test()
SplitText ThisWorkbook.Worksheets("Sheet1").Range("A1:A11")
End Sub
Sub SplitText(SrcRange As Range)
Dim rCell As Range
For Each rCell In SrcRange.Cells
rCell.Offset(, 1) = Left(rCell, InStrRev(rCell, " ") - 1)
rCell.Offset(, 2) = Mid(rCell, InStrRev(rCell, " ") + 1)
Next rCell
End Sub
這是 Darren Bartrup-Cook 答案的一個變體......
假設單元格A2已文本Some text with spaces
在里面,下面就會把Some text with
在C2和spaces
在D2
'Get text from A2
theText = Cells(2, 1).Value
'Get text to the left of last space
newTextLeft = Left(theText, InStrRev(theText, " ") - 1)
Cells(2, 3).Value = newTextLeft
'Get text to right of last space
newTextRight = Mid(theText, InStrRev(theText, " ") + 1)
Cells(2, 4).Value = newTextRight
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.