Auto Hide Rows with VBA

andrewb90

Well-known Member
Joined
Dec 16, 2009
Messages
1,077
Hello All,

I have the following code that I have been using successfully for a while now:

Code:
Private Sub SchedulerPrep()    Rows("88:207").Select
    Selection.EntireRow.Hidden = True
    Rows("164:196").Select
    Selection.EntireRow.Hidden = False
    Rows("164:196").Hidden = False
    For r = 164 To 196
        If r <> 164 And r <> 193 And r <> 194 Then
            If Range("D" & r).Value = "" Then Rows(r).Hidden = True
        End If
    Next r
    Range("A1").Select
End Sub

Now, however I have made a few chances to my sheet, and I can't quite figure out how to get the code to match. For every row that has a value in Column D I need that row and the row immediately below to be visible.

Any help would be greatly appreciated.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
you already have

Rows("164:196").Hidden = False

visible

Do you mean if the cell in "D" is NOT empty to hide it ???
Code:
Private Sub SchedulerPrep()
    Rows("88:207").Hidden = True
    Rows("164:196").Hidden = False
    For r = 164 To 196
        If r <> 164 And r <> 193 And r <> 194 Then
            If Range("D" & r).Value = "" Then Rows(r & ":" & r + 1).Hidden = True
            End If
    Next r
    Range("A1").Select
End Sub
 
Upvote 0
No, in the range 164:196 I need all rows that do not have a value in "D" to be hidden, however any row that has a value in D needs the row below it to stay visible.

Heres an example:

[TABLE="class: grid, width: 500, align: center"]
<tbody>[TR]
[TD]Rows:[/TD]
[TD]Column D[/TD]
[TD]Column E[/TD]
[/TR]
[TR]
[TD]170[/TD]
[TD]Joe[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]171[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]172[/TD]
[TD]Bob[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]173[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]174[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]175[/TD]
[TD]Sam[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]176[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]177[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]178[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]179[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]180[/TD]
[TD]Tom[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]181[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]182[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]183[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]184[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

Using this example chart rows 170,171,172,173,175,176,180,& 181 would be visible. Four would be visible because of the values in column D and the other four are because they are in the row directly below.
The remaining rows would stay hidden.

The code is written opposite really, It says that blank cells will be hidden, which was working until I added another condition to make a row remain visible.
 
Last edited:
Upvote 0
ok try..
Code:
Private Sub SchedulerPrep()
 Dim r As Long
 Rows("88:207").Hidden = True
    For r = 196 To 164 Step -1
        If r <> 164 And r <> 193 And r <> 194 Then
            If Range("D" & r).Value <> "" Then Rows(r & ":" & r + 1).Hidden = False
            End If
    Next r
    Range("A1").Select
End Sub
 
Upvote 0
No, in the range 164:196 I need all rows that do not have a value in "D" to be hidden, however any row that has a value in D needs the row below it to stay visible.
Assuming the values in column D are constants, not formula results, try
Code:
Sub Hide_Show()
  Dim c As Range
  
  Rows("164:196").Hidden = True
  For Each c In Range("D164:D196").SpecialCells(xlConstants)
    c.Resize(2).EntireRow.Hidden = False
  Next c
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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