Unreliable Autofilter with multiple IF statements

skull_eagle

Board Regular
Joined
Mar 25, 2011
Messages
89
Hi,

I'm trying to filter a sheet using a userform, the user clicks a checkbox on the userform and the filter is applied immediately.

It was working fine until a number of columns need to be filtered with the word "Valid" instead of just not blank.

If the Checkbox is selected, check if that column already has a filter applied, if it does then clear the filter, If it doesn't apply the filter.
One filter should add the another filter, it should not clear it.

I'm hoping someone smarter than me can simplify this and make it reliable.

This is in a Class Module

VBA Code:
Private Sub mLabelGroup_Click()

Dim ControlName, ColNum
Dim Rowz As Long
Dim wsRD As Worksheet
    Set wsRD = Worksheets("RawData")
    Set wsRD = ActiveSheet

'On Error Resume Next

ControlName = mLabelGroup.Name

ColNum = Replace(ControlName, "CB_", "")

          
If wsRD.AutoFilter.Filters(ColNum).On Then
  
    ActiveSheet.ListObjects("Table_RawData").Range.AutoFilter Field:=ColNum
        
Else
    
    ActiveSheet.ListObjects("Table_RawData").Range.AutoFilter Field:=ColNum, _
    Criteria1:="<>"
                
End If
          
If ColNum = 24 Then
    
    If wsRD.AutoFilter.Filters(ColNum).On Then
  
    ActiveSheet.ListObjects("Table_RawData").Range.AutoFilter Field:=ColNum
        
Else
    
    ActiveSheet.ListObjects("Table_RawData").Range.AutoFilter Field:=ColNum, _
    Criteria1:="Valid"
        
End If
End If
  
If ColNum = 27 Then
    
    If wsRD.AutoFilter.Filters(ColNum).On Then
  
    ActiveSheet.ListObjects("Table_RawData").Range.AutoFilter Field:=ColNum
        
Else
    
    ActiveSheet.ListObjects("Table_RawData").Range.AutoFilter Field:=ColNum, _
    Criteria1:="Valid"
    
 End If
 End If
 
If ColNum = 29 Then
    
    If wsRD.AutoFilter.Filters(ColNum).On Then
  
    ActiveSheet.ListObjects("Table_RawData").Range.AutoFilter Field:=ColNum
        
Else
    
    ActiveSheet.ListObjects("Table_RawData").Range.AutoFilter Field:=ColNum, _
    Criteria1:="Valid"
    
 End If
 End If
 
If ColNum = 65 Then
    
    If wsRD.AutoFilter.Filters(ColNum).On Then
  
    ActiveSheet.ListObjects("Table_RawData").Range.AutoFilter Field:=ColNum
        
Else
    
    ActiveSheet.ListObjects("Table_RawData").Range.AutoFilter Field:=ColNum, _
    Criteria1:="Valid"
    
End If
End If

If ColNum = 69 Then
    
    If wsRD.AutoFilter.Filters(ColNum).On Then
  
    ActiveSheet.ListObjects("Table_RawData").Range.AutoFilter Field:=ColNum
        
Else
    
    ActiveSheet.ListObjects("Table_RawData").Range.AutoFilter Field:=ColNum, _
    Criteria1:="Valid"
        
End If
End If
    

    
Rowz = wsRD.AutoFilter.Range.Columns(1).SpecialCells(xlCellTypeVisible).Cells.Count - 1
    
Search_Form.Controls("Results_1").Caption = Rowz & " Results"

    
End Sub
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Worked it out, Thanks
I had my Else statements back to front. I knew it was going to be an easy fix, took way to much brain power to find it.
 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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