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!
 

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.
 

Forum statistics

Threads
1,082,257
Messages
5,364,074
Members
400,778
Latest member
Canadian Sal

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top