I\'m trying to do a XML request through excel vba for one of the internal links (in our company). when i send request and receive response using the code below, i get the follow
Considering the fact, that you have already parsed the XML to string, then the easiest fact is to try to slice the string. To see how it works, put the string from .responseText
to A1 range and run this:
Sub TestMe()
Dim responseText As String
responseText = Range("A1")
Dim myArr As Variant
myArr = Split(responseText, "Demand"":""")
Debug.Print Left(myArr(UBound(myArr)), Len(myArr(UBound(myArr))) - 4)
End Sub
What it does is to split the string into array by the word Demand":"
and to take anything but the last 4 characters of the last unit of the array.
You could use InStrRev
Mid$(responseText, InStrRev(responseText, ":") + 2, (InStrRev(responseText, "}") - 1) - (InStrRev(responseText, ":") + 2))
InStrRev walks the string from right to left. We know you want the value at the end of the string so this direction is useful. We specify as an argument the character to find. The overall string is the responseText
.
The first character to find is ":"
, from right to left. This will be where you have :"13449"}]
. Offset from this + 2 to get the actual start of the value you want, in this case the 1
in 13449
.
Same logic to determine end point of string. I use "}"
as end point then make an adjustment to move forward to the numbers. Mid allows you to specify a string, start point and number of characters. I pass the arguments to extract the required string to Mid
. I used typed functions (with the $
at the end) as more efficient when working with strings.