打开closures的工作簿并运行macros
我试图打开不同的Excel工作簿,运行各自的macros,保存并closures。 到目前为止,我有这个代码如下:
Sub AllFiles() Application.Run "'L:\RESEARCH\Alternative Assets\Private Equity\PE Preqin Data\Preqin_Fundraising_Data_Macro.xlsm'!Get_File" End Sub
这个代码的问题是,虽然它打开工作簿,我不断收到运行时错误'9'下标超出范围。 我的猜测是macros无法find“活动”工作簿内的某些工作表或variables。 这里是我得到错误的代码(另一个工作簿中的macros)
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
任何人有任何解决scheme/build议这个错误? 谢谢!
使用ThisWorkbook而不是ActiveWorkbook。 那么你应该可以运行一个远程macros。
Set Sheet = ThisWorkbook.Sheets("Macro_Button")
注意:我总是build议使用ThisWorkbook
而不是ActiveWorkbook
因为它更加明确。 你永远无法确定哪个是活动的工作簿。 但是ThisWorkbook
总是指的是持有macros的工作簿。
另一个说明(基于您的下面的评论):上述代码不是用于几个工作簿。 这不够明确。 例:
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文件中工作。
- MIMEtypesXLSX(来自LibreOffice)
- Python Win32Com – ExportAsFixedFormat – MS Excel 2010文件
- 如何将相同的IF语句应用于同一列中的多个单元格? (EXCEL)
- 在weblogic上使用Apache poi 3.13导出xlsx:文件格式或扩展名无效
- 分割和join一个string的分隔符添加一个数字作为每Excel的计数
- Excel FORMULA:查找单元格中的所有字符是否在另一个单元格中,即使在另一个单元格中它们是多个字符
- Excel – 将数据从一个范围匹配到另一个范围,并从第三个范围或原始数据返回另一个值
- IFfunction通过select单元格
- SQL Server BCP实用程序