excel - 在 VBA 中定义工作表和工作表

标签 excel vba

我的工作簿有图表和普通工作表,因此我使用 Sheets而不是 Worksheets .但是,我不知道 sht 在下面的代码集中应该是什么类型。我确定它不可能是 Worksheet .

' Hide all sheets/charts except Sheet1
Sub Hide_Sheets()

    Dim sht As ???

    For Each sht In ActiveWorkbook.Sheets
        If sht.Name <> Sheet3.Name Then
            sht.Visible = False
        End If
    Next sht

End Sub

最佳答案

“图表和工作表是两个不同的集合。” --> https://stackoverflow.com/a/6804704/138938

如果您同时拥有图表工作表和常规工作表,则可以循环访问一组对象,如下所示:

Sub Hide_Objects()
    Dim wb As Workbook
    Dim obj As Object

    Set wb = ActiveWorkbook

    For Each obj In wb.Sheets
        If obj.Name <> "Sheet1" Then
            obj.Visible = False
        End If
    Next obj
End Sub

或者您可以像这样遍历两个集合:
Sub Hide_Sheets_And_Charts()
    Dim wb As Workbook
    Dim sht As Worksheet
    Dim cht As Chart

    Set wb = ActiveWorkbook

    For Each sht In wb.Worksheets
        If sht.Name <> "Sheet1" Then
            sht.Visible = False
        End If
    Next sht

    For Each cht In wb.Charts
        If cht.Name <> "Sheet1" Then
            cht.Visible = False
        End If
    Next cht

End Sub

关于excel - 在 VBA 中定义工作表和工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31281579/

相关文章:

Excel VBA 选项按钮在启动时默认选择

excel - Excel 中的 TIMEVALUE 转换大于 24 小时

vba - 从 VBA 中的并集生成的行范围中删除行

VBA评估函数和数组公式返回值的范围

string - 尝试仅显示一定数量的数字

excel - 更改 Excel for Mac 的 VBE 的默认字体大小

excel - VBA 中的 UTF-8 字符 Shell_NotifyIconW

excel - 在打开的Word文档中找到未知的姓名和姓氏,将其复制并使用excel VBA粘贴到excel .activesheet中的单元格A12中

vba - 在 access 97 中查找完整路径的目录部分(减去文件名)

vba - 搜索和替换多个单词