Hide rows based on Drop Down list in Excel

末鹿安然 提交于 2020-01-06 01:56:06

问题


This is basically a mail template, containing 3 different types of content.
I have an excel sheet with a combo-box list. The list has values 1,2 and 3. & 3 different types of content for them. When any one of them is selected . The remaining content must be hidden.


回答1:


Assuming you have validation option set in Range("A1") with different options (1-3 in my example). In the appropriate Sheet module put the following code and change it as needed.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$1" Then
Dim rngOpt1 As Range
Dim rngOpt2 As Range
Dim rngOpt3 As Range
'you doesn't need to put rows references here
'as we deal with it later
    Set rngOpt1 = Range("b10:c15")   'first area to be hidden
    Set rngOpt2 = Range("d16:e20")   'second...
    Set rngOpt3 = Range("f21:g25")   'you can guess
If Range("A1") = 1 Then 'your validation cell
    rngOpt1.EntireRow.Hidden = False
    rngOpt2.EntireRow.Hidden = True
    rngOpt2.EntireRow.Hidden = True
ElseIf Range("A1") = 2 Then
    rngOpt1.EntireRow.Hidden = True
    rngOpt2.EntireRow.Hidden = False
    rngOpt2.EntireRow.Hidden = True
Else
    'you can do it on you own... :)
End If
End If
End Sub

Each time you change value in A1 the appropriate rows range would be hidden. I'm not specially proud of its efficiency but it was my first idea.



来源:https://stackoverflow.com/questions/15584345/hide-rows-based-on-drop-down-list-in-excel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!