简体   繁体   中英

Write array results to string using Excel VBA

I have to loop through fval and write the corresponding text value as comma separated string like val1,val1,val3 , further description is provided below.

fval = "[12, 2, 7]"

Each value in fval is having corresponding text values like

id : "12" = "val1"
id : "7"  = "val2"
id : "2"  = "val3"

The code I have written is stuck at splitting fval and storing them into array.

Dim arry() As String

arry() = Split(Mid(fval, 2, Len(fval) - 2), ", ")

For x = LBound(arry) To UBound(arry)
    If id = arry(x) Then          

    End If
Next

Should I make an another array to store the returning values to write? Or is there any other workarounds.

As @Sivaprasath V mentioned concatenating the values works

Dim arry() As String

arry() = Split(Mid(fval, 2, Len(fval) - 2), ", ")

For x = LBound(arry) To UBound(arry)
    If id = arry(x) Then          
        If fieldVal <> "" Then
           fieldVal = fieldVal & ", " & id
        Else
           fieldVal = id
        End If
    End If
Next

By end of the loop fieldVal = val1,val1,val3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM