Macro autofilter Or/And operator

Tiobyte

New Member
Joined
Jul 11, 2018
Messages
7
Hi all,

I am currently using autofilter in a macro to move data over that = certain text.
Code:
With Sheets("Data")      
       If .AutoFilterMode Then .AutoFilterMode = False
      .Range("A1:O1").AutoFilter 6, "Test1"
      .Range("A1:O1").AutoFilter 2, "Test2"
      .Range("A1:O1").AutoFilter 3, "Test3"
      .AutoFilter.Range.Offset(1).Copy Sheets("Results").Range("A2")
      .AutoFilterMode = False
    End With

I now need to be able to say if column 6 = Test1 OR column 2 = Test2 OR column 3 = Test3.

I know how to do this with the or operator when its looking in the same column each time but not with different columns. All suggestions welcome thanks for your help.
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
If you can add another column to the sheet then you could use a formula such as =IF(OR(B1="Test2",C1="Test3",F1="Test1"),"Yes","No") then filter that column on "Yes".
 
Upvote 0
If you can add another column to the sheet then you could use a formula such as =IF(OR(B1="Test2",C1="Test3",F1="Test1"),"Yes","No") then filter that column on "Yes".

Unfortunately this way wont work for my scenario but thanks for the input.
 
Upvote 0
Tried the following code however it only applies the first condition not the or part, any ideas?
Code:
   With Sheets("Sheet1")      
       If .AutoFilterMode Then .AutoFilterMode = False
      .Range("A1:O1").AutoFilter 1, "Test1", Operator:=xlOr
      .Range("A1:O1").AutoFilter 6, "Test2"
      .AutoFilter.Range.Offset(1).Copy Sheets("Sheet2").Range("A2")
      .AutoFilterMode = False
   End With
 
Last edited:
Upvote 0

Thanks for the link, i came across this earlier and downloaded the sample file given. I tried recording a macro using the example filters with or/and functions to see what the VBA code would look like. From there i have unfortunately not been able to apply this to my code and get it to work but i will keep trying.
 
Upvote 0
I tried recording a macro using the example filters with or/and functions to see what the VBA code would look like. From there i have unfortunately not been able to apply this to my code and get it to work but i will keep trying.
Assuming column Z on the "Data" sheet is available as a helper column (if not substitute another column) then try this with a copy of your workbook.
Code:
Sub FilterAndCopy()
  Dim rCrit As Range
  
  With Sheets("Data")
    If .AutoFilterMode Then .AutoFilterMode = False
    Set rCrit = .Range("Z1:Z2")
    rCrit.Cells(2).Formula = "=OR(F2=""Test1"",B2=""Test2"",C2=""Test3"")"
    .Range("A1").CurrentRegion.AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=rCrit, CopyToRange:=Sheets("Results").Range("A2"), Unique:=False
    rCrit.ClearContents
  End With
  Sheets("Results").Rows(2).Delete
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,627
Messages
6,120,610
Members
448,973
Latest member
ChristineC

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