选择Case VBA不起作用

时间:2017-03-29 08:58:02

标签: vba excel-vba module intersection excel

我有以下代码,我希望根据案例隐藏列。但它不起作用。我怀疑它可能与表格的分组和布局有关但是在使用此代码之前从未遇到过问题。

Private Sub worksheet_Change(ByVal Target As Range)
  If Not Intersect(Target, Range("C2")) Is Nothing Then
    On Error GoTo bm_Safe_Exit
    ActiveSheet.Unprotect Password:="Password"
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Columns("AD:JF").Hidden = False

    Select Case Range("C2").Value2
        Case 1
            Columns("AQ:DC,DR:GD,GS:JE").Hidden = True
            ActiveSheet.Outline.ShowLevels RowLevels:=0, ColumnLevels:=3
            ActiveSheet.Outline.ShowLevels RowLevels:=0, ColumnLevels:=2
            ActiveSheet.Outline.ShowLevels RowLevels:=0, ColumnLevels:=1
        Case 2
            Columns("BD:DC,EE:GD,HF:JE").Hidden = True
            ActiveSheet.Outline.ShowLevels RowLevels:=0, ColumnLevels:=3
            ActiveSheet.Outline.ShowLevels RowLevels:=0, ColumnLevels:=2
            ActiveSheet.Outline.ShowLevels RowLevels:=0, ColumnLevels:=1
        Case 3
            Columns("BQ:DC,ER:GD,HS:JE").Hidden = True
            ActiveSheet.Outline.ShowLevels RowLevels:=0, ColumnLevels:=3
            ActiveSheet.Outline.ShowLevels RowLevels:=0, ColumnLevels:=2
            ActiveSheet.Outline.ShowLevels RowLevels:=0, ColumnLevels:=1
        Case 4
            Columns("CD:DC,FE:GD,IF:JE").Hidden = True
            ActiveSheet.Outline.ShowLevels RowLevels:=0, ColumnLevels:=3
            ActiveSheet.Outline.ShowLevels RowLevels:=0, ColumnLevels:=2
            ActiveSheet.Outline.ShowLevels RowLevels:=0, ColumnLevels:=1
        Case 5
            Columns("CQ:DC,FR:GD,IS:JE").Hidden = True
            ActiveSheet.Outline.ShowLevels RowLevels:=0, ColumnLevels:=3
            ActiveSheet.Outline.ShowLevels RowLevels:=0, ColumnLevels:=2
            ActiveSheet.Outline.ShowLevels RowLevels:=0, ColumnLevels:=1
        Case 6
            ActiveSheet.Outline.ShowLevels RowLevels:=0, ColumnLevels:=3
            ActiveSheet.Outline.ShowLevels RowLevels:=0, ColumnLevels:=2
            ActiveSheet.Outline.ShowLevels RowLevels:=0, ColumnLevels:=1
    End Select
End If

bm_Safe_Exit:
Application.EnableEvents = True
Application.ScreenUpdating = True
ActiveSheet.Protect Password:="password"

End Sub

包含电子表格布局的图片 ngram parser

1 个答案:

答案 0 :(得分:0)

当我尝试

Columns("AQ:DC,DR:GD,GS:JE").Hidden = True

我收到错误(类型不匹配),因此请尝试拆分隐藏命令,如下所示:

Columns("AQ:DC").Hidden = True
Columns("DR:GD").Hidden = True
Columns("GS:JE").Hidden = True