【问题标题】:Open Closed Workbook and Run Macro打开关闭的工作簿并运行宏
【发布时间】:2016-05-28 03:57:17
【问题描述】:

我正在尝试打开不同的 Excel 工作簿,运行它们各自的宏,保存并关闭。到目前为止,我有以下代码:

Sub AllFiles()

    Application.Run "'L:\RESEARCH\Alternative Assets\Private Equity\PE Preqin Data\Preqin_Fundraising_Data_Macro.xlsm'!Get_File"

End Sub

此代码的问题在于,尽管它打开了工作簿,但我不断收到运行时错误“9”下标超出范围。我的猜测是宏无法在“活动”工作簿中找到某些工作表或变量。这是我得到错误的代码(另一个工作簿中的宏)

Public Sub Get_File()

    Dim sFiletype As String     'Fund type reference
    Dim sFilename As String     'File name (fund type + date of download), if "" then default
    Dim sFolder As String       'Folder name (fund type), if "" then default
    Dim bReplace As Boolean     'To replace the existing file or not
    Dim sURL As String          'The URL to the location to extract information
    Dim pURL As String
    Dim Cell, Rng As Range
    Dim Sheet As Worksheet

    Dim oBrowser As InternetExplorer
    Set oBrowser = New InternetExplorer

    'Initialize variables
    Set Rng = Range("I2:I15")
    Set Sheet = ActiveWorkbook.Sheets("Macro_Button") 'POINT OF ERROR

    For Each Cell In Rng
        If Cell <> "" Then
        sFiletype = Cell.Value
        sFilename = sFiletype & "_" & Format(Date, "mmddyyyy")
        sFolder = Application.WorksheetFunction.VLookup(Cell.Value, Sheet.Range("I2:Z15"), 2, False)
        bReplace = True
        sURL = "www.preqin.com"
        pURL = Application.WorksheetFunction.VLookup(Cell.Value, Sheet.Range("I2:Z15"), 16, False)

        'Download using the desired approach, XMLHTTP / IE
            If Application.WorksheetFunction.VLookup(Cell.Value, Sheet.Range("I2:Z15"), 15, False) = 1 Then
            Call Download_Use_IE(oBrowser, sURL, pURL, sFilename, sFolder, bReplace)
            Else
            Call Download_NoLogin_Use_IE(oBrowser, pURL, sFilename, sFolder, bReplace)
            End If

        Else: GoTo Exit_Sub
        End If
    Next

Exit_Sub:

    'Close IE
    oBrowser.Quit

End Sub

有人对此错误有任何解决方案/建议吗?谢谢!

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    使用 ThisWorkbook 而不是 ActiveWorkbook。那么你应该可以运行远程宏了。

    Set Sheet = ThisWorkbook.Sheets("Macro_Button")
    

    注意:我总是建议使用ThisWorkbook 而不是ActiveWorkbook,因为它更明确。您永远无法确定哪个是活动工作簿。但ThisWorkbook 总是指持有宏的工作簿。

    另一个注意事项(基于您的以下 cmets):上面的代码并非设计用于与多个工作簿一起使用。它不够明确。示例:

    Set Rng = Range("I2:I15")
    

    在这一行中,您并没有说您指的是哪个工作表甚至工作簿。因此,VBA 为您做出决定。我猜VBA“决定”你不想要的东西。因此,您必须告诉 VBA 您想要什么并明确说明。以下是有关如何更改上述行的一些可能示例。

    Set Rng = ThisWorkbook.Sheets(1).Range("I2:I15") 'This will be a sheet in the remote Excel file
    Set Rng = ActiveWorkbook.Sheets(1).Range("I2:I15") 'This will be a sheet in the file from which you were originally calling the external macro
    Set Rng = Workbooks("someFile.xls").Sheets("someSheetInThatFile").Range("I2:I15") 'This would be a third Excel file: not the one calling the macro nor the Excel file hosting the macro
    

    然而,这只是上述代码中的一行,您必须对其进行更改才能使其适用于多个 Excel 文件。

    【讨论】:

    • 如果您从Personal.xlsb 运行宏,ThisWorkbook 是否实际上与ActiveWorkbook 相同?或者将使用ThisWorkbook 引用Personal.xls
    • 感谢拉尔夫的快速反应 - 我在上面添加了其余的子。当我将其更改为 ThisWorkbook 时,它只是跳过其余代码并退出。也许还有什么我需要改变的?
    • 我根据您的评论和我认为可能有问题的地方详细阐述了我的回答。
    • 正要提到我认为是范围。这完美地工作。感谢您的帮助和快速响应!注意:对于未来的用户,我使用了 ThisWorkbook.Sheet.Range(我在变量“Sheet”中指定了它是哪个工作簿)
    猜你喜欢
    • 1970-01-01
    • 2015-06-02
    • 1970-01-01
    • 1970-01-01
    • 2018-07-27
    • 2016-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多