展开范围内的隐藏行

时间:2015-12-09 23:01:29

标签: excel vba excel-vba

嗨,我是一个完整的编码初学者。我已经整理了一个隐藏的子工作表来隐藏资格问题列表中的行。当选择A列下拉时,需要操纵某些行来隐藏/取消隐藏。我试图让一个具有预定义范围的行被操纵但是有用。我试图使用“记录宏”功能创建一个也不起作用的子宏。如何使用预定义范围隐藏/取消隐藏单个行?

Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.Address = "$A$1" Then
        If Range("A1").Value = "Retail" Then
            Rows("1:11").EntireRow.Hidden = False
            Rows("20:70").EntireRow.Hidden = True
            Rows("12:17").EntireRow.Hidden = True
            Rows("70:70").EntireRow.Hidden = True
        ElseIf Range("A1").Value = "Restaurant" Then
            Rows("20:69").EntireRow.Hidden = True
            Rows("1:19").EntireRow.Hidden = False
            Rows("70:70").EntireRow.Hidden = True
        ElseIf Range("A1").Value = "Hospitality" Then
            Rows("20:69").EntireRow.Hidden = True
            Rows("1:19").EntireRow.Hidden = False
            Rows("70:70").EntireRow.Hidden = True
        ElseIf Range("A1").Value = "Professional Service" Then
            Rows("20:69").EntireRow.Hidden = True
            Rows("1:20").EntireRow.Hidden = False
            Rows("70:70").EntireRow.Hidden = True
        ElseIf Range("A1").Value = "Event" Then
            Rows("20:69").EntireRow.Hidden = True
            Rows("1:19").EntireRow.Hidden = False
            Rows("70:70").EntireRow.Hidden = True
        ElseIf Range("A1").Value = "Mail/Telephone" Then
            Rows("20:69").EntireRow.Hidden = True
            Rows("1:19").EntireRow.Hidden = False
            Rows("70:70").EntireRow.Hidden = False
        ElseIf Range("A1").Value = "Internet" Then
            Rows("1:69").EntireRow.Hidden = False
            Rows("20:20").EntireRow.Hidden = True
        ElseIf Range("A1").Value = "Nonprofit" Then
            Rows("20:69").EntireRow.Hidden = True
            Rows("1:19").EntireRow.Hidden = False
            Rows("70:70").EntireRow.Hidden = True
        ElseIf Range("A9").Value = "No" Then
            Call Macro3
        End If
    End If
End Sub


Sub Macro3()
'
' Macro3 Macro
'

'
    Rows("11:18").Select
    Selection.EntireRow.Hidden = False
End Sub

1 个答案:

答案 0 :(得分:0)

您的代码似乎工作正常,但可以减少:

例如:

Rows("20:70").EntireRow.Hidden = True
Rows("12:17").EntireRow.Hidden = True
Rows("70:70").EntireRow.Hidden = True

范围Rows("70:70")是范围Rows("20:70")的一部分,因此可以删除代码Rows("70:70").EntireRow.Hidden = True的一部分。

另一个例子是:

        Rows("20:69").EntireRow.Hidden = True
        Rows("1:19").EntireRow.Hidden = False  
        Rows("70:70").EntireRow.Hidden = True
可以合并

Rows("70:70")Rows("20:69"),因此可以Rows("20:70")代替Rows("20:69"),并且可以删除代码Rows("70:70").EntireRow.Hidden = True的这一部分。


  • 使用.EntireRow时不需要使用Rows,使用时间Rows("20:69").Hidden = True

  • 您可以使用多个范围:
    Range("12:17","70:70").EntireRow.Hidden = True
    而不是下面的代码:
    Rows("12:17").EntireRow.Hidden = True
    Rows("70:70").EntireRow.Hidden = True

所以,使用上面描述的所有内容并使用引用Ranges的简写方法,您的代码可能如下所示:

Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.Address = "$A$1" Then
        If [A1].Value = "Retail" Then
           [1:11].EntireRow.Hidden = 0
           [12:17,20:70].EntireRow.Hidden = 1
        ElseIf [A1].Value = "Restaurant" Then
            [1:19].EntireRow.Hidden = 0
            [20:70].EntireRow.Hidden = 1
        ElseIf [A1].Value = "Hospitality" Then
            [1:19].EntireRow.Hidden = 0
            [20:70].EntireRow.Hidden = 1
        ElseIf [A1].Value = "Professional Service" Then
            [1:20].EntireRow.Hidden = 0
            [20:70].EntireRow.Hidden = 1
        ElseIf [A1].Value = "Event" Then
            [1:19].EntireRow.Hidden = 0
            [20:70].EntireRow.Hidden = 1
        ElseIf [A1].Value = "Mail/Telephone" Then
            [1:19,70:70].EntireRow.Hidden = 0
            [20:69].EntireRow.Hidden = 1
        ElseIf [A1].Value = "Internet" Then
            [1:69].EntireRow.Hidden = 0
            [20:20].EntireRow.Hidden = 1
        ElseIf [A1].Value = "Nonprofit" Then
            [1:19].EntireRow.Hidden = 0
            [20:70].EntireRow.Hidden = 1
        ElseIf [A9].Value = "No" Then
            [11:18].EntireRow.Hidden = 0
        End If
    End If
End Sub