1

I want to find out if there are any elements in a list after applying filter to it. The list is of type variant which seems to be a problem. Most suggestions are in line with this post: but it does not work for me. What am i doing wrong?

Dim message As Variant
Dim tempMessage As Variant
Dim a As Long
message = Split("AACP;CP;sBcp;ccffcp", ";")
tempMessage = Filter(message, "CP", False, vbTextCompare)
If IsNull(tempMessage) = True Then
    Debug.Print "EMPTY"
Else
    Debug.Print tempMessage(0)
End If
Community
  • 1
  • 1
user3748315
  • 149
  • 3
  • 11

1 Answers1

3

You have a couple of ways:

  1. VarType(tempMessage) will be vbEmpty if it is completely blank.

  2. VarType(tempMessage) And vbArray will be non-zero if it's an array type (even with zero elements. Zero length arrays are allowed in VBA). Here I'm using And in the logical context.

If you've established it's an array, then you can take a guess at the dimensionality. (VBA provides no function for this, but luckily you should know from the documentation of Filter).

If you've established the variant is an array then use UBound(tempMessage) - LBound(tempMessage) + 1 to get the number of elements.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483