I would like to split a string with tokens separated ‘;’ into 2 list of values. For example I have the string: AXX1.CYY1;AXX2.CYY2;AXX3.CYY3;AXX4.CYY4 I would like list it to be separated into 2 lists, the first should contain values on the right of ‘.’ whereas the second should have values on the left Practically, the outcome should be:
AXX1;AXX2;AXX3;AXX4
CYY1;CYY2;CYY3;CYY4
So far I think my strategy is create a loop in a VBA function that splits the words. The single value is in turn split into 2 portions which are put in a list. Further to this how do you apply the code to excel cells. If there is any other method to accomplish it is welcome.
Your basic logic makes sense. It can be implemented like thus:
Function ChopandSplice(s As String) As Variant
Dim chunks As Variant, first As Variant, second As Variant
Dim temp As Variant
Dim i As Long
chunks = Split(s, ";")
ReDim first(0 To UBound(chunks))
ReDim second(0 To UBound(chunks))
For i = 0 To UBound(chunks)
temp = Split(chunks(i), ".")
first(i) = temp(0)
second(i) = temp(1)
Next i
ChopandSplice = Array(Join(first, ";"), Join(second, ";"))
End Function
Sub test()
Dim v As Variant
v = ChopandSplice("AXX1.CYY1;AXX2.CYY2;AXX3.CYY3;AXX4.CYY4")
Range("A1") = v(0)
Range("B1") = v(1)
End Sub
Running the test sub leads to:
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments