VBA to automatically reapply filter

ChrisTag1

New Member
Joined
Jan 13, 2020
Messages
11
Office Version
  1. 2016
Platform
  1. Windows
I have a table that pulls data from another range based on a dropdown from another worksheet. I have tried VBA's to clear the blank rows based on a cell value and can't seem to get them to work. (I think it's because of the formula in the cells - {=IFERROR(INDEX(J$20:J$117,SMALL(IF($I$20:$I$117=DWOR!$G$71,IF($I$20:$I$117<>"",ROW(J$20:J$117)-ROW(J$20)+1)),ROWS(J$20:$J20))),"")} )
So instead of doing that, I thought I would try just filtering the blanks out, but I need it to automatically reapply the filter when the table data changes because this worksheet is going to be hidden eventually. I have tried a few different codes but none seem to work. I have never really used VBA as I only started using and learning excel in the last couple months.
1.) Private Sub Worksheet_Change(ByVal Target As Range)
Sheets("Sheet3").AutoFilter.ApplyFilter
End Sub


2.) Sub ReapplyFilter()
ActiveSheet.AutoFilter.ApplyFilter
End Sub

3.) Private Sub Worksheet_Change(ByVal Target As Range)
If Me.FilterMode = True Then
With Application
.EnableEvents = False
.ScreenUpdating = False
End With

With ActiveWorkbook
.CustomViews.Add ViewName:="Mine", RowColSettings:=True
Me.AutoFilterMode = False
.CustomViews("Mine").Show
.CustomViews("Mine").Delete
End With
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
End If

End Sub


This last code, I don't understand any of it, so I can't even edit it to correspond with my own worksheet, if I even have to.
What I am doing is right clicking on my worksheet name, selecting view code, pasting the code in the screen, saving and then going back to excel. Maybe I am putting the codes in wrong, I have no idea.
Does anyone have an idea why these aren't working? My worksheet name is "Downtime Summary Primary HIDE", which I put into the first code. The second code seems pretty straightforward, no editing needed that I can see, yet still doesn't work.
Please help the not so smart newbie.


 

Attachments

  • Equiptable.png
    Equiptable.png
    24 KB · Views: 113

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
If the data is changing by formula it needs to be in either a Worksheet_Calculate or a Workbook_SheetCalculate event, it won't trigger by a Worksheet_Change event.
 
Upvote 0
I changed code to "Worksheet_Calculate". It is still only half working. It isn't unhiding any rows with data in it, when the data changes. First screenshot is 4 rows of data. Second is 1 row of data, and 3rd should be 4 rows of data, but it's only showing 1.

Another idea I had was to use a table to not show blanks. But I will need a code to automatically reset the table every time data is added or removed. I found some codes for that, but have yet to make them work either.
 

Attachments

  • equiptable4.png
    equiptable4.png
    132.8 KB · Views: 244
  • equiptable5.png
    equiptable5.png
    104 KB · Views: 243
  • equiptable6.png
    equiptable6.png
    116 KB · Views: 245
Upvote 0
Hi all,
I was able to get this working. I formatted as a table and ran a code that automatically reapplies my filter every time data is changed. My problem was that I was putting the code into the wrong worksheet. I had to put the code into the worksheet with my dropdown list and not into the worksheet with the table.

Private Sub Worksheet_Change(ByVal Target As Range)
With ActiveWorkbook.Worksheets("Downtime Summary Primary").ListObjects("Table6")
.AutoFilter.ApplyFilter
End With
End Sub


Thanks for everyones suggestions
 
Upvote 0

Forum statistics

Threads
1,214,667
Messages
6,120,815
Members
448,990
Latest member
rohitsomani

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