UDF to mimic FILTER function

JackDanIce

Well-known Member
Joined
Feb 3, 2010
Messages
9,922
Office Version
  1. 365
Platform
  1. Windows
Hi,

Latest version of Excel has new functions FILTER, UNIQUE and SORT, which I'm using fine.

Trying to create UDF versions of this to support use in Excel 2010, I'm struggling with this UDF function for FILTER, where

arr is a range input
arr_incl is a second range (equal size) input with only TRUE/FALSE values
ifEmpty is to be returned if arr_incl is only FALSE for all values but when I test it, it only returns #VALUE

Setup
A1:A4 contains A, dz, ez, B
C1 = UDF_FILTER(A1:A4,ISNUMBER(SEARCH("z",A1:A4)),"not found")
Code:
Public Function UDF_FILTER(ByRef arr As Variant, ByRef arr_incl As Variant, Optional ByRef ifEmpty As Variant = "") As Variant

    Dim a   As Long
    Dim r   As Variant: r = arr.Value
       
    UDF_FILTER = ifEmpty
   
    For a = LBound(r, 1) To UBound(r, 1)
        If arr_incl(a, 1) <> True Then r(a, 1) = ""
    Next a
   
    'Missing test if r is empty for all elements If Len(join(r)) <> 0 Then UDF_Filter = r, to do
    UDF_FILTER = r
   
    Erase r
End Function

If formula is in cell C1, I'd like output to show C1 = dz and C2 = ez only (aware my code above doesn't sort or remove empty spaces, but unsure how to say

"cell formula is in".Resize(Ubound(UDF_Filter, 1)).Value = UDF_Filter
"cell formula is in".Resize(Ubound(UDF_Filter,1)).SpecialCells(xltypeblanks).Delete xlUp

Any suggestions?

TIA,
Jack
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

Forum statistics

Threads
1,213,536
Messages
6,114,215
Members
448,554
Latest member
Gleisner2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top