尝试根据某些单元格值隐藏Excel中的行

时间:2015-05-12 18:03:16

标签: excel vba excel-vba

我正在寻找一个基于某些单元格输入隐藏某些行的公式。在单元格C5中,我有一个下拉选项" Corporates"和" Projects"。在单元格C8中,我有一个下拉选项" High"," Medium"和" {{ 1}}&#34 ;.在单元格Low中,我有公式H6。我的宏如下:

=C5&C8

当我点击Private Sub Worksheet_Change(ByVal Target As Range) If Target.Address(False, False) = "H6" Then Select Case Target.Value Case "CorporatesHigh": Rows("21:33").Hidden = True: Rows("12:20").Hidden = False Case "CorporatesMedium": Rows("21:33").Hidden = True: Rows("12:20").Hidden = False Case "CorporatesLow": Rows("25:33").Hidden = True: Rows("12:24").Hidden = False Case "ProjectsHigh": Rows("25:28").Hidden = False: Rows("29:33").Hidden = True: Rows("12:24").Hidden = True Case "ProjectsMedium": Rows("25:28").Hidden = False: Rows("29:33").Hidden = True: Rows("12:24").Hidden = True Case "ProjectsLow": Rows("25:33").Hidden = False: Rows("12:24").Hidden = True Case "": Rows("12:33").Hidden = False Case "Corporates": Rows("12:33").Hidden = False Case "Projects": Rows("12:33").Hidden = False Case "High": Rows("12:33").Hidden = False Case "Medium": Rows("12:33").Hidden = False Case "Low": Rows("12:33").Hidden = False End Select End If End Sub 时,宏工作正常,但在更改单元格H6C5时我需要它才能正常工作。

1 个答案:

答案 0 :(得分:0)

If-Not-Intersect-Is Nothing模式是检查更改的单元格是否在给定的单元格范围内的首选方法。

要处理所有三个单元格,您可以使用以下内容。如果需要,您可以轻松地向Range添加或删除单元格。

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("H6,C5,C8")) Is Nothing Then
        Select Case Range("H6").Value
            Case "CorporatesHigh": Rows("21:33").Hidden = True: Rows("12:20").Hidden = False
            Case "CorporatesMedium": Rows("21:33").Hidden = True: Rows("12:20").Hidden = False
            Case "CorporatesLow": Rows("25:33").Hidden = True: Rows("12:24").Hidden = False
            Case "ProjectsHigh": Rows("25:28").Hidden = False: Rows("29:33").Hidden = True: Rows("12:24").Hidden = True
            Case "ProjectsMedium": Rows("25:28").Hidden = False: Rows("29:33").Hidden = True: Rows("12:24").Hidden = True
            Case "ProjectsLow": Rows("25:33").Hidden = False: Rows("12:24").Hidden = True
            Case "": Rows("12:33").Hidden = False
            Case "Corporates": Rows("12:33").Hidden = False
            Case "Projects": Rows("12:33").Hidden = False
            Case "High": Rows("12:33").Hidden = False
            Case "Medium": Rows("12:33").Hidden = False
            Case "Low": Rows("12:33").Hidden = False
        End Select
    End If
End Sub