Hide/unhide a number of cells and columns depends of two values

otrava18

Board Regular
Joined
Feb 11, 2018
Messages
61
Hello all,

I have a challenge. I want to do a table that changes its structure. Is about a block of flats. I need to modify the number of stairway and the number of floors (biggest). Here i received a code to hide/unhide the number of stairway. If i have 3 stairways (based on a cell value "A1") i hide columns from stairway 4 to 8. I want to know if i could do in the same time with the number of rows (floors) based on "A2" value to hide/unhide from row 2 to 15.The code to hide/unhide a number of columns is:


Code:
Private Sub Worksheet_Change(ByVal Target As Range)  If Target.Address(0, 0) = "C3" Then
    If Target.Value > 0 And Target.Value < 9 Then
      Columns("E:K").Hidden = False
      If Target.Value < 8 Then Range(Columns("D").Offset(, Target.Value), "K:K").EntireColumn.Hidden = True
    End If
  End If
End Sub


Thanks to Rick Rothstein.
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Change the "A2" input cell to suit

Code:
[color=darkblue]Private[/color] [color=darkblue]Sub[/color] Worksheet_Change([color=darkblue]ByVal[/color] Target [color=darkblue]As[/color] Range)
    [color=darkblue]If[/color] Target.Address(0, 0) = "C3" [color=darkblue]Then[/color]
        [color=darkblue]If[/color] Target.Value > 0 And Target.Value < 9 [color=darkblue]Then[/color]
            Columns("E:K").Hidden = [color=darkblue]False[/color]
            [color=darkblue]If[/color] Target.Value < 8 [color=darkblue]Then[/color] Range(Columns("D").Offset(, Target.Value), "K:K").EntireColumn.Hidden = [color=darkblue]True[/color]
        [color=darkblue]End[/color] [color=darkblue]If[/color]
[B]    [color=darkblue]ElseIf[/color] Target.Address(0, 0) = "A2" [color=darkblue]Then[/color][/B]
[B]        [color=darkblue]If[/color] Target.Value > 0 And Target.Value <= 14 [color=darkblue]Then[/color][/B]
[B]            Application.ScreenUpdating = [color=darkblue]False[/color][/B]
[B]            Rows("2:15").Hidden = [color=darkblue]True[/color][/B]
[B]            [color=darkblue]If[/color] Target.Value > 0 [color=darkblue]Then[/color] Rows(2).Resize(Target.Value).Hidden = [color=darkblue]False[/color][/B]
[B]            Application.ScreenUpdating = [color=darkblue]True[/color][/B]
[B]        [color=darkblue]End[/color] [color=darkblue]If[/color][/B]
    [color=darkblue]End[/color] [color=darkblue]If[/color]
End [color=darkblue]Sub[/color]
 
Upvote 0

Forum statistics

Threads
1,215,006
Messages
6,122,666
Members
449,091
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