可以VBA,新建一个EXCEL,在sheet2的A1单元格输入要你存了N个文件的文件夹路径:例如 h:\明细表\, 然后在sheet2建一个CommandButton按钮,把以下代码复制到按钮里面,点按钮后,程序会先把路径下文件夹的文件名写到sheet2的的A列,再把这些所有的文件的C2-G200的内容汇总到sheet1中.
Private Sub CommandButton1_Click()
On Error Resume Next
Application.ScreenUpdating = False
Dim sh As Worksheet
Range("a2:b65536").ClearContents
mypath = Trim(Range("a1"))
If Right(mypath, 1) <> "\" Then
mypath = mypath & "\"
n = 2
myfile = Dir(mypath & "\*.*")
Do While myfile <> ""
Cells(n, 2) = myfile
Cells(n, 1) = mypath & myfile
myfile = Dir
n = n + 1
Loop
If n = 2 Then Exit Sub '如果N=2,则表示路径下没有提取到xls文件
For j = 2 To n - 1
Workbooks.Open Trim(Range("a" & j))
With ThisWorkbook.Worksheets("sheet1")
row1 = .Range("b65536").End(xlUp).Row
Workbooks(Trim(Range("b" & j))).Worksheets("sheet1").Range("b2:g100").Copy .Range("b" & row1 + 1)
End With
Workbooks(Trim(Range("b" & j))).Close
Next j
Application.ScreenUpdating = True
End Sub
追问谢谢你
但是建立好了,但是会按下按键后表格就死掉了。
追答刚刚则试修改了,可以运行的:,
Private Sub CommandButton1_Click()
On Error Resume Next
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim sh As Worksheet
Range("a2:b65536").ClearContents
mypath = Trim(Range("a1"))
If Right(mypath, 1) <> "\" Then mypath = mypath & "\"
n = 2
myfile = Dir(mypath & "\*.*")
Do While myfile <> ""
Cells(n, 2) = myfile
Cells(n, 1) = mypath & myfile
myfile = Dir
n = n + 1
Loop
If n = 2 Then Exit Sub '如果N=2,则表示路径下没有提取到xls文件
For j = 2 To n - 1
Workbooks.Open Trim(Range("a" & j))
With ThisWorkbook.Worksheets("sheet1")
row1 = .Range("b65536").End(xlUp).Row
Workbooks(Trim(Range("b" & j))).Worksheets("sheet1").Range("b2:g100").Copy .Range("b" & row1 + 1)
End With
Workbooks(Trim(Range("b" & j))).Close
Next j
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub