First of all, I'm totally new to VBA and I'm learning as I go. I'm working on an excel-sheet where I want to hide/unhide rows depending on some choices the user do as input in the beginning.
So for instance if I have four choices in the beginning (1 and 0), these could be:
1
0
1
0
IF these values are chosen I want to hide row 42, 56-60 and 70.
Or:
0
1
1
1
IF these values are chosen I want to hide row 28, 31-34 and 72-74.
And so on, for all possibly combinations of the 4-input-cells (2*2*2*2=16).
I figured one way was to choose which cells to show and which to hide for every single combination. Then I get something like the code shown at the bottom (this is for one choice 0 or 1 and one choice 0, 1 or 2). This doesn't work, any clues why? Also, is there another way to do this? I got around 400 possible combinations so having to do this for every one is quite a hassle
PHP Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
If Target.Column = 4 And Target.Row = 4 And Target.Value = "0" Then
If Target.Column = 4 And Target.Row = 5 And Target.Value = "0" Then
Rows("1:34").Select
Selection.EntireRow.Hidden = False
Rows("35:35").Select
Selection.EntireRow.Hidden = True
Rows("36:65").Select
Selection.EntireRow.Hidden = False
Rows("66:82").Select
Selection.EntireRow.Hidden = True
Rows("83:95").Select
Selection.EntireRow.Hidden = False
Rows("96:96").Select
Selection.EntireRow.Hidden = True
Rows("97:101").Select
Selection.EntireRow.Hidden = False
Rows("102:104").Select
Selection.EntireRow.Hidden = True
Rows("105:244").Select
Selection.EntireRow.Hidden = False
Rows("245:253").Select
Selection.EntireRow.Hidden = True
ElseIf Target.Column = 4 And Target.Row = 5 And Target.Value = "1" Then
Rows("1:34").Select
Selection.EntireRow.Hidden = False
Rows("35:35").Select
Selection.EntireRow.Hidden = True
Rows("36:75").Select
Selection.EntireRow.Hidden = False
Rows("76:82").Select
Selection.EntireRow.Hidden = True
Rows("83:95").Select
Selection.EntireRow.Hidden = False
Rows("96:96").Select
Selection.EntireRow.Hidden = True
Rows("97:101").Select
Selection.EntireRow.Hidden = False
Rows("102:104").Select
Selection.EntireRow.Hidden = True
Rows("105:246").Select
Selection.EntireRow.Hidden = False
Rows("247:253").Select
Selection.EntireRow.Hidden = True
ElseIf Target.Column = 4 And Target.Row = 5 And Target.Value = "2" Then
Rows("1:34").Select
Selection.EntireRow.Hidden = False
Rows("35:35").Select
Selection.EntireRow.Hidden = True
Rows("36:101").Select
Selection.EntireRow.Hidden = False
Rows("102:104").Select
Selection.EntireRow.Hidden = True
Rows("105:253").Select
Selection.EntireRow.Hidden = False
End If
ElseIf Target.Column = 4 And Target.Row = 4 And Target.Value = "1" Then
If Target.Column = 4 And Target.Row = 5 And Target.Value = "0" Then
Rows("1:65").Select
Selection.EntireRow.Hidden = False
Rows("66:82").Select
Selection.EntireRow.Hidden = True
Rows("83:95").Select
Selection.EntireRow.Hidden = False
Rows("96:96").Select
Selection.EntireRow.Hidden = True
Rows("97:100").Select
Selection.EntireRow.Hidden = False
Rows("101:101").Select
Selection.EntireRow.Hidden = True
Rows("102:244").Select
Selection.EntireRow.Hidden = False
Rows("245:253").Select
Selection.EntireRow.Hidden = True
ElseIf Target.Column = 4 And Target.Row = 5 And Target.Value = "1" Then
Rows("1:75").Select
Selection.EntireRow.Hidden = False
Rows("76:82").Select
Selection.EntireRow.Hidden = True
Rows("83:95").Select
Selection.EntireRow.Hidden = False
Rows("96:96").Select
Selection.EntireRow.Hidden = True
Rows("97:100").Select
Selection.EntireRow.Hidden = False
Rows("101:101").Select
Selection.EntireRow.Hidden = True
Rows("102:246").Select
Selection.EntireRow.Hidden = False
Rows("247:253").Select
Selection.EntireRow.Hidden = True
ElseIf Target.Column = 4 And Target.Row = 5 And Target.Value = "2" Then
Rows("1:100").Select
Selection.EntireRow.Hidden = False
Rows("101:101").Select
Selection.EntireRow.Hidden = True
Rows("102:253").Select
Selection.EntireRow.Hidden = False
End If
End If
Application.ScreenUpdating = True
End Sub
Bookmarks