Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Filtering data

0

Hi.

I recorded the following code using the Macro Recorder.

 ActiveSheet.ListObjects("Table17").Range.AutoFilter Field:=7, Criteria1:= _

        Array("Failed", "Later", "NR", "NS", "="), Operator:=xlFilterValues, Criteria2 _

        :=Array(0, "10/17/2023")

when I try to rerun the code it retuns an error: "AutoFilter method of Range class failed".

The filtered column includes dates and text. I have tried various arrangements of the code but without success. Any help much appreciated, thank you.

EDIT:

(I oviously made my request to simple AND unclear, sorry about that. I  now try again with an updated sample file...the 'real' workbook is quite big, 12 sheets. Each with a fair amount of data. The sheet with the issue currently has 700 rows, but will increase).

l aready have buttons and a filtering facility at the top of the sheet.
What I a after is the vba code to make the following work: 

..when I click on the 'Display All' button every row should be displayed - except the rows with "N" in column "Action Date" - without clicking other buttons etc.
I can already filter out the "N"s as shown in the pink filtering-criteria area.
As I said above I would like the Display All button to exclude the "N" entries.
I hope I have covered things properly this timr.
Thank you for your patience with wih me

Answer
Discuss

Discussion

Hi Beepee and welcome to the Forum.   Please edit your original question and use the Add Files... button to upload a representative Excel file (without any personal data) to show your existing data (and any macros). Then we should be able to give specific help.
John_Ru (rep: 6142) Oct 18, '23 at 12:44 pm
Hi.
I think I've added a single sheet sample file, with a newly recorded  macro that has the same resulting error.
All I'm trying to do is get the VBA code to hide the N entries please.
Thanks again
Beepee (rep: 2) Oct 19, '23 at 6:44 am
Hi BeePee. Looks like you added another fike (and some text in the discussion following my Answer to your original "short" question). I don't have time today but will try to revise my Answer tomorrow, not least since it makes little sense now!

Meantime please note the mis-spelling "Amanac" in the image at tbe top of worksheet Almanac. 
John_Ru (rep: 6142) Oct 24, '23 at 12:05 pm
Thanks for the replacement file, please see the second file to my Answer (and text following  "Revision 25 October 2023"). Please remember to mark Answer as Selected (if it works well for you)
John_Ru (rep: 6142) Oct 25, '23 at 9:47 am
Add to Discussion

Answers

0
Selected Answer

Beepee

Thanks for attaching the file. Before I Answer, please note that is this fixes your problem, please mark the Answer as Selected (in accordance with the Forum Rules).

In the attached revised files, I've replaced your macro-recorded line with the one in bold below (and revised the comments describing the procedure, including a note about how to filter out dates):

Sub HideN()
' To hide rows where column G matches cell K1 - text entries only,
' unless dates are converted to serial number using e.g.=DATEVALUE("05/10/2023")
'

    'ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=7, Criteria1:= _
        Array("Later", "NR", "NS", "="), Operator:=xlFilterValues, Criteria2:=Array(0, _
        "10/5/2023")

    ' ## replaced macro recorder line above with...
    ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=7, Criteria1:="<>" & Range("K1").Value

End Sub

If you put N in (yellow) cell K1 and press the purple button (near row 1 column J) labelled "Hide entries in G=", all the N entires will be hidden. Change it to NR and press again- they'll be restored but NR entries will be hidden.

To clear the filter altogether, click the green button "Clear filter from table" (to the right of K1, which is assigned to this code:

Sub ClearFilter()
' To clear filter from table

    If ActiveSheet.ListObjects("Table1").Range.AutoFilter = True Then ActiveSheet.ListObjects("Table1").Range.AutoFilter

End Sub

Revision 25 October 2023:

This addresses the (replacement) file attached to the question.

The current button ""Display All" invokes the code below, modified as follows (see changes in bold):

Private Sub AlmanacClearFilter()    'P226 clear filters to Display All
        'Origin:  1 Jul 2023 (C)BeePee From: Paul Kelly Webinar - excelmacromastery.com/vba-advanced-filter/
            'called via IndicateShape, from click-buttom "Display All" (Almanac w/s)
            'vbaGardenProject1(GardenAlmanac.xlsm)
            'Module: Almanac
        'Version update -- v 1.0  --  1 Jul 2023
        '
        '       Last saved as word docx  Ver 1.0         2023 18:00
'-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

'clear Filters
'NB TrialFiltering is code name for almanac w/s
   If TrialFiltering.FilterMode = True Then
      TrialFiltering.ShowAllData
   End If

'select current region
   Range("D8").CurrentRegion.Select
   Selection.Interior.Color = RGB(225, 240, 220) 'sage green

'reset header colour
   Range("B7:I7").Interior.Color = RGB(35, 95, 145)   'dark blue
   '''Worksheets("Filter Data").

   'Range("B7").AutoFilter ' display filter icons
   ' ## line above replaced with this to filter out N in column F...
   Range("B7").CurrentRegion.AutoFilter Field:=7, Criteria1:="<>N"
   Range("D8").Select

End Sub     'endAlmanacClearFilter P226

This displays all lines APART from those with "N" in column F. Accordingly, in the second attached file,  I have relabelled that button "Display All (<>N)" (and also corrected the spelling to Commodity in two places).

Hope this fixes your problem. If so, don't forget to mark this Answer and Selected (sorry to repeat but so many new user do forget)

Discuss

Discussion

NB I created the following msg then discovered that I don't know how to replace the sample file??? HELP!!! please.

Hi. Yes that works Thanks. However, I oviously made my request to simple AND unclear, sorry about that. I will now try again with an updated sample file...
The 'real' workbook is quite big, (12 sheets. Each with a fair amount of data). The sheet with the issue currently has 700 rows (but will increase).
I already have buttons and a filtering facility at the top of the sheet.
What I would like to happen is:
...when I click on the 'Display All' button every row should be displayed - except the rows with "N" in column "Action Date" - without clicking other buttons etc.
I can already filter out the "N"s as shown in the pink filtering-criteria area.
As I said above I would like the Display All button to exclude the "N" entries.
I hope I have covered things properly this timr.
Thank you for your patience with wih me
Regards...
Beepee (rep: 2) Oct 19, '23 at 11:27 am
BeePee. You can add a second Excel file to your original question - edit that and use the Add Files button to add the second, multi-page file.

For future questions, please try to give a full description at the start and don't expand the question if an Answer solves what you asked- we really don't like doing things twice or more!
John_Ru (rep: 6142) Oct 19, '23 at 1:11 pm
Hi John_Ru
That's exactly what I needed thank you. As CT says ...only easy if you know the answer. But you have made it look easy so thank-you again and also for the other corrections. I hope I have marked the correct bit for the soloution.
Beepee (rep: 2) Oct 25, '23 at 12:11 pm
Glad that worked for you. Thanks for selecting my Answer BeePee (you "marked the correct bit" thanks). 
John_Ru (rep: 6142) Oct 25, '23 at 2:49 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login