[英]VBA Excel Workbooks Object Variable
I am trying to set two workbook variables - here are the first four lines of my code: 我正在尝试设置两个工作簿变量-这是我的代码的前四行:
Dim wb1 As Workbook
Dim wb2 As Workbook
Set wb1 = Workbooks("C:\Users\ShaneM\Documents\File1.xlsx")
Set wb2 = Workbooks("C:\Users\ShaneM\Documents\Computer Languages\VBA\File2.xlsm")
The error I am getting on line 3 is: 我在第3行上遇到的错误是:
Runtime error 9 - Subscript Out Of Range
The files definitely exist in these locations. 这些文件肯定存在于这些位置。 Obviously much learning to be done.
显然有很多事情要做。 What am I doing wrong, please?
请问我做错了什么?
Thank you. 谢谢。
The .Workbooks()
is a collection of workbook objects opened in the current Application
object (which you don't write in your code, but it's called by default). .Workbooks()
是在当前Application
对象中打开的工作簿对象的集合(您没有在代码中编写该代码,但是默认情况下会调用它)。
The collection .Workbooks()
goes out of range
because the workbook is not currently open in your Application
; .Workbooks()
集合out of range
因为该工作簿当前未在您的Application
打开; if you want to use your workbook, in fact, what you need to do is opening it into the current Excel Application
instance: 实际上,如果您想使用工作簿,则需要将其打开到当前的Excel
Application
实例中:
Set wb1 = Workbooks.Open("C:\...")
This will though cause the workbook to show up, because you're opening in your current application, and unfortunately is not possible to open it in Visible=False
mode. 但是,这将导致工作簿显示,因为您正在当前应用程序中打开,遗憾的是无法在
Visible=False
模式下打开它。 However, you might want to use a new Excel instance set to Visible = False
, so you will be able to read the content without actually displaying it. 但是,您可能希望使用设置为
Visible = False
的新Excel实例,因此您将能够在不实际显示内容的情况下读取其内容。 Which means, in code: 这意味着,在代码中:
Set excObj = CreateObject("Excel.Application")
excObj.Visible = False
Set wb1 = excObj.Workbooks.Open("C:\...")
Please note that I'm specifying to open the workbook with excObj
, which is not the current Application
but another object of the same type that is intentionally set to Visible = False
. 请注意,我指定使用
excObj
打开工作簿,它不是当前的Application
而是另一个故意设置为Visible = False
的相同类型的对象。
According to https://msdn.microsoft.com/en-us/library/office/ff841074.aspx , the Workbooks
collection contains only the currently opened workbooks. 根据https://msdn.microsoft.com/zh-cn/library/office/ff841074.aspx ,
Workbooks
集合仅包含当前打开的工作簿。 The error simply tells you, that the requested workbook is not opened at the moment. 该错误只是告诉您,当前尚未打开所请求的工作簿。
You have to use the method Workbooks.Open : 您必须使用Workbooks.Open方法:
Set wb1 = Workbooks.Open("C:\Users\ShaneM\Documents\File1.xlsx")
When the Workbook is already opened, you can then use the property Workbooks.Item to get the object (note that you can omit .Item
, as it is the default property of the Workbooks
collection). 当已经打开工作簿时,您可以使用属性Workbooks.Item来获取对象(请注意,您可以省略
.Item
,因为它是Workbooks
集合的默认属性)。
Set wb1 = Workbooks("File1.xlsx")
Also note that the names of opened workbooks must be unique within the same application. 还要注意,打开的工作簿的名称在同一应用程序中必须唯一。 Ie the following will not work:
也就是说,下将无法正常工作:
Set wb1 = Workbooks.Open("C:\Folder1\File.xlsx")
Set wb2 = Workbooks.Open("C:\Folder2\File.xlsx")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.