JackDanIce
Well-known Member
- Joined
- Feb 3, 2010
- Messages
- 9,922
- Office Version
- 365
- Platform
- 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")
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
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