【发布时间】: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
有人对此错误有任何解决方案/建议吗?谢谢!
【问题讨论】: