Filter and delete rows that are visible.

Romano_odK

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

A while ago I go this from Peter. This works perfect in deleting the rows that are not visible. How do I change this so it will filter values that are visible?

Thank you for your time.

Romano

VBA Code:
Private Sub CommandButton9_Click()
  Application.EnableEvents = False
  Application.ScreenUpdating = False
  With ActiveSheet.ListObjects(1)
    .ListColumns.Add Position:=2
    On Error Resume Next
    .ListColumns(2).DataBodyRange.SpecialCells(xlVisible).Value = 1
    On Error GoTo 0
    If .Parent.FilterMode Then .Parent.ShowAllData
    .Sort.SortFields.Clear
    .Sort.SortFields.Add2 Key:=.ListColumns(2).DataBodyRange, SortOn:=xlSortOnValues, Order:=xlAscending
    .Sort.Apply
    On Error Resume Next
    .ListColumns(2).DataBodyRange.SpecialCells(xlBlanks).EntireRow.Delete
    On Error GoTo 0
    .ListColumns(2).Delete
  End With
  Application.ScreenUpdating = True
  Application.EnableEvents = False
End Sub
 

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
You could use something like this:

VBA Code:
Private Sub CommandButton9_Click()
   Application.EnableEvents = False
   Application.ScreenUpdating = False
   With ActiveSheet.ListObjects(1)
      Dim delRange As Range
      On Error Resume Next
      Set delRange = Intersect(.DataBodyRange, .DataBodyRange.Columns(1).SpecialCells(xlCellTypeVisible).EntireRow)
      On Error GoTo 0
      .AutoFilter.ShowAllData
   End With
   If Not delRange Is Nothing Then delRange.Delete
   Application.ScreenUpdating = True
   Application.EnableEvents = False
End Sub
 
Upvote 0
Solution
You could use something like this:

VBA Code:
Private Sub CommandButton9_Click()
   Application.EnableEvents = False
   Application.ScreenUpdating = False
   With ActiveSheet.ListObjects(1)
      Dim delRange As Range
      On Error Resume Next
      Set delRange = Intersect(.DataBodyRange, .DataBodyRange.Columns(1).SpecialCells(xlCellTypeVisible).EntireRow)
      On Error GoTo 0
      .AutoFilter.ShowAllData
   End With
   If Not delRange Is Nothing Then delRange.Delete
   Application.ScreenUpdating = True
   Application.EnableEvents = False
End Sub
This indeed works, thank you and have a nice day.
 
Upvote 0

Forum statistics

Threads
1,215,597
Messages
6,125,738
Members
449,255
Latest member
whatdoido

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