How to Combine Two Field in VBA Filter Function

gnaga

Well-known Member
Joined
Jul 9, 2002
Messages
748
Office Version
  1. 365
  2. 2016
  3. 2013
Platform
  1. Windows
Hi,

VBA Code:
Rng1.AutoFilter field:=10
                    Rng1.AutoFilter field:=18, Criteria1:="=" & ""
                    Rng1.AutoFilter field:=2, Criteria1:=">=" & ACel.Offset(I, -1), Operator:=xlAnd, Criteria2:="<=" & ACel.Offset(I, -1) + 1 
                    Rng1.AutoFilter field:=8, Criteria1:="=" & unqSmpIDs(J)

In the above code I want to add one more additonal sub criteria to Criteria2 with AND condition

for example

Rng1.AutoFilter field:=2, Criteria1:=">=" & ACel.Offset(I, -1), Operator:=xlAnd, Criteria2:=("<=" & ACel.Offset(I, -1) + 1 And ACel.Offset(I, 2) >= 0)

What is the syntax for this coding?
 

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.
You can't do more than 2 Custom Criteria on a single field. You will need to either use a helper column or switch to using an advanced filter.
 
Upvote 0
Thanks Alex. Could you please guide me how to use Advanced Filter Function in VBA for my requirement?
 
Upvote 0
Can you give:
• some sample data using XL2BB
• The name of the sheet
• what is in the criteria fields
 
Upvote 0
By the way can you explain your additional criteria.
Your initial criteria is that Field 2 is between to values (Value and Value + 1). One would assume that both these values are > 0, so what it the criteria for Field 2 that the 3rd item is meant to add ?
 
Upvote 0
Thanks Alex.

I would like to filter two dates (ex. 1st and 2nd may) this is my intial criteria. Additional criteria not from field 2 but from field 3 which is time data for 2nd May must be less than 6AM.

Basically I would like to compare two column in single criteria.

Sample data and expected out put is given below.

Book1
CDEFGHIJK
3Filter Output
4Rec NoDateTimeIDRec NoDateTimeID
5101-May-224:00:00 AM17S0046101-May-224:00:00 AM17S0046
6201-May-224:00:00 AM11S0046201-May-224:00:00 AM11S0046
7301-May-224:00:00 AM13S0010301-May-224:00:00 AM13S0010
8402-May-224:00:00 AM12S03BS402-May-224:00:00 AM12S03BS
9501-May-224:00:00 AM18S03AS
10602-May-227:00:00 AM17S0010
11702-May-228:00:00 AM15S0008
12803-May-224:00:00 AM14S0008
13903-May-224:00:00 AM19S0008
141004-May-224:00:00 AM17S0011
151104-May-224:00:00 AM11S0010
161204-May-228:00:00 AM12S0002
171305-May-228:00:00 AM12S0002
181406-May-228:00:00 AM12S0002
Sheet1
 
Upvote 0
It is quite late here so I am about to log off. Will have a look at it tomorrow.
So just to be clear. Any time on 1 May but only up to 6AM on 2 May ?

Can the code assume the heading at H4:K4 will already be there ?
 
Upvote 0
Yes. You are right.

1 may can be of any time but 2nd may up to 6AM.

I can maaintain the header H4:K4.
 
Upvote 0
Why isn't Rec No 5 included in the results since its date is 1 May?

Also, can you confirm whether you want to actually filter the original data in place, or whether you want the relevant data extracted to a separate location like in your post #6 Mini Sheet?

What is the name of the table that the original data is in?

I am assuming ..
  1. That in your sample Rec No 5 should be included in the results since its date is 1 May
  2. That you want the data filtered in place, not the results transferred to another place.
  3. That the table is called "Table1" and is on the Active Sheet
  4. That the table consists of the four columns shown only
  5. That, like your samples, Rec Nos are not repeated in that column.
If all those assumptions are correct, then you can use AutoFilter without a helper column as follows.

VBA Code:
Sub Filter_Special()
  Dim a As Variant, CritAry As Variant
  Dim i As Long, k As Long
 
  a = Range("Table1").Value
  ReDim CritAry(1 To UBound(a))
  For i = 1 To UBound(a)
    If a(i, 2) = DateSerial(2022, 5, 1) Or (a(i, 2) = DateSerial(2022, 5, 2) And a(i, 3) < TimeSerial(6, 0, 0)) Then
      k = k + 1
      CritAry(k) = a(i, 1)
    End If
  Next i
  ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=1, Criteria1:=Split(Join(CritAry)), Operator:=xlFilterValues
End Sub
 
Upvote 0
Solution
@Peter_SSs, I don't know which is faster but I have found that this also works instead of the Split-Join approach.

VBA Code:
  CritAry = Application.Trim(CritAry)
  ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=1, Criteria1:=CritAry, Operator:=xlFilterValues
 
Upvote 0

Forum statistics

Threads
1,215,420
Messages
6,124,803
Members
449,190
Latest member
cindykay

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