The Alanian answer is at the root of your problem, but does not go completely.
Perhaps you need to remember that a line of type DisplayNode = xNode.Text simply tells VBA what to return after the function exits, but actually does not exit the function. VBA will happily allow you to assign and reassign the return value and simply return everything you told him to last.
In addition, if you never assign a value to a function, VBA assumes that you want to return the default value regardless of the type of function. In your case, your function is of type Variant , and the default value of Variant is Empty . You can see this as zero because VBA implicitly converts Empty to zero in all situations.
With that in mind, consider what happens in your example if any of them are true:
1) your Nodes parameter has nothing in it (i.e. it is an empty array or collection, or something else in MSXML)
2) there are some nodes, but none of them answer any of the tests in your loop
3) there are some nodes, and the last node satisfies (1) or (2) above
You should be able to see that your function will return Empty , even if one or more LastTradePriceOnly nodes are found on it through your node tree. And then you will see it as zero when you display it in the message box.
It sounds like you really need a more specific subroutine - maybe something that finds a particular node in the tree and causes an error if it cannot do this. I think it depends on your data, but in this explanation your closest question should be considered as to why you can see zero in your message box.
source share