UDF to mimic FILTER function

JackDanIce

Well-known Member
Joined
Feb 3, 2010
Messages
9,727
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
 

Some videos you may like

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.

Watch MrExcel Video

Forum statistics

Threads
1,127,601
Messages
5,625,749
Members
416,133
Latest member
ToseSenpai

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
Top