Flitering Macro

Ziro54

New Member
Joined
Aug 3, 2014
Messages
24
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
Afternoon

I am using a macro to filter several arguments.

Using 4 cells to define my filters. But if I leave any of them blank, the filter returns with no results.

How do I write the macro to ignore if the cell is left bank?

Code:
Sub Filter()'
' Filter Macro
'


'
    ActiveSheet.Range("$A$3:$FM$301").AutoFilter Field:=8, Criteria1:= _
        Range("$D$2")
    ActiveSheet.Range("$A$3:$FM$301").AutoFilter Field:=11, Criteria1:= _
        Range("$E$2")
    ActiveSheet.Range("$A$3:$FM$301").AutoFilter Field:=10, Criteria1:= _
        Range("$F$2")
    ActiveSheet.Range("$A$3:$FM$301").AutoFilter Field:=12, Criteria1:= _
        Range("$G$2")
     
        
End Sub


Thank you in advance.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Maybe try putting each statement within an IF statement, i.e.
change:
Code:
    ActiveSheet.Range("$A$3:$FM$301").AutoFilter Field:=8, Criteria1:= _
        Range("$D$2")
to
Code:
    [COLOR=#ff0000]If Range("$D$2")<>"" Then [/COLOR]ActiveSheet.Range("$A$3:$FM$301").AutoFilter Field:=8, Criteria1:= _
        Range("$D$2")
and repeat the same logic for the other three statements.
 
Upvote 0
Joe4's solution is probably the best. I wrapped it in multiple case statements ....probably not the best idea

Code:
Sub Filter() '


Select Case False


Case IsEmpty(Range("D2").Value)
    ActiveSheet.Range("$A$3:$FM$301").AutoFilter Field:=8, Criteria1:= _
        Range("$D$2")
        
End Select


Select Case False
Case IsEmpty(Range("E2").Value)
    ActiveSheet.Range("$A$3:$FM$301").AutoFilter Field:=11, Criteria1:= _
        Range("$E$2")
        
        
End Select
Select Case False
Case IsEmpty(Range("F2").Value)
    ActiveSheet.Range("$A$3:$FM$301").AutoFilter Field:=10, Criteria1:= _
        Range("$F$2")
        
End Select
Select Case False
Case IsEmpty(Range("G2").Value)
    ActiveSheet.Range("$A$3:$FM$301").AutoFilter Field:=12, Criteria1:= _
        Range("$G$2")
End Select


End Sub
 
Upvote 0
untested but try

Code:
Sub Filter() '
' Filter Macro
'
    Dim cell As Range
    Dim i As Integer


'
    For Each cell In Range("D2,E2,F2,G2").Cells
        i = i + 1
        If Len(cell.Value) > 0 Then
            Range("$A$3:$FM$301").AutoFilter Field:=Choose(i, 8, 11, 10, 12), Criteria1:=cell.Value
        End If
    Next cell
End Sub

Dave
 
Upvote 0
Cross posted https://www.excelforum.com/excel-programming-vba-macros/1250318-skip-blanks.html

While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules).
This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 
Upvote 0
Thank you very much. Works perfectly!!!!


Joe4's solution is probably the best. I wrapped it in multiple case statements ....probably not the best idea

Code:
Sub Filter() '


Select Case False


Case IsEmpty(Range("D2").Value)
    ActiveSheet.Range("$A$3:$FM$301").AutoFilter Field:=8, Criteria1:= _
        Range("$D$2")
        
End Select


Select Case False
Case IsEmpty(Range("E2").Value)
    ActiveSheet.Range("$A$3:$FM$301").AutoFilter Field:=11, Criteria1:= _
        Range("$E$2")
        
        
End Select
Select Case False
Case IsEmpty(Range("F2").Value)
    ActiveSheet.Range("$A$3:$FM$301").AutoFilter Field:=10, Criteria1:= _
        Range("$F$2")
        
End Select
Select Case False
Case IsEmpty(Range("G2").Value)
    ActiveSheet.Range("$A$3:$FM$301").AutoFilter Field:=12, Criteria1:= _
        Range("$G$2")
End Select


End Sub
 
Upvote 0
Note that Case statements are great when you have multiple options that each value can be equal to, but are a bit overkill when you are just checking for one value.
If you had followed the methodology I suggested (or the one dmt32) suggested, your code would be substantially shorter.
Code:
Sub Filter() '
' Filter Macro
'

    If Range("$D$2") <> "" Then ActiveSheet.Range("$A$3:$FM$301").AutoFilter _
        Field:=8, Criteria1:=Range("$D$2")
    If Range("$E$2") <> "" Then ActiveSheet.Range("$A$3:$FM$301").AutoFilter _
        Field:=11, Criteria1:=Range("$E$2")
    If Range("$F$2") <> "" Then ActiveSheet.Range("$A$3:$FM$301").AutoFilter _
        Field:=10, Criteria1:=Range("$F$2")
    If Range("$G$2") <> "" Then ActiveSheet.Range("$A$3:$FM$301").AutoFilter _
        Field:=12, Criteria1:=Range("$G$2")
     
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,957
Messages
6,122,472
Members
449,087
Latest member
RExcelSearch

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