我有一个宏,可以从特定文件夹打开电子表格,并将输出保存到另一个工作簿中名为 Sheet1 的工作表中。如果文件名称为“MyFile.xls”,则该宏有效,但我希望它能够在任何文件名上运行,但它必须具有“Book2”工作表。
这是我的代码:
Dim source As Workbook
Dim output As Workbook
Dim sourceSheet as WorkSheet
Dim outputSheet as WorkSheet
Dim file As String
file = "C:\Spreadsheets\MyFile.xls" 'I would like it to handle any files from any location'
Set output = ThisWorkBook
output.Activate
If Len(Dir$(file)) > 0 Then
Set source = workbooks.Open(file)
Set sourceSheet = source.Worksheets("Book2") 'Must only run if the sheet is called Book2'
Set outputSheet = output.Worksheets("Sheet1") 'Saves sheets into a new sheet called Sheet1'
End Sub