Help with a tricky column filter

russelldt

Board Regular
Joined
Feb 27, 2021
Messages
158
Office Version
  1. 365
Platform
  1. MacOS
Hello,

I have the following macro button in place to filter, and unfilter column 5 (table 1) for quantities greater than 1. I have added another column 6, which has a 1 or 0. The macro button to filter and unfilter column 5 must only apply to rows with a 1 in Column6.

I can't quite figure this out.


Thanks




Sub FilterTable()

Dim ws As Worksheet
Dim tbl As ListObject
Dim rng As Range

Set ws = ActiveSheet
Set tbl = ws.ListObjects("Table1")


If tbl.AutoFilter.FilterMode Then

tbl.AutoFilter.ShowAllData
Else

Set rng = tbl.ListColumns(5).DataBodyRange
rng.AutoFilter Field:=1, Criteria1:="<>", Operator:=xlAnd
End If

End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
I may have misunderstood, but try this.

VBA Code:
Sub FilterTable()
    Dim ws As Worksheet
    Dim tbl As ListObject
    Dim rng As Range
    
    Set ws = ActiveSheet
    Set tbl = ws.ListObjects("Table1")
    
    If tbl.AutoFilter.FilterMode Then
        tbl.AutoFilter.ShowAllData
    Else
        Set rng = tbl.ListColumns(5).DataBodyRange
        tbl.Range.AutoFilter Field:=5, Criteria1:=">1", Operator:=xlAnd, Criteria2:="=1"
    End If
End Sub
 
Upvote 0
Thanks Chris, it works when I "hide" the rows in column 5, but when I unhide the rows, all the rows reappear, including the ones in column 6 that I don't want to show (those with a 0)
 
Upvote 0
Try:

VBA Code:
Sub FilterTable()
    Dim ws As Worksheet
    Dim tbl As ListObject
    Dim rng As Range
    
    Set ws = ActiveSheet
    Set tbl = ws.ListObjects("Table1")
    
    If tbl.AutoFilter.FilterMode Then
        tbl.AutoFilter.ShowAllData
    Else
        ' Apply filter to columns 5 and 6
        tbl.Range.AutoFilter Field:=5, Criteria1:=">1"
        tbl.Range.AutoFilter Field:=6, Criteria1:="=1"
        
        ' Hide the rows where column 6 has a value of 0
        Dim i As Long
        For i = tbl.DataBodyRange.Rows.Count To 1 Step -1
            If tbl.DataBodyRange.Cells(i, 6).Value = 0 Then
                tbl.DataBodyRange.Cells(i, 1).EntireRow.Hidden = True
            End If
        Next i
    End If
End Sub
 
Upvote 0
Sorry, same result. This is the before condition



1681746639154.png


this is filtered

1681746696448.png


then this when I unfilter

1681746736040.png
 

Attachments

  • 1681746578596.png
    1681746578596.png
    9.2 KB · Views: 2
Upvote 0

Forum statistics

Threads
1,214,905
Messages
6,122,174
Members
449,071
Latest member
cdnMech

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