在使图表可见后使工作表可见时,可见属性失败了吗?

时间:2015-02-10 10:35:42

标签: excel vba excel-vba

以下代码有效:

Sub ShowSheets(vSheets() As Variant)

    Dim i As Long

    'Make worksheets visible
    For i = LBound(vSheets, 1) To UBound(vSheets, 1)
        If TypeName(ThisWorkbook.Sheets(vSheets(i, 1))) = "Worksheet" Then
            ThisWorkbook.Worksheets(vSheets(i, 1)).Visible = xlSheetVisible
        End If
    Next i

    'Make chart sheets visible
    For i = LBound(vSheets, 1) To UBound(vSheets, 1)
        Debug.Print (TypeName(ThisWorkbook.Sheets(vSheets(i, 1))))
        If TypeName(ThisWorkbook.Sheets(vSheets(i, 1))) = "Chart" Then
            ThisWorkbook.Charts(vSheets(i, 1)).Visible = xlSheetVisible
        End If
    Next i

End Sub

以下代码失败:

Sub ShowSheets(vSheets() As Variant)

    Dim i As Long

    'Make chart sheets visible
    For i = LBound(vSheets, 1) To UBound(vSheets, 1)
        Debug.Print (TypeName(ThisWorkbook.Sheets(vSheets(i, 1))))
        If TypeName(ThisWorkbook.Sheets(vSheets(i, 1))) = "Chart" Then
            ThisWorkbook.Charts(vSheets(i, 1)).Visible = xlSheetVisible
        End If
    Next i

    'Make worksheets visible
    For i = LBound(vSheets, 1) To UBound(vSheets, 1)
        If TypeName(ThisWorkbook.Sheets(vSheets(i, 1))) = "Worksheet" Then
            ThisWorkbook.Worksheets(vSheets(i, 1)).Visible = xlSheetVisible
        End If
    Next i

End Sub

失败并显示错误消息:

Run-time error '-2147417848 (80010108)'

Method 'Visible' of object '_Worksheet'failed

成功的代码与失败的代码之间唯一不同的是,在成功的代码中我首先检查Worksheets然后查看Charts,而在代码中失败我反转该顺序。它始终是导致失败的图表之后的第一个工作表。

为什么第二个代码会失败?

更多信息:两种情况下情况相同。 vSheets(i, 1)始终等同于工作表名称。工作表和工作簿不受保护。

编辑:这是调用例程

Sub ToggleSheets()
    'Save sheets in tToggleSheets.DatabodyRange
    Dim wksTables As Worksheet
    Dim loSheets As ListObject
    Dim vSheets() As Variant
    Dim bAnySheetVisible As Boolean

    Set wksTables = GetSheetByCodename(ThisWorkbook, "wTables")
    Set loSheets = wksTables.ListObjects("tToggleSheets")
    vSheets = loSheets.DataBodyRange

    'Check if all sheets are hidden
    bAnySheetVisible = AnySheetVisible(vSheets)

    If bAnySheetVisible = True Then
        'If any is visible, hide all
        Call HideSheets(vSheets)
    Else
        'Else unhide all
        Call ShowSheets(vSheets)
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

问题不在您发布的代码中。我的子 MAIN()运行你的代码"原样。"

主要:

  1. 隐藏所有表格(包括工作和图表),但第一张表格
  2. 除外
  3. 发出 MsgBox 以验证隐藏状态
  4. 调用您的例行程序

  5.     Sub ShowSheets(vSheets() As Variant)
    
        Dim i As Long
    
        'Make chart sheets visible
        For i = LBound(vSheets, 1) To UBound(vSheets, 1)
            Debug.Print (TypeName(ThisWorkbook.Sheets(vSheets(i, 1))))
            If TypeName(ThisWorkbook.Sheets(vSheets(i, 1))) = "Chart" Then
                ThisWorkbook.Charts(vSheets(i, 1)).Visible = xlSheetVisible
            End If
        Next i
    
        'Make worksheets visible
        For i = LBound(vSheets, 1) To UBound(vSheets, 1)
            If TypeName(ThisWorkbook.Sheets(vSheets(i, 1))) = "Worksheet" Then
                ThisWorkbook.Worksheets(vSheets(i, 1)).Visible = xlSheetVisible
            End If
        Next i
    
    End Sub
    Sub MAIN()
        Dim vvSheets(1 To 8, 1 To 1) As Variant
        Dim i As Long, msg As String
    
        msg = ""
        For i = 1 To Sheets.Count
            If i > 1 Then
                Sheets(i).Visible = False
            End If
            vvSheets(i, 1) = Sheets(i).Name
            msg = msg & vbCrLf & Sheets(i).Name
        Next i
        MsgBox msg
    
        Call ShowSheets(vvSheets)
    End Sub
    

    您的代码运行没有错误。我怀疑该错误可能与数组 Dim' 或填写在调用例程中的方式有​​关。

    我的初始配置:

    enter image description here