6

I have a question: How can I split the string between commas which are inside open and closed parentheses and store each in an array variable?

Example:

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

Having an input string above, I want to store in an array variable the three and so on substring between commas which are inside open and closed parentheses :

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

As of now, I am having difficulty of using loop together with array in VBA so my code is like a brute force which can only process a maximum of 3 text since the code will became long so I made a limit.

See my code here:

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

How can I make this loop?

1
  • Start with counting the number of commas in your string. Then make a loop. Commented Sep 30, 2018 at 16:28

4 Answers 4

10
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

If you want to combine the Replace and Split then you can use this as well

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, Mr. Rout for your response, I tried the code and it works just like what I've wanted. Also because of your answer, I just realize the idea of having to replace first the open and closed parentheses, then focus on string between commas, that's an awesome technique and problem solving I just learned. For now, I will learn the functions of LBound and UBoundin the code. Also, thank you for providing comments so that I can understand your code, I really appreciate it! Having my first question, with this welcoming answer really makes me feel welcomed. Thank you very much!
4

How about:

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 Comments

Thank you so much for your response, I tried this and it works. But I have additional question, I don't understand the variable a, how does it able to output the correct string? Like how does variable a works together with variable arr in loop? Thank you very much!
@VinIsLearning the variable a represents an individual item in the array arr()
0

Another flavour:

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

You can see here for Mid$() function

Comments

0

You can use the trim function to remove blank spaces.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.