Hide & unhide rows

timesareGMT

New Member
Joined
Aug 19, 2011
Messages
26
Hi all,

I create a calculation page on which the final result displays in cell D21.

How can I set up a macro to:
1. If D21 < 0.5, hide rows 22 to 28.
2. If D21 > 0.5, unhide rows 22 to 28.

And, how can I prevent the assigned macro from being stopped running when I password protect the spreadsheet?

Thanks a lot.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
What happens if the value is exactly 0.5?

Here is some VBA that should do the trick, assuming the workbook protection is not password protected, and that rows should be displayed if value is exactly 0.5...

Code:
Sub hiderows()
ActiveWorkbook.Unprotect
Range("D22:D28").Select
If Range("D21").Value < 0.5 Then
    Selection.EntireRow.Hidden = True
Else
    Selection.EntireRow.Hidden = False
End If
ActiveWorkbook.Protect Structure:=True, Windows:=False
End Sub
 
Upvote 0
What happens if the value is exactly 0.5?

Here is some VBA that should do the trick, assuming the workbook protection is not password protected, and that rows should be displayed if value is exactly 0.5...

Code:
Sub hiderows()
ActiveWorkbook.Unprotect
Range("D22:D28").Select
If Range("D21").Value < 0.5 Then
    Selection.EntireRow.Hidden = True
Else
    Selection.EntireRow.Hidden = False
End If
ActiveWorkbook.Protect Structure:=True, Windows:=False
End Sub

Thanks for the info.

But how can I make it run automatically without manually pressing ALT+F8?

And again, how to get it work under the password protection condition?

Thanks.
 
Upvote 0
Thanks for the info.

But how can I make it run automatically without manually pressing ALT+F8?

And again, how to get it work under the password protection condition?

Thanks.

To run automatically depends on the trigger. Do you want it to run when the sheet is activated or when the value in D21 is changed? Is D21 a reference to another cell, or is the value of D21 entered/updated directly in the cell?
 
Upvote 0
To run automatically depends on the trigger. Do you want it to run when the sheet is activated or when the value in D21 is changed? Is D21 a reference to another cell, or is the value of D21 entered/updated directly in the cell?

Thanks for your reply.

I want the macro run automatically once D21 has been changed.

The value of D21 is an outcome of sequences of calculations being presented in terms of a numerical value (e.g. 0.123)

Thanks.
 
Upvote 0
Thanks for your reply.

I want the macro run automatically once D21 has been changed.

The value of D21 is an outcome of sequences of calculations being presented in terms of a numerical value (e.g. 0.123)

Thanks.

This code should be placed in the worksheet module:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$D$21" Then
    Range("D22:D28").Select
    If Range("D21").Value < 0.5 Then
        Selection.EntireRow.Hidden = True
    Else
        Selection.EntireRow.Hidden = False
    End If
End If
End Sub

Are you password protecting the individual worksheet, or the entire workbook?
 
Upvote 0
right click the tab, choose 'view code' and paste in the following:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    On Error GoTo safeExit
    If Target.Address = "$D$21" Then
        Application.EnableEvents = False
        Range("D22:D28").EntireRow.Hidden = (Target.Value > 0.5)
    End If
safeExit:
    Application.EnableEvents = True
End Sub
This should hide/unhide the rows when the value changes
 
Upvote 0
This code should be placed in the worksheet module:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$D$21" Then
    Range("D22:D28").Select
    If Range("D21").Value < 0.5 Then
        Selection.EntireRow.Hidden = True
    Else
        Selection.EntireRow.Hidden = False
    End If
End If
End Sub

Are you password protecting the individual worksheet, or the entire workbook?
Dangre, you're going to have problems with this version as hiding/unhiding the rows counts as a worksheet change, so the code will end up calling itself into an infinite loop
 
Upvote 0
Dangre, you're going to have problems with this version as hiding/unhiding the rows counts as a worksheet change, so the code will end up calling itself into an infinite loop

The code does not execute unless the change is made to cell D21 explicitly.
 
Upvote 0
right click the tab, choose 'view code' and paste in the following:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    On Error GoTo safeExit
    If Target.Address = "$D$21" Then
        Application.EnableEvents = False
        Range("D22:D28").EntireRow.Hidden = (Target.Value > 0.5)
    End If
safeExit:
    Application.EnableEvents = True
End Sub
This should hide/unhide the rows when the value changes


Thanks for your info.

But have I missed something? The automatic hiding/unhiding function was not working.

Your further guidance is sought.

Thanks.
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,836
Members
452,947
Latest member
Gerry_F

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