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.