根据VBA 2010中的字符中断字符串

时间:2013-04-19 07:38:47

标签: excel vba excel-vba substr

在Excel 2010中,使用VBA,如何在找到某个字符时对字符串进行分离?

A1 = "This is a | test of | the | emergency broadcast signal" 我将其分配给像

这样的变量
strColumnA = Range("A" & CStr(currRow)).Value

现在我想在工作表2的末尾添加4个新行。所有列A,如:

A1 = "This is a"
A2 = "test of"
A3 = "the"
A4 = "emergency broadcast signal"

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

使用此功能,因为不需要循环,将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

答案 1 :(得分:0)

使用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
相关问题