根据VBA 2010中的一个字符打断string
在Excel 2010中,使用VBA时,如何在find某个字符时将string拆分?
假设A1
= "This is a | test of | the | emergency broadcast signal"
并且我把它分配给一个像这样的variables
strColumnA = Range("A" & CStr(currRow)).Value
现在我想在工作表2的末尾追加4个新行。所有列A,如:
A1 = "This is a" A2 = "test of" A3 = "the" A4 = "emergency broadcast signal"
有任何想法吗?
因为不需要循环,所以将Application.Trim()
放在下面也是很重要的:
Sub test() Dim r As Variant, s As String s = [a1].Value r = Split(Application.Trim(s), "|") [b1].Resize(UBound(r, 1) + 1) = Application.Transpose(r) End Sub
使用Split()
Sub Sample() Dim Ret Dim strColumnA As String Dim i As Long strColumnA = "This is a | test of | the | emergency broadcast signal" Ret = Split(strColumnA, "|") For i = LBound(Ret) To UBound(Ret) Debug.Print Ret(i) Next i End Sub