隐藏/取消隐藏行时运行时错误'13'类型不匹配

时间:2019-11-06 14:52:54

标签: excel vba

我建立了一个动态的excel表单,其中所有相关的行仅在您从下拉列表中进行选择时才取消隐藏。一切工作正常,直到我决定添加更多行并希望将它们隐藏在该部分中,直到做出特定选择为止。换句话说,我的节根据顶部的选择取消隐藏,但是现在有必要在这些节中隐藏一些行,直到下拉触发它们取消隐藏为止。

我试图将部分隐藏成碎片,而不是隐藏全部10行,我只隐藏一些第一行,然后隐藏其余的行,同时隐藏中间的行,直到它们被另一个选择触发

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column = 2 And Target.Row = 2 Then
       Select Case Target.Value
        Case "New deployment"
            [3:3].EntireRow.Hidden = False
        Case Else
            [3:76].EntireRow.Hidden = True
            [B5:B11].ClearContents
            [B31:B38].ClearContents
            [B40:B46].ClearContents
            [B13:B23].Value = "Not included"
            [B48:B58].Value = "Not included"
            [B3:B3].Value = "Please select"
       End Select
    End If    
    If Target.Column = 2 And Target.Row = 3 Then
            Select Case Target.Value
            Case "One device"
                [4:29].EntireRow.Hidden = False
                [8:9].EntireRow.Hidden = True
                [30:76].EntireRow.Hidden = True
                [B31:B38].ClearContents
                [B40:B46].ClearContents
                [B48:B58].Value = "Not included"
            Case "Two devices"
                [4:29].EntireRow.Hidden = True
                [30:64].EntireRow.Hidden = False
                [34:35].EntireRow.Hidden = True
                [43:44].EntireRow.Hidden = True
                [B5:B11].ClearContents
                [B13:B23].Value = "Not included"
            Case Else
                [4:76].EntireRow.Hidden = True
                [B5:B11].ClearContents
                [B31:B38].ClearContents
                [B40:B46].ClearContents
                [B13:B23].Value = "Not included"
                [B48:B58].Value = "Not included"
           End Select
        End If

        If Target.Column = 2 And Target.Row = 2 Then
           Select Case Target.Value
            Case "Modification"
                [65:70].EntireRow.Hidden = False
            Case Else
                [65:70].EntireRow.Hidden = True
           End Select
        End If

        If Target.Column = 2 And Target.Row = 2 Then
           Select Case Target.Value
            Case "Disconnect"
                [71:76].EntireRow.Hidden = False
            Case Else
                [71:76].EntireRow.Hidden = True
           End Select
        End If

        If Target.Column = 2 And Target.Row = 13 Then
           Select Case Target.Value
            Case "Included"
                [9:9].EntireRow.Hidden = False
            Case Else
                [9:9].EntireRow.Hidden = True
           End Select
        End If

        If Not Application.Intersect([B5:B5], Range(Target.Address)) Is Nothing Then
           [B6:B7].ClearContents
        End If

        If Not Application.Intersect([B31:B31], Range(Target.Address)) Is Nothing Then
           [B32:B33].ClearContents
        End If

        If Not Application.Intersect([B40:B40], Range(Target.Address)) Is Nothing Then
           [B41:B42].ClearContents
        End If

        If Not Application.Intersect([B6:B6], Range(Target.Address)) Is Nothing Then
           [B7:B7].ClearContents
        End If

        If Not Application.Intersect([B32:B32], Range(Target.Address)) Is Nothing Then
           [B33:B33].ClearContents
        End If

        If Not Application.Intersect([B41:B41], Range(Target.Address)) Is Nothing Then
           [B42:B42].ClearContents
        End If
    End Sub

  

不匹配错误'13'

我在添加以下代码块后立即出现:

If Target.Column = 2 And Target.Row = 13 Then
   Select Case Target.Value
    Case "Included"
        [9:9].EntireRow.Hidden = False
    Case Else
        [9:9].EntireRow.Hidden = True
   End Select
End If

1 个答案:

答案 0 :(得分:0)

我最终设法通过将代码的所提到的代码段更改为以下代码来克服该错误:

    If Range("B13").Value = "Included" Then
    [9:9].EntireRow.Hidden = False
    Else
    [9:9].EntireRow.Hidden = True
    End If