‘選擇一個目錄,將目錄中的所有EXCEL文件導入當前工作表
‘這些EXCEL文件最好格式能一樣,這里是每個文件是同一個格式
Sub 批量()
Dim FD, str$, arr
Set FD = Application.FileDialog(msoFileDialogFolderPicker)
If FD.Show = -1 Then t = FD.SelectedItems(1) Else Exit Sub ‘如果沒選擇文件夾則退出
Application.ScreenUpdating = False
Cells.NumberFormatLocal = "@"
str = Dir(t & "\*.xl*") ‘查找格式為EXCEL的文件
While Len(str) > 0 ‘文件名不為空時
Workbooks.Open (t & IIf(Right(t, 1) = "", "", "") & str) ‘打開工作簿
With ActiveWorkbook.ActiveSheet
.Range(.Cells(2, "l"), .Cells(.[a65536].End(3).Row, "l")) = "’" & Left(str, Len(str) – IIf(Right(str, 1) = "x", 5, 4))
arr = .UsedRange
Workbooks(str).Close False ‘關閉工作薄
Kill (t & IIf(Right(t, 1) = "", "", "") & str) ’刪除工作薄(如果不刪除,省去這一步)
End With
With ActiveSheet
rw = .[a65536].End(3).Row + 1
.Cells(rw, 1).Resize(UBound(arr, 1), UBound(arr, 2)) = arr ‘將數據寫入當前工作表
End With
str = Dir() ‘查找下一個文件
Wend
If [a1] = "" Then Rows(1).Delete ‘如果A1為空,刪除第一行
Application.ScreenUpdating = True
End Sub