Break string based on a character in VBA 2010

前端 未结 2 343
太阳男子
太阳男子 2021-01-15 14:39

In Excel 2010, using VBA, how can I breakapart a string when it finds a certain character?

Let say A1 = \"This is a | test of | the | emergency br

相关标签:
2条回答
  • 2021-01-15 15:35

    Use 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
    
    0 讨论(0)
  • 2021-01-15 15:40

    Use this as there is no need for a loop, also it is important to leave the Application.Trim() in:

    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
    
    0 讨论(0)
提交回复
热议问题