VBA - hide all rows unless column F contains specific text in cell

strongman86

Board Regular
Joined
Feb 15, 2017
Messages
115
Office Version
  1. 2019
Platform
  1. Windows
Hi Lads,

I have macro below which hides all rows and leaves all rows with text ''APPLE'' in F column on Sheet2, it works ok, but the problem is that in this table all cells are referrencing to different sheet i.e. ='Sheet1'!A1 which means Marco hides all rows not containing APPLE and all the rest of my around 1000 rows with reference formulas in them. I there anything I can add to code so it ignores ''Empty''/Reference cells? Thanks.


HTML:
Sub hidesome()    Dim r As Long    Application.ScreenUpdating = False    ActiveSheet.DisplayPageBreaks = False    r = Range("F" & ActiveSheet.Rows.Count).End(xlUp).Row    For r = 2 To r        Select Case Cells(r, 6)            Case "APPLE"                Rows(r).Hidden = False            Case Else                Rows(r).Hidden = True        End Select    Next rEnd Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
How about
Code:
Sub hidesome()
Application.ScreenUpdating = False
ActiveSheet.DisplayPageBreaks = False
Range("A1:F1").AutoFilter 6, "apple", xlOr, ""

End Sub
 
Upvote 0
Thanks Fluffy. Works great. Couple more thing I would need: add second text ''banana'' if I add to code as I did on below it gives me Run time error. Also How can I make this macro automated so it runs every time workbook is closed.? Thanks.

Code:
Sub hidesome()
Application.ScreenUpdating = False
ActiveSheet.DisplayPageBreaks = False
Range("A1:F1").AutoFilter 6, "apple", "banana", xlOr, ""


End Sub
 
Upvote 0
How about
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Application.ScreenUpdating = False
ActiveSheet.DisplayPageBreaks = False
Range("A1:F1").AutoFilter 6, Array("apple", "banana", ""), xlFilterValues

End Sub
 
Upvote 0
Unfortunately it's not working. When I save & close woorkbook and reopen: filters are not applied.
 
Last edited:
Upvote 0
What sheet do you want it to work on?
 
Upvote 0
Ok, how about
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Application.ScreenUpdating = False
With Sheets("List")
   .DisplayPageBreaks = False
   .Range("A1:F1").AutoFilter 6, Array("apple", "banana", ""), xlFilterValues
End With

End Sub
 
Upvote 0
No still doesn't work.
When I change first row of code and do manually it works OK.

Code:
Sub hidesome()
Application.ScreenUpdating = False
With Sheets("List")
   .DisplayPageBreaks = False
   .Range("A1:F1").AutoFilter 6, Array("apple", "banana", ""), xlFilterValues
End With

End Sub
 
Last edited:
Upvote 0
Did you put the code in the ThisWorkbook module?
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,322
Members
452,635
Latest member
laura12345

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