你在這裏。
創建一個新的BLANK工作簿並將這些過程放置在一個標準的代碼模塊中。保存這個新文件,然後運行Fuji()
。
Public Sub Fuji()
Dim c&, sPath$, sFile$, v, wsReport As Worksheet
On Error Resume Next
sPath = "c:\tmp\fiji\" '<-- Edit source file folder and INCLUDE final backslash.
ActiveSheet.Copy
Set wsReport = ActiveSheet
wsReport.Name = "Merged"
DoEvents
sFile = Dir(sPath & "*.*")
SetExcelEnvironment 1
Do
Application.StatusBar = "Processing... " & sPath & sFile
With Workbooks.Open(sPath & sFile)
With .Worksheets(1)
v = .Range(.[a1], .Cells(.Rows.Count, "a").End(xlUp))
With wsReport.Cells(.Rows.Count, "a").End(xlUp)(2).Resize(UBound(v))
.Value = v
.Offset(, 1) = sFile
End With
End With
.Close 0
End With
sFile = Dir
Loop Until sFile = ""
With wsReport
.Rows(1).Delete
.Cells.EntireColumn.AutoFit
End With
SetExcelEnvironment 0
End Sub
Private Sub SetExcelEnvironment(bProcessing As Boolean)
With Application
.DisplayAlerts = Not bProcessing
.ScreenUpdating = Not bProcessing
.StatusBar = ""
.DisplayStatusBar = bProcessing
End With
End Sub
注:此假定僅列A將被收集到報告文件和源文件的名稱將B列報
注:這是假定所有的文件位於同一個文件夾中,並且您可以在Fuji()
例程頂部附近的sPath行上編輯該源文件夾的位置。
注意:這裏假設源文件夾只包含用這個過程將被剔除(並被Excel理解)的文件。
注意:這裏假定所有的源文件數據都來自第一張表。
UPDATE
基於多列數據的更新的要求,請使用此版本:
Public Sub Fuji()
Dim c&, sPath$, sFile$, v, wsReport As Worksheet
On Error Resume Next
sPath = "c:\tmp\fiji\" '<-- Edit this and INCLUDE final backslash.
sFile = Dir(sPath & "*.*")
ActiveSheet.Copy
Set wsReport = ActiveSheet
wsReport.Name = "Merged"
DoEvents
SetExcelEnvironment 1
Do
Application.StatusBar = "Processing... " & sPath & sFile
With Workbooks.Open(sPath & sFile)
With .Worksheets(1)
v = .[a1].CurrentRegion.Resize(.Cells(.Rows.Count, "a").End(xlUp).Row)
With wsReport.Cells(.Rows.Count, "a").End(xlUp)(2).Resize(UBound(v, 1), UBound(v, 2))
.Value = v
.Offset(, UBound(v, 2)).Resize(, 1) = sFile
End With
End With
.Close 0
End With
sFile = Dir
Loop Until sFile = ""
With wsReport
.Rows(1).Delete
.Cells.EntireColumn.AutoFit
End With
SetExcelEnvironment 0
End Sub
Private Sub SetExcelEnvironment(bProcessing As Boolean)
With Application
.DisplayAlerts = Not bProcessing
.ScreenUpdating = Not bProcessing
.StatusBar = ""
.DisplayStatusBar = bProcessing
End With
End Sub
看起來像一個基本的VBA的鍛鍊。請嘗試對其進行編碼,顯示您的代碼,許多人將準備好幫助您把它帶到最後。 –
是否有一個文件夾中的所有文件?而且,這些文件夾中只有這些文件? –
是的,所有文件都在一個文件夾中,文件夾中沒有其他文件 – fuji2015