VBA - Filter Out Blanks from Selected Column Macro

kirkjd

New Member
Joined
Jan 31, 2014
Messages
13
Hi All,

For years I have been struggling to come up with a macro to filter out blanks in a set of filtered data (rather than clicking on the filter button and scrolling down to (blanks) and unchecking the box).

Any ideas?

Thanks!
 
The following works for me:
Code:
Sub Version3()
    ActiveCell.CurrentRegion.AutoFilter (ActiveCell.Column - ActiveCell.CurrentRegion.Column) + 1, "<>"
End Sub

Pete

Would it be possible to modify this code so that rather than filtering blanks out of the selected column, it filters the value you currently have selected out of the selected column?
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Replying from non-Excel tablet, so will give this a look at work in the morning!
 
Upvote 0
Would it be possible to modify this code so that rather than filtering blanks out of the selected column, it filters the value you currently have selected out of the selected column?

I've actually sorted out a simple solution for this:
Code:
Sub FilterOutCurrent()
    ActiveCell.CurrentRegion.AutoFilter (ActiveCell.Column - ActiveCell.CurrentRegion.Column) + 1, "<>" & ActiveCell.Value
End Sub
 
Upvote 0
You read my mind - here are the four permutations that you might need to use:
Code:
Sub DynamicFilter()
    
    'Filter the database on the field containing the ActiveCell to display rows where the value in the ActiveCell's column is BLANK
    ActiveCell.CurrentRegion.AutoFilter (ActiveCell.Column - ActiveCell.CurrentRegion.Column) + 1, Criteria1:="="
    
    'Filter the database on the field containing the ActiveCell to display rows where the value in the ActiveCell's column is NONBLANK
    ActiveCell.CurrentRegion.AutoFilter (ActiveCell.Column - ActiveCell.CurrentRegion.Column) + 1, Criteria1:="<>"
    
    'Filter the database on the field containing the ActiveCell to display rows where the value in the ActiveCell's column is EQUAL TO the value in the ActiveCell
    ActiveCell.CurrentRegion.AutoFilter (ActiveCell.Column - ActiveCell.CurrentRegion.Column) + 1, Criteria1:="=" & ActiveCell.Value
    
    'Filter the database on the field containing the ActiveCell to display rows where the value in the ActiveCell's column is NOT EQUAL TO the value in the ActiveCell
    ActiveCell.CurrentRegion.AutoFilter (ActiveCell.Column - ActiveCell.CurrentRegion.Column) + 1, Criteria1:="<>" & ActiveCell.Value


End Sub

Cheers

Pete
 
Upvote 0
...and here are all the other possible variations included in my earlier post:
Code:
Sub DynamicFilter()
    
    'Filter the database on the field containing the ActiveCell to display rows where the value in the ActiveCell's column is BLANK
    ActiveCell.CurrentRegion.AutoFilter ((ActiveCell.Column - ActiveCell.CurrentRegion.Column) + 1), Criteria1:="="

    'Filter the database on the field containing the ActiveCell to display rows where the value in the ActiveCell's column is NONBLANK
    ActiveCell.CurrentRegion.AutoFilter ((ActiveCell.Column - ActiveCell.CurrentRegion.Column) + 1), Criteria1:="<>"

    'Filter the database on the field containing the ActiveCell to display rows where the value in the ActiveCell's column IS EQUAL TO the value in the ActiveCell
    ActiveCell.CurrentRegion.AutoFilter ((ActiveCell.Column - ActiveCell.CurrentRegion.Column) + 1), Criteria1:="=" & ActiveCell.Value

    'Filter the database on the field containing the ActiveCell to display rows where the value in the ActiveCell's column IS NOT EQUAL TO the value in the ActiveCell
    ActiveCell.CurrentRegion.AutoFilter ((ActiveCell.Column - ActiveCell.CurrentRegion.Column) + 1), Criteria1:="<>" & ActiveCell.Value

    'Filter the database on the field containing the ActiveCell to display rows where the value in the ActiveCell's column IS EQUAL TO the value "Cat"
    ActiveCell.CurrentRegion.AutoFilter ((ActiveCell.Column - ActiveCell.CurrentRegion.Column) + 1), Criteria1:="=Cat"

    'Filter the database on the field containing the ActiveCell to display rows where the value in the ActiveCell's column IS NOT EQUAL TO the value "Cat"
    ActiveCell.CurrentRegion.AutoFilter ((ActiveCell.Column - ActiveCell.CurrentRegion.Column) + 1), Criteria1:="<>Cat"

    'Filter the database on the field containing the ActiveCell to display rows where the value in the ActiveCell's column BEGINS WITH the value "Cat"
    ActiveCell.CurrentRegion.AutoFilter ((ActiveCell.Column - ActiveCell.CurrentRegion.Column) + 1), Criteria1:="Cat*"

    'Filter the database on the field containing the ActiveCell to display rows where the value in the ActiveCell's column DOES NOT BEGIN WITH the value "Cat"
    ActiveCell.CurrentRegion.AutoFilter ((ActiveCell.Column - ActiveCell.CurrentRegion.Column) + 1), Criteria1:="<>Cat*"

    'Filter the database on the field containing the ActiveCell to display rows where the value in the ActiveCell's column ENDS WITH the value "Cat"
    ActiveCell.CurrentRegion.AutoFilter ((ActiveCell.Column - ActiveCell.CurrentRegion.Column) + 1), Criteria1:="=*Cat"

    'Filter the database on the field containing the ActiveCell to display rows where the value in the ActiveCell's column DOES NOT END WITH the value "Cat"
    ActiveCell.CurrentRegion.AutoFilter ((ActiveCell.Column - ActiveCell.CurrentRegion.Column) + 1), Criteria1:="<>*Cat"

    'Filter the database on the field containing the ActiveCell to display rows where the value in the ActiveCell's column CONTAINS the value "Cat"
    ActiveCell.CurrentRegion.AutoFilter ((ActiveCell.Column - ActiveCell.CurrentRegion.Column) + 1), Criteria1:="=*Cat*"

    'Filter the database on the field containing the ActiveCell to display rows where the value in the ActiveCell's column is DOES NOT CONTAIN the value "Cat"
    ActiveCell.CurrentRegion.AutoFilter ((ActiveCell.Column - ActiveCell.CurrentRegion.Column) + 1), Criteria1:="<>*Cat*"

End Sub
 
Upvote 0

Forum statistics

Threads
1,216,086
Messages
6,128,736
Members
449,466
Latest member
Peter Juhnke

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