Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 89246

Filter out Empty elements from Array - XPATH

$
0
0

Context:

I'm trying to Filter out Empty values from an array. I know we can loop an array to Redim Preserve but figured there could be a way to incorporate the Filter function. However, since Filter treats all elements in an Array as String data types, there is no such thing as arr = Filter(arr, Empty, False) which I have tried in many forms.

This brought me to WorksheetFuntion.FilterXML however, I have little to none experience in XPATH and am clearly doing something wrong missing the concept of elements and nodes. I'm aware that Excel works on XPATH 1.0 and therefor I was browsing for a way to "filter" out empty elements and came accross [not(node())]. However implementing this returned an Error 1004 on retrieving arr1


Sample:

Sub Test()

Dim arr0 As Variant, arr1 As Variant, arr2 As Variant

arr0 = Array("A", "A", Empty, "A")
arr1 = WorksheetFunction.FilterXML("<t><s>"& Join(arr0, "</s><s>") & "</s></t>", "//s[not(node())]")
arr2 = WorksheetFunction.FilterXML("<t><s>"& Join(arr0, "</s><s>") & "</s></t>", "//s[string-length(text()) > 0]")

End Sub

Question:

To circumvent the issue my second option was to check the length of the strings which in this case did work and returned an array without empty elements.

Can someone enlighten me on how to improve the [not(node())] syntax to make it work. Some explaination on why the current code doesn't work would also be highly appreciated.


Viewing all articles
Browse latest Browse all 89246

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>