Macro with Autofilter Rule

jevi

Active Member
Joined
Apr 13, 2010
Messages
339
Office Version
  1. 2016
Platform
  1. Windows
Hi All,

I have registered this macro but is not taking correctly the autofilter rule (criteria). I need to filter the data based on column X1 (data are from A1:A200 but may vary everyday the row of data) and have only the values that are <> "OK". I try to write it in the macro but is not working.

Please any help?

Thank you,

Range("X1").Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$X$200").AutoFilter Field:=24, Criteria:="<>OK", _


VBA Code:
Macro1 Macro
'

'
    Windows("CDatabase.xlsx").Activate
    Columns("A:W").Select
    Selection.Copy
    Windows("C Report.xlsm").Activate
    Columns("A:W").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Range("X1").Select
    Selection.AutoFilter
    ActiveSheet.Range("$A$1:$X$200").AutoFilter Field:=24, Criteria:="<>OK", _
    Sheets("DATABASE").Select
    Columns("A:X").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Sheet1").Select
    Sheets.Add After:=ActiveSheet
    Columns("A:A").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("B7").Select
    Sheets("P").Select
    Windows("C Database.xlsx").Activate
    ActiveWorkbook.Save
    ActiveWindow.Close
    Application.CutCopyMode = False
    ActiveWorkbook.Save
End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Try changing
Rich (BB code):
Criteria:="<>OK"

To
Rich (BB code):
Criteria1:="<>OK"
 
Upvote 0
Solution
Thank you both, it worked great:). Another question as I have a limit of row here, how can I say till the data finishes as the row might be different (for sure I can do 10000 rows max that can happen) but I am curios to know the VBA language :). ActiveSheet.Range("$A$1:$X$200").
 
Upvote 0
If your data starts in A1 then you can probably just use Activesheet.Range("A1").CurrentRegion
Current region will include everything until it hits a fully blank row or fully blank column.
 
Upvote 0
Ah Ok, super:). So now I know it and I can use it in the other VBA. Thank you so much for explianing that to me.
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,122
Members
452,381
Latest member
Nova88

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