我有一個文件夾,其中包含許多文件,我需要:打開本週的文件,將它們存儲在一個數組中,將它們傳遞給一個子文件,並通過它們循環以獲取摘要信息。在Excel VBA宏中的數組
我可以從下面的代碼中獲取所需的日期文件。但是,代碼拋出一個錯誤,將其存儲在數組中並將其傳遞給數組。
Sub BatchProcessing()
firstday = Date - Weekday(Date) + 2 'To get the 1st day of week
lastday = Date - Weekday(Date) + 6 'To get the 5th day of week
MyPath = "P:\Data\" 'Path where my files were present
Dim Day
Dim strArray(0 To 5) As String
iCount=0
For Day = firstday To lastday 'To loop through all 5 day files
formatted_date = Format(Day, "yyyyMd")
MyTemplate = "TestFile" & formatted_date & ".xlsx" ' Set the template.
Workbooks.Open MyPath & MyTemplate
strArray(iCount) = ActiveWorkbook.Name
iCount = iCount+1
Next
CreateStats(strArray) 'Calling a sub which will do the required calculation
End Sub
Sub CreateStats(strArray As String)
For Each element in strArray
set OriginalWorkbook = strArray(i)
'Do the processing'
Next
End Sub
什麼是錯誤? – Sam 2013-03-18 20:53:03
@Sam:我現在編輯了數組聲明。但是,上面的代碼現在將數組中的名稱存儲?以及如何傳遞它另一個功能? – Jill448 2013-03-18 21:23:38
您應該在代碼中設置一個斷點,以查看數組是否按照您的預期填充,並且還要查看這些尺寸是否正確。要將它傳遞給子過程,必須將過程更改爲接受數組作爲參數。 – Sam 2013-03-18 21:31:13