PrivateFunction filelist(folderspec, Optional pstr = "*.txt") OnErrorGoTo errline Dim fs, f, f1, fc, i, farr Set fs = CreateObject("Scripting.FileSystemObject") Set f = fs.GetFolder(folderspec) Set fc = f.Files ReDim farr(1To fc.Count) ForEach f1 In fc If f1.Name Like pstr AndNot f1.Name Like"*~$*"Then i = i + 1 farr(i) = f1.Name EndIf Next ReDimPreserve farr(1To i) filelist = farr errline: EndFunction
Dim t AsDouble Dim wb As Workbook Application.ScreenUpdating = False
t = Timer
k = 1 For i = 1To UBound(fileAr) Set wb = Workbooks.Open(filePath & fileAr(i)) With wb.Worksheets(1) k = k + 1 For j = 1To5 ThisWorkbook.Worksheets("Sheet1").Cells(k, j) = .Range("c" & j + 4) Next EndWith wb.Close False Next i Application.ScreenUpdating = True
Sub 方法二数组() Dim i, j, k Dim filePath AsString, fileAr Dim br(1To5000, 1To5) filePath = ThisWorkbook.Path & "\表单\" fileAr = filelist(filePath, "*.xlsx")
Dim t AsDouble Dim wb As Workbook Application.ScreenUpdating = False
t = Timer
k = 0 Dim tmp For i = 1To UBound(fileAr) Set wb = Workbooks.Open(filePath & fileAr(i))
With wb.Worksheets(1) tmp = .Range("c5:c9") k = k + 1 For j = 1To5 br(i, j) = tmp(j, 1) Next EndWith wb.Close False Next i ThisWorkbook.Worksheets(1).[a2].Resize(k, UBound(br, 2)) = br Application.ScreenUpdating = True
Sub 方法三() Dim i, j, k Dim filePath AsString, fileAr Dim br(1To5000, 1To5) filePath = ThisWorkbook.Path & "\表单\" fileAr = filelist(filePath, "*.xlsx")
Dim t AsDouble Dim wb As Workbook Application.ScreenUpdating = False
t = Timer
k = 0 Dim tmp For i = 1To UBound(fileAr) k = k + 1 For j = 1To5 br(i, j) = Application.ExecuteExcel4Macro("'" & filePath & "[" & fileAr(i) & "]信息卡'!" & Range("c" & j + 4).Address(, , xlR1C1)) Next Next i ThisWorkbook.Worksheets(1).[a2].Resize(k, UBound(br, 2)) = br Application.ScreenUpdating = True