我以前用那个来回答你的问题。
它将文件名复制到您要查找的文件所在的目录中。
您将文件放在一张 (FILES) 中,您可以选择要合并的文件。
合并后的文件将位于 (DB) 数据表中。
我的工作簿名为 CopyDb,但您可以自定义它。
Sub CopyDb()
Dim xRg, xCell As Range
Dim xVal As String
Dim MyPath, MyFileName, Aux As String
Dim x
Dim LastRow, LastCol As Long
Set wsDb = ThisWorkbook.Worksheets("DB")
Set wsFiles = ThisWorkbook.Worksheets("FILES")
x = Shell("cmd /k type nul > list.txt", vbHide)
x = Shell("cmd /k dir /A:-D /b > list.txt", vbHide)
MyPath = ActiveWorkbook.Path
MyFileName = "list.txt"
Workbooks.OpenText Filename:=MyPath & "/list.txt" _
, Origin:=xlWindows, StartRow:=1, DataType:=xlFixedWidth, FieldInfo:= _
Array(0, 2), TrailingMinusNumbers:=True
Windows("list.txt").Activate
ActiveSheet.Range(ActiveSheet.Cells(1, 1), ActiveSheet.Cells(ActiveSheet.UsedRange.Rows.Count, 1)).Copy
Windows("list.txt").Close
wsFiles.Activate
wsFiles.Cells(1, 1).Activate
wsFiles.Paste
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
wsFiles.Application.CutCopyMode = False
Selection.NumberFormat = "@"
x = Shell("cmd /k del list.txt /q", vbHide)
Set xRg = Application.InputBox("Please select the file names:", , _
ActiveWindow.RangeSelection.Address, , , , , 8)
If xRg Is Nothing Then Exit Sub
For Each xCell In xRg
xVal = xCell.Value
If TypeName(xVal) = "String" And xVal <> "" Then
Workbooks.Open (MyPath & "\" & xVal)
Windows(xVal).Activate
With ActiveWorkbook.ActiveSheet
Range(.Cells(1, 1), .Cells(.UsedRange.Row + .UsedRange.Rows.Count - 1, _
.UsedRange.Column + .UsedRange.Columns.Count - 1)).Copy
End With
ActiveWorkbook.Close
Windows("CopyDb.xlsm").Activate
LastRow = wsDb.UsedRange.SpecialCells(xlCellTypeLastCell).Row
wsDb.Activate
wsDb.Cells(LastRow + 1, 1).Select
wsDb.Paste
wsDb.Application.CutCopyMode = False
End If
Next
End Sub
希望对你有帮助