【问题标题】:Break string based on a character in VBA 2010基于 VBA 2010 中的字符中断字符串
【发布时间】:2013-04-19 07:38:47
【问题描述】:

在 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"

有什么想法吗?

【问题讨论】:

    标签: vba excel substr


    【解决方案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
    

    【讨论】:

      【解决方案2】:

      使用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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-27
        • 1970-01-01
        • 1970-01-01
        • 2017-02-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多