Private Sub cmd1_Click()
Dim strArray1() As String, strArray2() As String
Dim strMultiplier As String, strText As String, strOut As String, strDelimiter As String * 1
Dim varDelimiters As Variant
Dim i As Integer, j As Integer, k As Integer, p As Integer
Rem retrieve text from wherever it's held from the SMS
Rem for this test, use literal strings to represent possible scenarios
strText = "1001,2010,458,456=10,895,893,5677=150" ' test case 1
'strText = "1001,2010,458,456=10,895,893,5677=150" ' test case 2
'strText = "1010 2010 x10 895 893 5677 x15" ' test case 3
'strText = "1001 2010 458 456=10 895 893 5677=150" ' test case 4
Rem initialise array with all possible delimiters
varDelimiters = Array(",", " ")
For i = 0 To UBound(varDelimiters)
strArray1 = Split(strText, varDelimiters(i))
Rem check if array has single element - indicates delimiter not present in input
If UBound(strArray1) > 1 Then Exit For
Next
ReDim strArray2(0)
For i = 0 To UBound(strArray1)
p = InStr(1, strArray1(i), "=") ' 1st possible multiplier delimiter
If p = 0 Then p = InStr(1, strArray1(i), "x") ' 2nd possible multiplier delimiter
Rem repeat the line above for each variant of multiplier delimiter (e.g. [EMAIL="'@'"]'@'[/EMAIL])
If Not p = 0 Then
strMultiplier = Right(strArray1(i), Len(strArray1(i)) - p)
Rem check whether current element has a term preceding the multiplier
If p > 1 Then
Rem term present; remove the multiplier
strArray1(i) = Left(strArray1(i), p - 1)
k = UBound(strArray2)
ReDim Preserve strArray2(i)
Else
Rem term not present; clear the element
strArray1(i) = vbNullString
k = UBound(strArray2)
ReDim Preserve strArray2(i - 1)
End If
Rem step through the multiplier array to find empty slot to populate
For j = k To UBound(strArray2)
If strArray2(j) = vbNullString Then
strArray2(j) = strMultiplier
End If
Next
End If
Next
Rem reconstitute the two arrays into a single string
strOut = vbNullString
For i = 0 To UBound(strArray1)
If strArray1(i) <> vbNullString Then
If strOut <> vbNullString Then strOut = strOut & ","
strOut = strOut & strArray1(i) & "=" & strArray2(i)
End If
Next
MsgBox "Input string:" & vbCr & strText & vbCr & vbCr & "Output string:" & vbCr & strOut, vbInformation, "Test Results"
End Sub