如何根据单元格值隐藏列

时间:2017-10-17 19:55:53

标签: excel vba excel-vba

我正在根据单元格值查找隐藏列。 例如,当值为1时,I到BV列必须隐藏。当值为2时,BV列必须隐藏,但I到O列必须可见。

我的代码仅适用于1,我不知道该怎么办... 谢谢你的帮助

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("B2")) Is Nothing Then
        If Target = 1 Then
            Columns("I:BV").EntireColumn.Hidden = True
        Else: Columns("I:BV").EntireColumn.Hidden = False
        End If
    End If
End Sub

Private Sub Worksheet_Change2(ByVal Target As Range)
    If Not Intersect(Target, Range("B2")) Is Nothing Then
        If Target = 2 Then
            Columns("O:BV").EntireColumn.Hidden = True
        Else: Columns("O:BV").EntireColumn.Hidden = False
        End If
    End If
End Sub

Private Sub Worksheet_Change3(ByVal Target As Range)
    If Not Intersect(Target, Range("B2")) Is Nothing Then
        If Target = 4 Then
            Columns("U:BV").EntireColumn.Hidden = True
        Else: Columns("U:BV").EntireColumn.Hidden = False
        End If
    End If
End Sub

Private Sub Worksheet_Change4(ByVal Target As Range)
    If Not Intersect(Target, Range("B2")) Is Nothing Then
        If Target = 5 Then
            Columns("AA:BV").EntireColumn.Hidden = True
        Else: Columns("AA:BV").EntireColumn.Hidden = False
        End If
    End If
End Sub

Private Sub Worksheet_Change5(ByVal Target As Range)
    If Not Intersect(Target, Range("B2")) Is Nothing Then
        If Target = 6 Then
            Columns("AG:BV").EntireColumn.Hidden = True
        Else: Columns("AG:BV").EntireColumn.Hidden = False
        End If
    End If
End Sub

Private Sub Worksheet_Change6(ByVal Target As Range)
    If Not Intersect(Target, Range("B2")) Is Nothing Then
        If Target = 7 Then
            Columns("AM:BV").EntireColumn.Hidden = True
        Else: Columns("AM:BV").EntireColumn.Hidden = False
        End If
    End If
End Sub

Private Sub Worksheet_Change7(ByVal Target As Range)
    If Not Intersect(Target, Range("B2")) Is Nothing Then
        If Target = 8 Then
            Columns("AS:BV").EntireColumn.Hidden = True
        Else: Columns("AS:BV").EntireColumn.Hidden = False
        End If
    End If
End Sub

Private Sub Worksheet_Change8(ByVal Target As Range)
    If Not Intersect(Target, Range("B2")) Is Nothing Then
        If Target = 9 Then
            Columns("AY:BV").EntireColumn.Hidden = True
        Else: Columns("AY:BV").EntireColumn.Hidden = False
        End If
    End If
End Sub

Private Sub Worksheet_Change9(ByVal Target As Range)
    If Not Intersect(Target, Range("B2")) Is Nothing Then
        If Target = 10 Then
            Columns("BE:BV").EntireColumn.Hidden = True
        Else: Columns("BE:BV").EntireColumn.Hidden = False
        End If
    End If
End Sub

Private Sub Worksheet_Change10(ByVal Target As Range)
    If Not Intersect(Target, Range("B2")) Is Nothing Then
        If Target = 11 Then
            Columns("BK:BV").EntireColumn.Hidden = True
        Else: Columns("BK:BV").EntireColumn.Hidden = False
        End If
    End If
End Sub

Private Sub Worksheet_Change11(ByVal Target As Range)
    If Not Intersect(Target, Range("B2")) Is Nothing Then
        If Target = 12 Then
            Columns("BQ:BV").EntireColumn.Hidden = True
        Else: Columns("BQ:BV").EntireColumn.Hidden = False
        End If
    End If
End Sub

3 个答案:

答案 0 :(得分:2)

只需计算列号是否大于您想要显示的最后一列:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("B2")) Is Nothing Then
        Dim i As Long
        Dim lastVisible As Long
        'Use cell B2 in the calculation, just in case Target is
        ' something like A1:D17
        lastVisible = 2 + Range("B2").Value * 6
        'That formula is calculating lastVisible such that:
        'If B2 is  1, lastVisible will be  8 (i.e. column H)
        'If B2 is  2, lastVisible will be 14 (i.e. column N)
        'If B2 is  3, lastVisible will be 20 (i.e. column T)
        'If B2 is  4, lastVisible will be 26 (i.e. column Z)
        '... etc, up to
        'If B2 is 11, lastVisible will be 68 (i.e. column BP)
        'If B2 is 12, lastVisible will be 74 (i.e. column BV)

        For i = 3 To 74
            Columns(i).Hidden = i > lastVisible
        Next
    End If
End Sub

答案 1 :(得分:1)

使用ElseIf在IF语句中进行多个条件测试的一个更改事件。如果没有为您编写所有内容,请遵循以下结构和关键元素。堆栈溢出有很多例子可以提供帮助。

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("B2")) Is Nothing Then

        'Code to unhide all columns goes here.

        'Then test the contents of B2
        If Target = 1 Then
            Columns("I:BV").EntireColumn.Hidden = True
        ElseIf Target = 2 Then 
            Columns("O:BV").EntireColumn.Hidden = True
        ElseIf Target = 3 Then ......'Continue with rest of conditions
        End If
   End If

End Sub

答案 2 :(得分:0)

你可以用更少的行来做到这一点:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim primaryCols As Range
If Not Intersect(Target, Range("B2")) Is Nothing Then
        Range(Columns(3 + Range("B2").Value * 6), Columns(74)).EntireColumn.Hidden = False
        Range(Columns(9), Columns(2 + Range("B2").Value * 6)).EntireColumn.Hidden = True
End If
End Sub

基本上,它使用一个小算术来为你想要的那些人获取你的起始列,并从第一列开始使用结束列来隐藏那些。