简体   繁体   English

如何在VBA中打开并激活另一个工作簿?

[英]How to open and activate another workbook in VBA?

I'm creating a macro where I will need to run it in 1 file (called "Masterfile"), it will open and execute the macro on another file ("SurveyReport") and then give me a message box saying "done!". 我正在创建一个宏,我需要在1个文件中运行它(称为“Masterfile”),它将打开并在另一个文件(“SurveyReport”)上执行宏,然后给我一个消息框说“完成!” 。

The code I have to execute on the SurveyReport file works fine when I open that file manually and execute it. 当我手动打开该文件并执行它时,我必须在SurveyReport文件上执行的代码工作正常。 The code I need to open SurveyReport from MasterFile is also working it seems, I ran the below with no issues: 我需要从MasterFile打开SurveyReport的代码似乎也正常工作,我运行下面没有任何问题:

    Sub PivotTable()
    '
    ' PivotTable Macro

    Dim MasterFile As String

    MasterFile = ActiveWorkbook.Name

    Dim SurveyReport As String

    SurveyReport = Application.GetOpenFilename("Excel files (*.xlsx), *xlsx", 1, "Please select the Survey Create Report file", , False)

    Workbooks.Open (SurveyReport)


    End Sub

But, when I try to activate the SurveyReport file so I can begin executing the macro in it, I get a "Subscript out of range" error. 但是,当我尝试激活SurveyReport文件以便我可以开始在其中执行宏时,我收到“下标超出范围”错误。 I've tried using the following code after the above block and before the code to execute in the SurveyReport file: 我已经尝试在上面的块之后和在SurveyReport文件中执行代码之前使用以下代码:

    Windows(SurveyReport).Activate

This didn't work, not did: 这不起作用,没有做到:

    ThisWorkbook.Activate

...which only had the effect of activating the MasterFile. ......只有激活MasterFile的效果。

SurveyReport file is a .xlsx file. SurveyReport文件是.xlsx文件。 I tried saving it as a .xls file and amending the code, but no joy. 我尝试将其保存为.xls文件并修改代码,但没有快乐。 I also tried passing it the file name directly (ie Windows("filename.xlsx").Activate), same issue. 我也尝试直接传递文件名(即Windows(“filename.xlsx”)。激活),同样的问题。

ActiveWorkbook is as it says on the tin - whichever workbook happens to be active when the code runs. ActiveWorkbook就像它在锡上所说的那样 - 无论哪个工作簿在代码运行时都是活动的。
ThisWorkbook is always the workbook that the code is sitting in. ThisWorkbook 始终是代码所在的工作簿。

You can SET references to specific workbooks rather than just using their names each time. 您可以设置对特定工作簿的引用,而不是每次只使用它们的名称。 A name can change, or reference the wrong object.... imagine you have a friend called Darren. 一个名字可以改变,或引用错误的对象....想象你有一个叫Darren的朋友。 Each time you mention him you mention him by name. 每次你提到他,你都会提到他的名字。 Someone that doesn't know Darren hasn't a clue which Darren out of all the ones available in the world you're talking about. 一个不了解达伦的人并不知道达伦在你所谈论的世界上所有可用的方面。 Now imagine you have a little replica of Darren in your pocket... nah, that's a terrible anology - it wouldn't be a replica, it would be a reference to the real Darren... anyway, I digress. 现在想象一下你的口袋里有一个Darren的复制品......不,这是一个可怕的神学 - 它不会是复制品,它会引用真正的Darren ......无论如何,我离题了。

This code sets a reference to the workbook, you can then use that reference any time you want to refer to the correct workbook: 此代码设置对工作簿的引用,然后您可以在任何时候使用该引用来引用正确的工作簿:

Sub PivotTable()

    Dim MasterFile As Workbook

    Dim SurveyRptName As String
    Dim SurveyReport As Workbook

    Set MasterFile = ThisWorkbook '

    SurveyRptName = Application.GetOpenFilename("Excel files (*.xlsx), *xlsx", 1, _
        "Please select the Survey Create Report file", , False)
    If SurveyRptName <> "False" Then
        Set SurveyReport = Workbooks.Open(SurveyRptName)
    End If

    SurveyReport.Activate 'You don't need this line.  It doesn't matter if
                          'the workbook is active, the code knows which one
                          'you're talking about in the next line.
    MsgBox "This is " & SurveyReport.Name & _
        " containing " & SurveyReport.Worksheets.Count & " sheets." & vbCr & _
        "The value in cell A1 of the first sheet is " & _
            SurveyReport.Worksheets(1).Range("A1")

End Sub  

Edit: Of course, if you press Cancel when selecting a file then the lines following the IF...THEN code won't have a reference to work on and you'll get a Object Variable or With block variable not set - best not to run the bottom bit of code if you haven't successfully opened the Survey Report file. 编辑:当然,如果你在选择一个文件时按下取消,那么IF...THEN代码后面的行将没有工作参考,你将得到一个Object Variable or With block variable not set - 最好不要如果尚未成功打开Survey Report文件,则运行底部的代码。

The part of the answer that is missing - is that he tried to call a method of an object when his variable was STRING - the open command and subsequent commands will give you an OBJECT - which has properties and methods like .Activate. 缺少答案的部分 - 是当他的变量是STRING时,他试图调用一个对象的方法 - 打开命令和后续命令会给你一个OBJECT - 它具有像.Activate这样的属性和方法。 The STRING type has no such method or property (to be sure - it may have others). STRING类型没有这样的方法或属性(可以肯定 - 它可能有其他方法)。 the solution provided by Darren solves this by declaring his SurveyReport as type Workbook - an object of Excel. Darren提供的解决方案通过将他的SurveyReport声明为类型工作簿(Excel的一个对象)来解决这个问题。

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

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