如何使用VBA在逗号之间分割字符串并将每个字符串存储在数组变量中

时间:2018-09-30 16:20:47

标签: arrays excel string vba excel-vba

我有一个问题:   如何在逗号之间用逗号分隔开括号,并将其存储在数组变量中?

示例:

strinput = "( u1 u1t_a, u2 u2t_b, s2 s2t_c, s4 s4t_d, ...n )"

上面有一个输入字符串,我想在数组变量中存储逗号之间的三个等子字符串,它们之间用逗号隔开:

substr(0) = "u1 u1t_a"
substr(1) = "u2 u2t_b"
substr(2) = "s2 s2t_c"
substr(n) = "...n"

截至目前,我在VBA中很难同时使用循环和数组,因此我的代码就像蛮力的,由于代码太长,最多只能处理3个文本,所以我做了一个限制。

在这里查看我的代码:

strinput = "( u1 u1t_a, u2 u2t_b, s2 s2t_c )"

substr1 = Right(strinput, Len(strinput) - Find("(", strinput))
    'Output: u1 u1t_a, u2 u2t_b, s2 s2t_c )
substr1f = Left(substr1, Find(",", substr1) - 1)
    'Output: u1 u1t_a

substr2 = Right(substr1, Len(substr1) - Find("(", substr1))
    'Output: u2 u2t_b, s2 s2t_c )
substr2f = Left(substr2, Find(",", substr2) - 1)
    'Output: u2 u2t_b

substr3 = Right(substr2, Len(substr2) - Find("(", substr2))
    'Output: s2 s2t_c )
substr3f = Left(substr3, Find(")", substr3) - 1)
    'Output: s2 s2t_c

如何进行此循环?

4 个答案:

答案 0 :(得分:10)

这是您要尝试做的吗?我已经注释了该代码,因此您可能不会对它有所了解...并且欢迎使用stackoverflow:)

Option Explicit

Sub Sample()
    Dim Ar As Variant
    Dim strinput  As String, s As String
    Dim i As Long

    strinput = "( u1 u1t_a, u2 u2t_b, s2 s2t_c, s4 s4t_d, ...n )"

    '~~> Replace ( and ) with ""
    s = Replace(Replace(strinput, ")", ""), "(", "")

    '~~> Split and store in an arry based on ","
    Ar = Split(s, ",")

    '~~> See what is there in the array
    For i = LBound(Ar) To UBound(Ar)
        Debug.Print Ar(i)
    Next i
End Sub

如果您想合并Replace and Split,那么也可以使用它

Option Explicit

Sub Sample()
    Dim Ar As Variant
    Dim strinput  As String
    Dim i As Long

    strinput = "( u1 u1t_a, u2 u2t_b, s2 s2t_c, s4 s4t_d, ...n )"

    Ar = Split(Split(Split(strinput, "(")(1), ")")(0), ",")

    '~~> See what is there in the array
    For i = LBound(Ar) To UBound(Ar)
        Debug.Print Ar(i)
    Next i
End Sub

答案 1 :(得分:4)

怎么样:

Sub qwerty()

    strinput = "( u1 u1t_a, u2 u2t_b, s2 s2t_c, s4 s4t_d, ...n )"
    strinput = Replace(Replace(strinput, "( ", ""), " )", "")
    arr = Split(strinput, ", ")

    For Each a In arr
        MsgBox a
    Next a
End Sub

答案 2 :(得分:0)

另一种风味:

Sub MySub()
    Dim strinput As String, a As Variant
    strinput = "( u1 u1t_a, u2 u2t_b, s2 s2t_c, s4 s4t_d, ...n )"
    For Each a In Split(Mid$(strinput, 3, Len(strinput) - 4), ", ") ‘ first extract a substring out of your string leaving off first two and last two characters, then Split resulting string using commas as delimiter
        MsgBox a
    Next
End Sub

您可以see here for Mid$() function

答案 3 :(得分:0)

您可以使用修整功能删除空格。

Sub test()
    Dim substr As Variant
    Dim strinput As String
    Dim i As Long

    strinput = "( u1 u1t_a, u2 u2t_b, s2 s2t_c, s4 s4t_d, ...n )"

    strinput = Replace(strinput, "(", "")
    strinput = Replace(strinput, ")", "")

    substr = Split(strinput, ",")

    For i = LBound(substr) To UBound(substr)
        substr(i) = Trim(substr(i))
        Debug.Print substr(i)
    Next i
End Sub