help creating a FILTER UDF

arielks

New Member
Joined
Mar 15, 2019
Messages
6
here is what I have right now
Code:
Function FILTERIF(select_range As range, select_column, select_row As Integer, criteria_column1 As Integer, criteria1 As String, Optional criteria_column2 As Integer, Optional criteria2 As String)Dim rows As Integer
Dim i As Integer
rows = select_range.rows.Count
Dim arrMod(1 To 99999) As String
N = 1


For i = 1 To rows
If select_range.rows(i).Columns(criteria_column1) = criteria1 Then
arrMod(N) = select_range.rows(i).Columns(select_column)
N = N + 1
End If
Next i


FILTERIF = arrMod(select_row)


End Function
which honestly works amazing but lacks a few features that I need.
  • how do I let it define multiple criterias the way that Sumifs and countifs has, where you can add more than 100 criterias.
  • how can I define the true/false condition for the IF statement in the excel formula itself? aka be able to define if I want the criteria to equal, not equal, be more than, or less than.
    • in countifs for example, its easy, you just add a "<"& at the beggining of the criteria to apply the condition, but the way that my function is coded right now, that is not possible and I'm not sure how to code it otherwise.

I appreciate all the help and tips you guys might have for me!
 

Some videos you may like

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.

arielks

New Member
Joined
Mar 15, 2019
Messages
6
also I apologize for double posting, can't find the edit button for some reason.

I wanted to give an example of how I use this function:

=FILTERIF(Invoices,1,2,3,"N6020733")

=FILTERIF(Invoices,1,2,3,"N6020733") 'which is a named table range

select_range = Invoices 'which is a named table range

=FILTERIF(Invoices,1,2,3,"N6020733")

select_column = 1 'define which column of the selected range do I want my result from.

=FILTERIF(Invoices,1,2,3,"N6020733")

select_row = 2 'define which row of the selected range do I want my result from.

=FILTERIF(Invoices,1,2,3,"N6020733")criteria_column1 = 3 'define which column do you want to filter by criteria

=FILTERIF(Invoices,1,2,3,"N6020733")

criteria1 = "N6020733"'define what value should criteria_column1 be equals to


so now I can have a range of cells
=FILTERIF(Invoices,1,1,3,"N6020733")
=FILTERIF(Invoices,1,2,3,"N6020733")
=FILTERIF(Invoices,1,3,3,"N6020733")
=FILTERIF(Invoices,1,4,3,"N6020733")
=FILTERIF(Invoices,1,5,3,"N6020733")

which will pull a filtered list of these ranges that can update on my sheet dynamically. Honestly it has been very useful so far, but unfortunately doesn't do as much as I hoped it would.
 

arielks

New Member
Joined
Mar 15, 2019
Messages
6
Please, I'm still looking for help.

I've changed the formula to now produce an array, and I can look for what I need with the INDEX formula.


Code:
Function XFILTERIF(select_range As range, select_column, criteria_column1 As Integer, criteria1 As String, Optional criteria_column2 As Integer, Optional criteria2 As String)Dim rows As Integer
Dim i As Integer
rows = select_range.rows.Count
Col = select_range.Columns.Count
Dim arrMod() As String
N = 0
If IsEmpty(select_range) Then Exit Function


For i = 1 To rows
If select_range.rows(i).Columns(criteria_column1) = criteria1 Then
N = N + 1
ReDim Preserve arrMod(1 To N, 1 To Col)


For j = 1 To Col
arrMod(N, j) = select_range.rows(i).Columns(j)
Next j


End If


Next i


XFILTERIF = arrMod


End Function

The idea is, that I can make a nested formula and that way I can use more than one filter, by filtering the same range the first formula produces, but that requires me to use the columns in the array!

I tried to do
Code:
ReDim Preserve arrMod(1 To N, 1 To Col)

For j = 1 To Col
arrMod(N, j) = select_range.rows(i).Columns(j)
Next j

but the formula does not seem to work when I added the line:
Code:
ReDim Preserve arrMod(1 To N, 1 To Col)

please help.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,230
Messages
5,594,947
Members
413,953
Latest member
Arthur1471

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