简体   繁体   English

Excel VBA Sheets(Array())。选择下标超出范围

[英]Excel VBA Sheets(Array()).Select Subscript out of range

Having a little problem with this tiny bit of code in Excel VBA. Excel VBA中的这一小段代码有一点问题。

All I'm trying to do is select multiple sheets in a workbook and export them as one PDF. 我要做的就是在工作簿中选择多张工作表,然后将它们导出为一个PDF。 I have many Sheets that I don't want to hide or move around, so earlier in my code I looped and saved the particular names in an array. 我不想隐藏或移动许多表格,因此在我的代码的前面,我循环并将特定名称保存在数组中。 I was getting this error, so i thought I would resort to simply hard coding their names to see if that worked. 我遇到了这个错误,因此我想我将仅对它们的名称进行硬编码,看看是否可行。

But even with hard coded names i get the 'Subscript out of range error', so there must be something else going wrong. 但是即使使用硬编码的名称,我也会收到“下标超出范围错误”,因此肯定还有其他问题。

wb.Sheets(Array("Sheet 1", "Sheet 2", "Sheet 3", "Sheet 4", "Sheet 5")).Select

Selection.ExportAsFixedFormat Type:=xlTypePDF, _ ............

Are there any known errors relating to the above statement? 是否有与上述陈述有关的已知错误? (I've only included the first 2 lines as it doesn't execute past the first line) OR are there any suggestions to stop this error occuring? (我只包括了前两行,因为它没有执行到第一行之后),或者有什么建议可以阻止此错误的发生?

Note: I have changed the Sheet names as they relate to sensitive data. 注意:我已经更改了工作表名称,因为它们与敏感数据有关。 However an example is "S&C vendor1 " 但是,一个示例是“ S&C vendor1

Be sure to spell worksheet names properly: 确保正确拼写工作表名称:

Sub dural()
   Set wb = ActiveWorkbook
   wb.Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Select
End Sub

There is no space in the default worksheet name. 默认工作表名称中没有空格。

Thanks for the help everyone. 感谢大家的帮助。 It turns out that I accidently had it inside a loop and must have been selecting multiple times and then ran out of range. 事实证明,我不小心将其放入循环中,并且必须进行多次选择然后超出范围。

Thanks for all the help anyway. 无论如何,感谢您的所有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM