[英]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.