Sticking filter criteria in VBA

picklefactory

Well-known Member
Joined
Jan 28, 2005
Messages
506
Office Version
  1. 365
Platform
  1. Windows
Hi folks
I have a workbook I'm trying to tweak. I have a number of form control buttons running macros on the title bar of the sheet simplifying a few very basic tasks just sorting or filtering based on single criteria only... all works fine, been using it for weeks now....until I try and add an additional filter for a different column and criteria. Currently, there was only one filter in place. This new filter is still only based on a single criteria in a single column, but no matter how I choose to action it, once it has functioned, I cannot get rid of it and it then seems to work as an additional criteria to my other filter. I'll try and explain more clearly. The first, original filter is simply filtering a range by customer name in a single column from a ComboBox in a user form that generates a list of customers, that works fine with no noticeable issues. I have a Reset function to reset the entire workbook back to initial opening condition..... also works fine. The new filter is simply an alternative option to the customer filter to filter any rows that contain "No WO" in a specific column, and is not intended to work with or as an additional criteria to the customer filter, simply a second, stand alone filter for a different column and criteria. I have tried umpteen methods of actioning it, a simple control button on the worksheet running a macro, a command button on a userform running from a textbox input, running it manually from the VBA window etc..... all of them operate the new filter perfectly, but it then seems to stick. I can reset the sheet and all goes back to WB opening condition (Unfiltered), at least visually, but if I then try and run the customer name filter, that then functions with the added criteria of the 2nd filter, so I can then only see rows containing the selected customer + "No WO" as a 2nd criteria. It's probably something dumb I'm missing, but it's been frying my head all afternoon. Can anyone see what I'm missing please?

Apologies for the ton of code here, but I don't know where the issue is, so not sure which bits to post.

This is the sheet unfiltered


Book1
ABCDEFGHIJK
1SALES ORDERCUST POPART NUMBERPART DESCRIPTIONCUSTOMERQTYDUE DATESTOCKNO OF OPSCURRENT OPWORKS ORDER
313459020759706BRACKETCastings PLC8430/05/187111211739
513447CFC2 ENG-113029BG4001.0EDGE SENSOR JIG BOTTOMOcado Ltd129/04/1802211746
613447CFC2 ENG-113029BG4001.1EDGE SENSOR JIG TOPOcado Ltd129/04/1803211747
813416X22919161728-467861890 TANK ID ADR CHUTEWhale Tankers101/05/180No WO
1013441180416WG.002COOLANT PROTECTOR 750MMWogaard1030/04/180No WO
14101415500034847347-10450WA Fan Guard LHJCB Manufacturing2001/05/1807211811
15101425500182547333-H0405WA RH FAN GUARDJCB Manufacturing2001/05/1807211812
191345414354667-2-30070-0033 ASPECT BRACKETCounterplas Ltd250030/05/1805211816
201345414354667-2-30078-000SIDE TO SIDE PLATESCounterplas Ltd50030/05/180No WO
211345414354667-2-30070-0044 ASPECT BRACKETCounterplas Ltd35030/05/1805211817
221345414354667-2-30070-0022 ASPECT BRACKETCounterplas Ltd35030/05/18051211818
24103585500048951320-06429FLANGE EXHAUSTJCB Power Systems20030/04/1820032211607
26101435500034846347-10322WA Mounting BracketJCB Manufacturing2016/04/180No WO
2810632RE36557335-A0563PRESSING FENDER MOUNTJCB Heavy Products2521/05/180No WO
3013350X20975171521-55280SUPPORT BAR SUZIEWhale Tankers112/03/180No WO
3113390X21957157222-45439WATER FEED PIPEWhale Tankers109/04/18094211624
3213390X21957157429-45482VACUUM PUMP TSWhale Tankers109/04/1806211630
3313390X21957168537-52199SPIGOT-TUBEWhale Tankers109/04/180No WO
3413406X22492154072-40922HYD BOOM SIDE ARM SUPPORTWhale Tankers114/04/1803211760
FAB



This is the sheet after the customer filter


Book1
ABCDEFGHIJK
1SALES ORDERCUST POPART NUMBERPART DESCRIPTIONCUSTOMERQTYDUE DATESTOCKNO OF OPSCURRENT OPWORKS ORDER
813416X22919161728-467861890 TANK ID ADR CHUTEWhale Tankers101/05/180No WO
3013350X20975171521-55280SUPPORT BAR SUZIEWhale Tankers112/03/180No WO
3113390X21957157222-45439WATER FEED PIPEWhale Tankers109/04/18094211624
3213390X21957157429-45482VACUUM PUMP TSWhale Tankers109/04/1806211630
3313390X21957168537-52199SPIGOT-TUBEWhale Tankers109/04/180No WO
3413406X22492154072-40922HYD BOOM SIDE ARM SUPPORTWhale Tankers114/04/1803211760
3513406X22492154074-43468EXTENDING ARM FABWhale Tankers114/04/18071211761
3613406X22492175491-556755" PIPEWORKWhale Tankers214/04/1807211633
3713406X22492175783-55916HIGH LIFT BOOM ARMWhale Tankers114/04/1809211762
3813406X22492175787-55927MOUNTING BRACKETWhale Tankers114/04/1804211765
FAB


This is the sheet after 'No WO' filter

Book1
ABCDEFGHIJK
1SALES ORDERCUST POPART NUMBERPART DESCRIPTIONCUSTOMERQTYDUE DATESTOCKNO OF OPSCURRENT OPWORKS ORDER
813416X22919161728-467861890 TANK ID ADR CHUTEWhale Tankers101/05/180No WO
1013441180416WG.002COOLANT PROTECTOR 750MMWogaard1030/04/180No WO
201345414354667-2-30078-000SIDE TO SIDE PLATESCounterplas Ltd50030/05/180No WO
26101435500034846347-10322WA Mounting BracketJCB Manufacturing2016/04/180No WO
2810632RE36557335-A0563PRESSING FENDER MOUNTJCB Heavy Products2521/05/180No WO
3013350X20975171521-55280SUPPORT BAR SUZIEWhale Tankers112/03/180No WO
3313390X21957168537-52199SPIGOT-TUBEWhale Tankers109/04/180No WO
4713283X19006 C/O135075SA REEL SPOOL 120MTR LWTWhale Tankers430/04/180No WO
5213434X23406156726-44528WITTIG RFW 120 PUMPWhale Tankers101/05/180No WO
5613308X19882171524-55295BRACKET SUZI SUPPORTWhale Tankers203/05/180No WO
5713308X19882171520-55279OS PACKING SHIMWhale Tankers303/05/180No WO
6713465X245621456311300 DE-WATERING ASSY. DRWhale Tankers522/05/180No WO
6913445X23769 C/O135075SA REEL SPOOL 120MTR LWTWhale Tankers530/05/180No WO
FAB


This is the sheet when I then reset and clear all filters, then rerun the customer filter.... it also brings in the "No WO" filter too


Book1
ABCDEFGHIJK
1SALES ORDERCUST POPART NUMBERPART DESCRIPTIONCUSTOMERQTYDUE DATESTOCKNO OF OPSCURRENT OPWORKS ORDER
813416X22919161728-467861890 TANK ID ADR CHUTEWhale Tankers101/05/180No WO
3013350X20975171521-55280SUPPORT BAR SUZIEWhale Tankers112/03/180No WO
3313390X21957168537-52199SPIGOT-TUBEWhale Tankers109/04/180No WO
4713283X19006 C/O135075SA REEL SPOOL 120MTR LWTWhale Tankers430/04/180No WO
5213434X23406156726-44528WITTIG RFW 120 PUMPWhale Tankers101/05/180No WO
5613308X19882171524-55295BRACKET SUZI SUPPORTWhale Tankers203/05/180No WO
5713308X19882171520-55279OS PACKING SHIMWhale Tankers303/05/180No WO
6713465X245621456311300 DE-WATERING ASSY. DRWhale Tankers522/05/180No WO
6913445X23769 C/O135075SA REEL SPOOL 120MTR LWTWhale Tankers530/05/180No WO
FAB



Worksheet Open Code

Code:
Private Sub Workbook_Open()
    Application.ScreenUpdating = False
    
    Dim x As Workbook

'## Open FAB workbook first:
    Set x = Workbooks.Open("X:\ORDER BOOK2\NEW ORDER BOOK\Order Book Fab Data.xls")
    
    'Copy/paste FAB data
    Application.Calculate
    Worksheets("FAB").Activate
    ActiveSheet.Cells.EntireRow.Hidden = False 'ENSURE ALL ROWS ARE UNHIDDEN INITIALLY
    Worksheets("DATA INPUT").Range("A2:L500").Copy
    Worksheets("FAB").Range("A2").PasteSpecial xlPasteValues
    Worksheets("FAB").Range("A2").Select
    BeginRow = 1 'START ROW
    EndRow = 500
    ChkCol = 1

    For RowCnt = BeginRow To EndRow
        If Cells(RowCnt, ChkCol).Value = "HIDE" Then
            Cells(RowCnt, ChkCol).EntireRow.Hidden = True
        End If
    Next RowCnt
     
     x.Close
   
    'Open MC shop workbook next
    Set x = Workbooks.Open("X:\ORDER BOOK2\NEW ORDER BOOK\Order Book MC Data.xls")
 
    'Copy/paste MC shop data
    Application.Calculate
    Worksheets("MACHINE SHOP").Activate
    ActiveSheet.Cells.EntireRow.Hidden = False 'ENSURE ALL ROWS ARE UNHIDDEN INITIALLY
    Worksheets("DATA INPUT MC SHOP").Range("A2:K500").Copy
    Worksheets("MACHINE SHOP").Range("A2").PasteSpecial xlPasteValues
    Worksheets("MACHINE SHOP").Range("A2").Select
    BeginRow = 1 'START ROW
    EndRow = 500
    ChkCol = 1

    For RowCnt = BeginRow To EndRow
        If Cells(RowCnt, ChkCol).Value = "HIDE" Then
            Cells(RowCnt, ChkCol).EntireRow.Hidden = True
        End If
    Next RowCnt
    
    x.Close

   
    
    'Hide row/column letters/numbers
        Dim wsSheet As Worksheet
    For Each wsSheet In ThisWorkbook.Worksheets
        If Not wsSheet.Name = "Blank" Then
            wsSheet.Activate
            With ActiveWindow
                .DisplayHeadings = True
                .DisplayWorkbookTabs = True
                .DisplayHorizontalScrollBar = True
            End With
        End If
    Next wsSheet
    
    Application.ScreenUpdating = True
    Worksheets("FAB").Activate
    Worksheets("FAB").Range("A2").Select
    Call ClearClipboard
End Sub

Module 1

Code:
Sub DateSortFAB()
 Range("A2:L250").Sort key1:=Range("G2"), order1:=xlAscending, _
  Header:=xlNo
End Sub
Sub ValueSortFAB()
 Range("A2:L250").Sort key1:=Range("L2"), order1:=xlDescending, _
  Header:=xlNo
End Sub
Sub CustomerSortFAB()
 Range("A2:L250").Sort key1:=Range("E2"), order1:=xlAscending, _
  Header:=xlNo
End Sub
Sub FilterCustomerFAB()
UserForm1.Show
End Sub
Sub ResetFAB()
    Application.ScreenUpdating = False
    ActiveSheet.Cells.EntireRow.Hidden = False 'ENSURE ALL ROWS ARE UNHIDDEN INITIALLY
    Worksheets("DATA INPUT").Range("A2:L500").Copy
    Worksheets("FAB").Range("A2").PasteSpecial xlPasteValues
    Worksheets("FAB").Range("A1").Select
    BeginRow = 1 'START ROW
    EndRow = 500
    ChkCol = 1

    For RowCnt = BeginRow To EndRow
        If Cells(RowCnt, ChkCol).Value = "HIDE" Then
            Cells(RowCnt, ChkCol).EntireRow.Hidden = True
        End If
    Next RowCnt
    ActiveSheet.Cells.EntireRow.Hidden = False 'ENSURE ALL ROWS ARE UNHIDDEN INITIALLY
    Worksheets("DATA INPUT").Range("A2:L500").Copy
    Worksheets("FAB").Range("A2").PasteSpecial xlPasteValues
    Worksheets("FAB").Range("A1").Select
    Application.CutCopyMode = False
    BeginRow = 1 'START ROW
    EndRow = 500
    ChkCol = 1

    For RowCnt = BeginRow To EndRow
        If Cells(RowCnt, ChkCol).Value = "HIDE" Then
            Cells(RowCnt, ChkCol).EntireRow.Hidden = True
        End If
    Next RowCnt
    Application.ScreenUpdating = True
    
    Call ClearClipboard

End Sub


UserForm 1 code - this is the original filter that runs from the combobox

[CODEPrivate Sub CommandButton1_Click()
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=5, Criteria1:=ComboBox1.Value
UserForm1.Hide
End Sub

Private Sub CommandButton2_Click()
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=11, Criteria1:="No WO"
UserForm1.Hide
End Sub

Private Sub UserForm_Initialize()

Dim v, e
With Sheets("FAB").Range("E2:E500")
v = .Value
End With
With CreateObject("scripting.dictionary")
.comparemode = 1
For Each e In v
If Not .exists(e) Then .Add e, Nothing
Next
If .Count Then Me.ComboBox1.List = Application.Transpose(.keys)
End With
End Sub
[/CODE]
 
Bingo.... you nailed it. I had to do the same thing to the customer filter, as that still had the same issue, but seems to functioning as hoped now.
Thank you very much for your time and help, us part timers always struggle on the detail issues.

Code:
Private Sub CommandButton1_Click()
    With ActiveSheet.ListObjects("Table1").AutoFilter
   If .FilterMode Then .ShowAllData
   .Range.AutoFilter 5, ComboBox1.Value
End With

 '   ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=5, Criteria1:=ComboBox1.Value
    UserForm1.Hide
End Sub

Private Sub CommandButton2_Click()
With ActiveSheet.ListObjects("Table1").AutoFilter
   If .FilterMode Then .ShowAllData
   .Range.AutoFilter 11, "No WO"
End With

    BeginRow = 1 'START ROW
    EndRow = 500
    ChkCol = 1

    For RowCnt = BeginRow To EndRow
        If Cells(RowCnt, ChkCol).Value = "HIDE" Then
            Cells(RowCnt, ChkCol).EntireRow.Hidden = True
        End If
    Next RowCnt

UserForm1.Hide
End Sub
 
Upvote 0

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,981
Messages
6,122,566
Members
449,089
Latest member
Motoracer88

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