简体   繁体   English

VBA运行时错误'9':下标超出范围;试图激活另一个工作簿

[英]VBA Run-time error '9': Subscript out of range; trying to activate another workbook

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,它从另一个我不允许更改的电子表格中获取信息,并将其带入内置宏的电子表格。这是我的代码:

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. 我有几本VBA书籍,访问过互联网上的几十个网站,包括那些在stackoverflow.com上的网站,并且找不到我收到运行时错误的原因。 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. 工作簿已经打开,我已经尝试添加标题后面的所有内容,我已经尝试删除.xls ,我甚至在变量中完成了上述所有操作。 Any ideas? 有任何想法吗?

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 (eg .xlsx for a 2010 Excel workbook, .xlsm for a 2010 Excel macro-enabled workbook, or whatever the extension is. 如果它是您指定的excel-2010文件,则可能需要更新代码以反映“RITE 1624”文件的扩展名(例如,对于2010 Excel工作簿的.xlsx,对于2010 Excel宏启用的工作簿,.xlsm,或者扩展名是什么。

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). 打开VBA编辑器,然后按Ctrl + G打开立即窗口(或执行视图>立即窗口)。 Then run the following Macro: 然后运行以下宏:

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 . 请注意,这将为您提供与宏相同的Excel实例中所有打开的工作簿的名称。 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). 如果您的RITE 1624文件位于单独的Excel实例中,则宏所在的实例将无法看到该文件(并且它不会出现在上面的OpenWkbkNames代码的输出中)。 The simplest way to resolve this is to open all of your necessary files from the Excel instance that contains your macro. 解决此问题的最简单方法是从包含宏的Excel实例中打开所有必需的文件。

Sub BringUpWorkbook()

    Workbooks.Open("RITE 1624.xls").Activate
End Sub

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

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