VBA 运行时错误“9”:下标超出范围;尝试激活另一个工作簿
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18193153/
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
VBA Run-time error '9': Subscript out of range; trying to activate another workbook
提问by Lou
I'm trying to create a VBA in Excel 2010 that takes info from another spreadsheet that I'm not allowed to alter and bring it over to the spreadsheet with my macro built in. Here's my code:
我正在尝试在 Excel 2010 中创建一个 VBA,该 VBA 从另一个电子表格中获取信息,我不允许更改并将其带入带有内置宏的电子表格中。这是我的代码:
Sub BringUpWorkbook()
Workbooks("RITE 1624.xls").Activate
End Sub
I have several VBA books, have visited dozens of sites on the Internet, including those here at stackoverflow.com, and cannot find a reason why I'm receiving the run-time error. The workbook is already open, I've tried adding everything trailing the title, I've tried removing the .xls
, I've even did all of the above with in a variable. Any ideas?
我有几本 VBA 书籍,访问了 Internet 上的数十个站点,包括 stackoverflow.com 上的站点,但找不到收到运行时错误的原因。工作簿已经打开,我尝试添加标题后面的所有内容,我尝试删除.xls
,我什至在变量中完成了上述所有操作。有任何想法吗?
回答by Jaycal
Make sure the extension is correct. If it's a excel-2010 file like you indicate, you may need to update your code to reflect the extension of your "RITE 1624" file (e.g. .xlsx for a 2010 Excel workbook, .xlsm for a 2010 Excel macro-enabled workbook, or whatever the extension is.
确保扩展名正确。如果它是您指出的 excel-2010 文件,您可能需要更新代码以反映“RITE 1624”文件的扩展名(例如 .xlsx 表示 2010 Excel 工作簿,.xlsm 表示 2010 Excel 启用宏的工作簿,或任何扩展名。
Sub BringUpWorkbook()
Workbooks("RITE 1624.xlsx").Activate
End Sub
EDIT:
编辑:
To make sure you have the right name of the workbook, you can print the name of each workbook in an immediate window.
为了确保您拥有正确的工作簿名称,您可以在即时窗口中打印每个工作簿的名称。
Open up the VBA editor, and then press Ctrl+G to open the Immediate Window (or do View > Immediate window). Then run the following Macro:
打开 VBA 编辑器,然后按 Ctrl+G 打开即时窗口(或执行查看 > 即时窗口)。然后运行以下宏:
Sub OpenWkbkNames()
For Each Workbook In Workbooks
Debug.Print Workbook.Name
Next
End Sub
Note that this will give you the names of all the open workbooks in the same Excel instance as your macro. If your RITE 1624 file is in a separate Excel instance, then the instance that your macro is in will not be able to see that file (and it won't appear in the output of the OpenWkbkNames code above). The simplest way to resolve this is to open all of your necessary files from the Excel instance that contains your macro.
请注意,这将为您提供与您的宏相同的 Excel 实例中所有打开的工作簿的名称。如果您的 RITE 1624 文件在单独的 Excel 实例中,那么您的宏所在的实例将无法看到该文件(并且它不会出现在上面的 OpenWkbkNames 代码的输出中)。解决此问题的最简单方法是从包含宏的 Excel 实例中打开所有必需的文件。
Sub BringUpWorkbook()
Workbooks.Open("RITE 1624.xls").Activate
End Sub