Be the first user to complete this post
|
Add to List |
VBA-Excel: Array Functions – Filter()
Description:
Filter() Function returns one dimensional array containing the filtered array elements based upon the filter options provided
Format:
Filter(arrArrayName,FilterValue [, Include[, vbCompare]])
Arguments:
- arrArrayName
- Mandatory
- Type: Array
- Array which needs to be filtered
- FilterValue
- Mandatory
- The expression based on which the Array will be filtered
- Include
- Optional
- Type : Boolean
- Decides whether to include(True) or exclude(False) the FilterValue. Default is True that means it will include the FilterValue.
- vbCompare
- Optional
- Type: Numeric
- The type of comparison to find the string in the main string, like vbBinaryCompare (Value =0), vbTextCompare (value=1).
Example:
Function FnFilterArray() Dim arrA arrA = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December") arrTemp = Filter(arrA, "M") MsgBox Join(arrTemp, "*") arrTemp1 = Filter(arrA, "J", False) MsgBox Join(arrTemp1, "*") arrTemp1 = Filter(arrA, "e", True, vbTextCompare) MsgBox Join(arrTemp1, "*") End Function
Cases :
1) Filter(arrA, "M")
Filter all the elements of arrA and return array consisting elements which contains “M” (Upper Case M)
Note: Filter(arrA, "M") and Filter(arrA, "M", True) will return the same result because if True is the default value if not provided.
2) Filter(arrA, "J", False)
Filter all the elements of arrA and return array consisting elements which doesn’t contain “J”
3) Filter(arrA, "e", True, vbTextCompare)
Filter all the elements of arrA and return array consisting elements which contain “e” (Upper as well as lower cases)
Also Read:
- VBA-Excel: Date-Time Functions – Hour(), Minute(), Second()
- VBA-Excel: Date-Time Functions – WeekDay() and WeekDayName()
- VBA-Excel: Date-Time Functions - Timer()
- VBA-Excel: Date-Time Functions – Month(), Year() and MonthName()
- VBA Excel – Looping Through a Range of Cells