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

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
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:

Rows:Column DColumn E
170Joe
171
172Bob
173
174
175Sam
176
177
178
179
180Tom
181
182
183
184

<tbody>
</tbody>

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,216,127
Messages
6,129,024
Members
449,482
Latest member
al mugheen

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