Hide entire rows based on specific value in cells across rows and columns on multiple sheets

ExceLoki

Well-known Member
Joined
Dec 13, 2021
Messages
538
Office Version
  1. 365
Platform
  1. Windows
i have a workbook that contains many sheets that pull values from an input sheet. some of the formulas on many sheets say "HIDE ROW" if the input does not work for those sections.
i am trying to come up with vba to hide entire rows if anywhere in the row has the shown value "HIDE ROW". this goes across several sheets, rows, and columns.
any help is apperciated!
 

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
Welcome to the MrExcel board!

Try this with a copy of your workbook.

VBA Code:
Sub Hide_Rows()
  Dim ws As Worksheet
  Dim rFound As Range
  
  Application.ScreenUpdating = False
  For Each ws In Worksheets
    ws.Cells.EntireRow.Hidden = False
    With ws.UsedRange
      Set rFound = .Find(What:="HIDE ROW", LookIn:=xlValues, MatchCase:=False)
      If Not rFound Is Nothing Then
        Do
          rFound.EntireRow.Hidden = True
          Set rFound = .Find(What:="HIDE ROW", LookIn:=xlValues, MatchCase:=False)
        Loop Until rFound Is Nothing
      End If
    End With
  Next ws
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
thanks for the welcome!

i get an error when running this. the bold/underlined line is the highlighted part when debug
1639487485887.png


Sub HideRows2()

Dim ws As Worksheet
Dim rFound As Range

Application.ScreenUpdating = False
For Each ws In Worksheets
ws.Cells.EntireRow.Hidden = False
With ws.UsedRange
Set rFound = .Find(What:="HIDE ROW", LookIn:=xlValues, MatchCase:=False)
If Not rFound Is Nothing Then
Do
rFound.EntireRow.Hidden = True
Set rFound = .Find(What:="HIDE ROW", LookIn:=xlValues, MatchCase:=False)
Loop Until rFound Is Nothing
End If
End With
Next ws
Application.ScreenUpdating = True
End Sub
 
Upvote 0
here are a few examples of the sheets i'm using
-----------
HIDE ROW example.xlsm
ABCDEFGHIJKLMNOPQRSTUVW
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
25
26
27
28
32
33
34
35
36
37
38
39N/AN/A
40HIDE ROWHIDE ROW
41HIDE ROWHIDE ROW
42For married participants, the qualified joint and survivor annuity is the 50% Joint and Survivor Annuity.
43
44
45
46
47
48
49
50
51
52
53
54
55
56
1

----------------------
HIDE ROW example.xlsm
ABCDEFGHIJKLMNOPQRSTUVW
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
25
26
27
28
32
33
34
35
36
37
38
39N/AN/A
40HIDE ROWHIDE ROWHIDE ROW
41HIDE ROWHIDE ROW
42HIDE ROW
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
4

-------------
HIDE ROW example.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAAB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30N/AN/A
31
34HIDE ROWHIDE ROWHIDE ROW
35
36HIDE ROWHIDE ROW
37
40N/AHIDE ROWN/A
41
42
43
44
45
46
47
48
49
6

-------------
HIDE ROW example.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAAB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18·HIDE ROW
19
20
21
22
23
24
25
26
27
28
29
30N/AN/A
31
34HIDE ROWHIDE ROWHIDE ROW
35
36HIDE ROWHIDE ROW
37
40N/AHIDE ROWN/A
41
42HIDE ROWS
43
44
45
46
47
48
49
50
51
52
53
7
 
Upvote 0
here are a few examples of the sheets i'm using
Thanks for the samples which clearly show the issue. Merged cells and vba often do not sit well together.

However, you could try this version for your circumstances.

VBA Code:
Sub Hide_Rows_v2()
  Dim ws As Worksheet
  Dim rFound As Range
 
  Application.ScreenUpdating = False
  For Each ws In Worksheets
    With ws.UsedRange
      Set rFound = .Find(What:="HIDE ROW", LookIn:=xlValues, MatchCase:=False)
      If Not rFound Is Nothing Then
        Do
          rFound.MergeArea.EntireRow.Hidden = True
          Set rFound = .Find(What:="HIDE ROW", LookIn:=xlValues, MatchCase:=False)
        Loop Until rFound Is Nothing
      End If
    End With
  Next ws
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
that worked wonderfully! thank you!

one more question, is there an easy way to unhide those rows and rest everything?
 
Upvote 0
.. and rest everything
I don't know what you mean by that but this should unhide all rows on all worksheets.

VBA Code:
Sub Unhide_Rows()
  Dim ws As Worksheet
  
  For Each ws In Worksheets
    ws.Cells.EntireRow.Hidden = False
  Next ws
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,001
Messages
6,122,648
Members
449,092
Latest member
peppernaut

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