VBA 参考工作表与图表表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6797427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 13:42:05  来源:igfitidea点击:

VBA Refer to worksheet vs chart sheet

vbachartsworksheetrefer

提问by Jesse Smothermon

I'm trying to write a small function that takes in a filepath (where the workbook was saved at), targetpath (where the pdf will be saved to), and a string of tab names (pipe (|) delimited) in excel.

我正在尝试编写一个小函数,该函数接受 Excel 中的文件路径(保存工作簿的位置)、目标路径(保存 pdf 的位置)和一串选项卡名称(以管道 (|) 分隔)。

The user of the function doesn't have to input a string of tab names (it's optional) and if they don't, I want to select all of the visible tabs and print them. This would be in the case if the user has 50 charts in separate worksheets and don't want to write a string like "Chart1|Chart2|...."

该函数的用户不必输入选项卡名称字符串(可选),如果不输入,我想选择所有可见选项卡并打印它们。如果用户在单独的工作表中有 50 个图表并且不想编写像“Chart1|Chart2|....”这样的字符串,就会出现这种情况。

Code:

代码:

For Each WSO.Name In WBO.Worksheets 
    strSheets = strSheets & WSO.Name & "|" 
Next WSO

strSheets = Left(strSheets, Len(strSheets) - 1) 
arraySheets() = Split(strSheets, "|")

WBO.Sheets(arraySheets()).Select     
WBO.ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _ 
    strFilePath, Quality:=xlQualityStandard, _ 
    IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _ 
    True

There's two problems with the For Each loop: it doesn't grab any sheets such as "Chart1", it only grabs sheets such as "Sheet1". Also, it will grab hidden sheets so that when I try to select them all I get an out of bounds error.

For Each 循环有两个问题:它不抓取诸如“Chart1”之类的任何工作表,它只抓取诸如“Sheet1”之类的工作表。此外,它会抓取隐藏的工作表,这样当我尝试全部选择它们时,我会收到越界错误。

I didn't know if a Chart sheet is referred to differently then a regular sheet or why hidden sheets are also chosen.

我不知道图表工作表是否与普通工作表不同,或者为什么还选择了隐藏工作表。

回答by GSerg

Use WBO.Sheetsinstead of WBO.Worksheetsin the loop.

在循环中使用WBO.Sheets而不是WBO.Worksheets

Verify that WSO.Visible = xlSheetVisibleto filter out hidden sheets.

验证WSO.Visible = xlSheetVisible以过滤掉隐藏的工作表。

回答by Steve Rindsberg

There's two problems with the For Each loop: it doesn't grab any sheets such as "Chart1", it only grabs sheets such as "Sheet1"

For Each 循环有两个问题:它不抓取诸如“Chart1”之类的任何工作表,它只抓取诸如“Sheet1”之类的工作表

Charts and Worksheets are two different collections.
Try this:

图表和工作表是两个不同的集合。
尝试这个:

Sub Demo()
Dim oWs As Worksheet
Dim oCs As Chart

For Each oWs In ActiveWorkbook.Worksheets
    Debug.Print oWs.Name
Next

For Each oCs In ActiveWorkbook.Charts
    Debug.Print oCs.Name
Next
End Sub