
請協助編寫公式以從 300 個關閉的工作簿中提取資料(我不想打開這 300 個工作簿中的任何一個)。項目描述: 有300張Excel時間表。需要將每個時間表的範圍 (A1:AC51) 複製到主計算工作簿中,計算工資單,然後將工資單結果複製到同一主工作簿中的另一個工作表中。這裡的技巧是在不開啟 300 個 Excel 時間表的情況下完成此操作。我編寫了下面的程式碼,但我不知道如何編寫公式以從關閉的工作簿中提取數據,請幫忙?再次,
- 有 300 個 excel 時間表(關閉的工作簿),Sheets("EmpInput")
- 有一個主要的計算工作簿(開啟),Sheets(“Main”)
- 目標:從每個時間表複製完全相同的範圍,計算工資,將結果複製到另一張表,Sheets("Results")
先非常感謝!
Public Sub GetTimesheetData()
Dim fsoFileObject As New Scripting.FileSystemObject
Dim employeeTimesheet As File
Dim folderPath As String
Dim nextEmpty As Long
folderPath = "C:\GatherTimesheets\" '<<<<< There are 300 excel timesheets in this folder.
For Each employeeTimesheet In fso.folderPath.Files '<<<< Is this correct?
If empoyeeTimesheet.Name Like "*.xls" Then
Sheets("Main").Select
With Range("A1:AC51")
.Formula = "='C:\GatherTimesheets\" & employeeTimesheet '<<<< How to write this formula??
.Value = .Value
End With
nextEmpty = Sheets("Results").Range("D65444").End(xlUp).Row + 1
Sheets("Main").Range("CS1:DF1").Copy
With Sheets("StagingRaw").Range("D" & nextEmpty)
.PasteSpecial xlValues
Application.CutCopyMode = False
End With
End If
Next employeeTimesheet
End Sub
答案1
格式為='C:\Path\[SourceFileName.xlsx]SourceSheetName'!A1:Z9
.
嘗試Range("A1:AC51").FormulaArray = "='C:\GatherTimesheets\[" & employeeTimesheet & "]EmpInput'!A1:AC51"
。
欲了解更多信息,請參閱Range.FormulaArray 屬性 (Excel)和建立指向另一個工作簿中的儲存格區域的外部參考(連結)。另外,使用選項顯式以避免拼字錯誤,例如empoyeeTimesheet
.