FILTER(sourcearray, match [,include] [,compare])

Returns an array containing a subset of values that contain a substring (Variant).


sourcearrayThe array of strings to be searched (1-dimensional) (Variant / Array)
matchThe text string to search for (String).
include(Optional) True or False to specify whether to return substrings that include or exclude match (Boolean)
compare(Optional) A vbCompareMethod constant specifying the type of string comparison to use (Integer):
-1 = vbUseCompareOption (uses the "Option Compare" setting)
0 = vbBinaryCompare
1 = vbTextCompare
2 = vbDatabaseCompare (uses an Access database)

REMARKS
* This function searches an array for all the elements that match a given text string and returns an array of just the elements that match.
* There is no way to specify whether the search text should match the entire element or just part of it.
* If no matches of match are found within "sourcearray", then an empty array is returned.
* If "sourcearray" is Null, then an error occurs ??
* If "include" = True then the subset of the array that contains the substring "match" as a substring is returned.
* If "include" = False then the subset of the array that does not contain the substring "match" as a substring is returned.
* If "include" is left blank, then ???
* If "compare" is left blank, then 0 is used. CHECK
* The array that is returned only contains enough elements to contain the number of matched items.
* The array returned is always a 1 dimensional array.
* This function can be used to determine if a particular element exists in a 1-dimensional array.
* This function will always return a zero-based array irrespective of whether you declare "Option Base 1".
* This function can return an empty array. When this happens the lowerbound is 0 and the upperbound is -1.
* This function was added in Office 2000.
* The equivalent .NET function is Microsoft.VisualBasic.Strings.Filter
* For the Microsoft documentation refer to docs.microsoft.com

Dim myArray(2) As String 
Dim SubStrings() As String
myArray(0) = "This"
myArray(1) = "is"
myArray(2) = "it"
SubStrings = Filter(myArray, "is", True, VBA.vbCompareMethod.vbTextCompare)
Debug.Print VBA.Join(SubStrings, ",") 'This, is"
SubStrings = Filter(myArray, "is", True, VBA.vbCompareMethod.vbBinaryCompare)
Debug.Print VBA.Join(SubStrings, ",") 'This"
SubStrings = Filter(myArray, "is", False, VBA.vbCompareMethod.vbTextCompare)
Debug.Print VBA.Join(SubStrings, ",") 'is, it"

© 2019 Better Solutions Limited. All Rights Reserved. © 2019 Better Solutions Limited Top