Hide multiple non contiguous Rows based on Dropdown List

jondu

Board Regular
Joined
Feb 6, 2019
Messages
71
Hi,
I'm a newbie in Excel. Could you help me to code for hidding rows that are non consecutives ?

My cell A1 is a dropdown menu, with the choice between Value1 and Value2.

I found this (adapted to be simple), wich is working :

Code:
Private Sub Worksheet_Change(ByVal Target As Range)    If Not Intersect(Target, Range("$A$1")) Is Nothing Then
        With Range("$A$1")
            Range("10:14").EntireRow.Hidden = .Value = "Value1"
            Range("15:20").EntireRow.Hidden = .Value = "Value2"
            Range("21:25").EntireRow.Hidden = .Value = "Value1"
            Range("26:30").EntireRow.Hidden = .Value = "Value2"
            Range("31:35").EntireRow.Hidden = .Value = "Value1"
            Range("36:40").EntireRow.Hidden = .Value = "Value2"
            Range("41:45").EntireRow.Hidden = .Value = "Value1"
            Range("46:50").EntireRow.Hidden = .Value = "Value2"
        End With
    End If
End Sub

But the problem : I want to add another dropdown choice, in another cell, let's say in A2.
How can I make it to enrich my code ??

I have been busy for so long ...Thanks !
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Hi & welcome to MrExcel
You can simplify your existing code like
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Not Intersect(Target, Range("A1")) Is Nothing Then
      With Range("$A$1")
         Range("10:14,21:25,31:35,41:45").EntireRow.Hidden = .Value = "Value1"
         Range("15:20,26:30,36:40,46:50").EntireRow.Hidden = .Value = "Value2"
      End With
   End If
End Sub
With the dropdown in A2, do you want to hide/unhide the same rows, or different rows?
 
Upvote 0
ouaw, thanks for the quick reaction!
With the dropdown A2 I would like to hide at the same time other different rows AND some same rows
 
Upvote 0
Example :
if Dropdown A1 = value1, then rows 10:15 are hidden and 30:40
If Dropdown A1 = value2, then rows 10:12 and 20:30 are hidden
If Dropdown A2 = value3, then rows 13:15 are hidden (in addition to the hidden rows from A1)
****** id="cke_pastebin" style="position: absolute; top: 0px; width: 1px; height: 1px; overflow: hidden; left: -1000px;">






50/5000













[COLOR=rgba(0, 0, 0, 0.87)]in addition to the hidden lines via the dropdown A1












50/5000









[/COLOR]


[COLOR=rgba(0, 0, 0, 0.87)]in addition to the hidden lines via the dropdown A1




[/COLOR]
</body>
 
Upvote 0
How about
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Not Intersect(Target, Range("A1:A2")) Is Nothing Then
      With Range("$A$1")
         Range("10:14,21:25,31:35,41:45").EntireRow.Hidden = .Value = "Value1"
         Range("15:20,26:30,36:40,46:50").EntireRow.Hidden = .Value = "Value2"
      End With
      With Range("A2")
         Range("13:15").EntireRow.Hidden = .Value = "Value3"
      End With
   End If
End Sub
 
Upvote 0
Thank you again ! And how can I manage if I want to hide unique rows sometimes ? (not in Range)
 
Upvote 0
ok let's be more accurate in my need.


Dropdown V2 :
Value1
Value2
Value3
Value4
Value5
etc.


If Value1 selected : Then Rows 17 + 20 + 29 + 31:50 + 106:107 + 114:128 should be hidden
If Value 2 selected : Then Rows 17 + 19:20 + 29 + 31:50 + 114:128 should be hidden
If Value 3 selected : Then Rows 17 + 19 + 29 + 31:50 + 114:128 should be hidden
If Value 4 selected : 17 + 20 + 29 + 31:50 + 106:107 + 114:128 should be hidden
If Value 5 selected : 17 + 19 + 29 + 31:50 + 114:128
etc.


Dropdown V3 :
ValueX
ValueY


If ValueX selected : Then Rows 58:60 + 89:90 + 92:94 + 108 + 111 should be hidden (taking into account the rows already hidden by the choice of the V2)
If ValueY selected : no more rows hidden




When I switch the value frome dropdown V2 (value1, value2, etc.) it must only hide the cells linked only to this value (not add to the hidden rows due to previously selected values)
 
Upvote 0
here what I have for the moment (i'm sure it must look horrible as a code for you)
The problem is that I always have to check the Value0 before to be able to select another value in V2. I would like the switch more efficient.



Code:
Private Sub Worksheet_Change(ByVal Target As Range)If Not Intersect(Target, Range("V2")) Is Nothing Then
    Select Case Range("V2")
        Case "Value0": macroALL
        Case "Value1": macro1
        Case "Value2": macro2
        Case "Value3": macro3
        Case "Value4": macro4
        Case "Value5": macro5
    End Select
End If
If Not Intersect(Target, Range("V3")) Is Nothing Then
    Select Case Range("V3")
        Case "ValueX": macro6
        Case "ValueY": macro7
    End Select
    
End If
End Sub


Sub macroALL()


If Range("v2").Value = "Value0" Then Rows("1:200").EntireRow.Hidden = False


End Sub


Sub macro1()


Rows("17").EntireRow.Hidden = True
Rows("20").EntireRow.Hidden = True
Rows("29").EntireRow.Hidden = True
Rows("31:50").EntireRow.Hidden = True
Rows("106:107").EntireRow.Hidden = True
Rows("114:128").EntireRow.Hidden = True


End Sub


Sub macro2()


Rows("17").EntireRow.Hidden = True
Rows("29").EntireRow.Hidden = True
Rows("19:20").EntireRow.Hidden = True
Rows("31:50").EntireRow.Hidden = True
Rows("114:128").EntireRow.Hidden = True


End Sub


Sub macro3()


Rows("17").EntireRow.Hidden = True
Rows("29").EntireRow.Hidden = True
Rows("19").EntireRow.Hidden = True
Rows("31:50").EntireRow.Hidden = True
Rows("114:128").EntireRow.Hidden = True


End Sub


Sub macro4()


Rows("17").EntireRow.Hidden = True
Rows("20").EntireRow.Hidden = True
Rows("29").EntireRow.Hidden = True
Rows("31:50").EntireRow.Hidden = True
Rows("106:107").EntireRow.Hidden = True
Rows("114:128").EntireRow.Hidden = True


End Sub


Sub macro5()


Rows("17").EntireRow.Hidden = True
Rows("19").EntireRow.Hidden = True
Rows("29").EntireRow.Hidden = True
Rows("31:50").EntireRow.Hidden = True
Rows("114:128").EntireRow.Hidden = True


End Sub




Sub macro6()
   
Rows("58:60").EntireRow.Hidden = True
Rows("89:90").EntireRow.Hidden = True
Rows("92:94").EntireRow.Hidden = True
Rows("108").EntireRow.Hidden = True
Rows("1111").EntireRow.Hidden = True


End Sub
 
Upvote 0
How about
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Not Intersect(Target, Range("A1:A2")) Is Nothing Then
      Cells.EntireRow.Hidden = False
      Select Case Range("A1")
         Case "Value1"
            Range("17:17,20:20,29:29,31:50,106:107,114:128").EntireRow.Hidden = True
         Case "Value2"
            Range("17:17,19:20,29:29,31:50,114:128").EntireRow.Hidden = True
      End Select
      Select Case Range("A2")
         Case "Value3"
            Range("58:60,89:90,92:94,108:111").EntireRow.Hidden = True
      End Select
   End If
End Sub
 
Upvote 0
Woua perfect, everything is solved :cool:
Thanks for the responsiveness. I am very grateful, what a sense of community! (first time I interact in a forum, so impressed)
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,996
Members
452,373
Latest member
TimReeks

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