How can I also filter out empty cell with this line?

Romano_odK

Active Member
Joined
Jun 4, 2020
Messages
380
Office Version
  1. 365
Platform
  1. Windows
Good morning,

I recieved this code last week and it works great. Now I would like it to also filter empty cells. Can this be done?

Thank you for your time.


VBA Code:
Private Sub FilterOK_Click()
  Dim tbl As ListObject
  Dim col As ListColumn
  Dim fnd As Range
 
  Set tbl = ActiveSheet.ListObjects("ItemsImport")
  Set col = tbl.ListColumns("Afwijking%" & Chr(10) & "opslag")
  Set fnd = col.Range.Find("ok", , xlValues, xlWhole, , , False)
 
  Application.ScreenUpdating = False
  If Not fnd Is Nothing Then
    tbl.Range.AutoFilter col.Index, "ok"
    tbl.DataBodyRange.EntireRow.Delete
    ActiveSheet.ShowAllData
  End If
  Application.ScreenUpdating = True
End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Try this:

VBA Code:
Private Sub FilterOK_Click()
  Dim tbl As ListObject
  Dim col As ListColumn
  Dim fnd As Range
  
  Set tbl = ActiveSheet.ListObjects("ItemsImport")
  Set col = tbl.ListColumns("Afwijking%" & Chr(10) & "Opslag")
  Set fnd = col.Range.Find("", , xlValues, xlWhole, , , False)
  
  Application.ScreenUpdating = False
  If Not fnd Is Nothing Then
    tbl.HeaderRowRange.Select
    tbl.Range.AutoFilter col.Index, ""
    tbl.DataBodyRange.EntireRow.Delete
    ActiveSheet.ShowAllData
  End If
  Application.ScreenUpdating = True
End Sub

🧙‍♂️
 
Upvote 0
Now I would like it to also filter empty cells.
Would you want to do ok and blanks at the same time?

VBA Code:
Private Sub FilterOK_Click_v2()
  Dim tbl As ListObject
  Dim col As ListColumn
  Dim fndok As Range, fndblank As Range
  
  Set tbl = ActiveSheet.ListObjects("ItemsImport")
  Set col = tbl.ListColumns("Afwijking%" & Chr(10) & "Opslag")
  Set fndok = col.Range.Find("ok", , xlValues, xlWhole, , , False)
  Set fndblank = col.Range.Find("", , xlValues, xlWhole, , , False)
  
  Application.ScreenUpdating = False
  If Not fndok Is Nothing Or Not fndblank Is Nothing Then
    tbl.HeaderRowRange.Select
    tbl.Range.AutoFilter col.Index, "ok", xlOr, ""
    tbl.DataBodyRange.EntireRow.Delete
    ActiveSheet.ShowAllData
  End If
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
If you want to delete both: empty and ok.

You can use the following:

VBA Code:
Private Sub FilterOK_Click()
  Dim tbl As ListObject
  Dim col As ListColumn
  Dim fnd As Range
  Dim arr As Variant, ar As Variant
  
  arr = Array("", "ok")
  
  Set tbl = ActiveSheet.ListObjects("ItemsImport")
  Set col = tbl.ListColumns("Afwijking%" & Chr(10) & "Opslag")
  
  For Each ar In arr
    Set fnd = col.Range.Find(ar, , xlValues, xlWhole, , , False)
    If Not fnd Is Nothing Then Exit For
  Next
  
  Application.ScreenUpdating = False
  If Not fnd Is Nothing Then
    tbl.HeaderRowRange.Select
    tbl.Range.AutoFilter col.Index, arr, xlFilterValues
    tbl.DataBodyRange.EntireRow.Delete
    ActiveSheet.ShowAllData
  End If
  Application.ScreenUpdating = True
End Sub

This way if you want to add more items you just add them to this line, for example if you want to add closed and canceled ones:

Rich (BB code):
Private Sub FilterOK_Click()
  Dim tbl As ListObject
  Dim col As ListColumn
  Dim fnd As Range
  Dim arr As Variant, ar As Variant
  
  arr = Array("", "Ok", "Closed", "Cancelled")
  
  Set tbl = ActiveSheet.ListObjects("ItemsImport")
  Set col = tbl.ListColumns("Afwijking%" & Chr(10) & "Opslag")
  
  For Each ar In arr
    Set fnd = col.Range.Find(ar, , xlValues, xlWhole, , , False)
    If Not fnd Is Nothing Then Exit For
  Next
  
  Application.ScreenUpdating = False
  If Not fnd Is Nothing Then
    tbl.HeaderRowRange.Select
    tbl.Range.AutoFilter col.Index, arr, xlFilterValues
    tbl.DataBodyRange.EntireRow.Delete
    ActiveSheet.ShowAllData
  End If
  Application.ScreenUpdating = True
End Sub

😇
 
Upvote 0
Solution
If you want to delete both: empty and ok.

You can use the following:

VBA Code:
Private Sub FilterOK_Click()
  Dim tbl As ListObject
  Dim col As ListColumn
  Dim fnd As Range
  Dim arr As Variant, ar As Variant
 
  arr = Array("", "ok")
 
  Set tbl = ActiveSheet.ListObjects("ItemsImport")
  Set col = tbl.ListColumns("Afwijking%" & Chr(10) & "Opslag")
 
  For Each ar In arr
    Set fnd = col.Range.Find(ar, , xlValues, xlWhole, , , False)
    If Not fnd Is Nothing Then Exit For
  Next
 
  Application.ScreenUpdating = False
  If Not fnd Is Nothing Then
    tbl.HeaderRowRange.Select
    tbl.Range.AutoFilter col.Index, arr, xlFilterValues
    tbl.DataBodyRange.EntireRow.Delete
    ActiveSheet.ShowAllData
  End If
  Application.ScreenUpdating = True
End Sub

This way if you want to add more items you just add them to this line, for example if you want to add closed and canceled ones:

Rich (BB code):
Private Sub FilterOK_Click()
  Dim tbl As ListObject
  Dim col As ListColumn
  Dim fnd As Range
  Dim arr As Variant, ar As Variant
 
  arr = Array("", "Ok", "Closed", "Cancelled")
 
  Set tbl = ActiveSheet.ListObjects("ItemsImport")
  Set col = tbl.ListColumns("Afwijking%" & Chr(10) & "Opslag")
 
  For Each ar In arr
    Set fnd = col.Range.Find(ar, , xlValues, xlWhole, , , False)
    If Not fnd Is Nothing Then Exit For
  Next
 
  Application.ScreenUpdating = False
  If Not fnd Is Nothing Then
    tbl.HeaderRowRange.Select
    tbl.Range.AutoFilter col.Index, arr, xlFilterValues
    tbl.DataBodyRange.EntireRow.Delete
    ActiveSheet.ShowAllData
  End If
  Application.ScreenUpdating = True
End Sub

😇
Thank you that is what I needed. Have a great day.
 
Upvote 0

Forum statistics

Threads
1,223,240
Messages
6,170,951
Members
452,368
Latest member
jayp2104

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