vba 将多个名为成员账户明细的execl文件,合并至结果表中
时间: 2024-02-11 15:04:54 浏览: 127
你可以使用VBA编写一个宏,来将多个名为“成员账户明细”的Excel文件,合并至一个结果表中。以下是示例代码:
```
Sub MergeFiles()
Dim folderPath As String
Dim selectedFiles() As Variant
Dim i As Integer
Dim j As Integer
Dim nextRow As Long
Dim src As Workbook
Dim dest As Workbook
Dim sht As Worksheet
'选择要合并的文件夹
folderPath = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1)
'列出所有名为“成员账户明细”的文件
selectedFiles = Filter(Split(CreateObject("WScript.Shell").Exec("CMD /C DIR """ & folderPath & "\成员账户明细*.xlsx"" /B /S").StdOut.ReadAll, vbCrLf), ".xlsx")
'打开结果表
Set dest = Workbooks.Add
Set sht = dest.Worksheets(1)
nextRow = 1
'合并所有文件
For i = 0 To UBound(selectedFiles)
Set src = Workbooks.Open(selectedFiles(i))
With src.Worksheets(1)
'复制数据到结果表中
.UsedRange.Copy sht.Cells(nextRow, 1)
nextRow = nextRow + .UsedRange.Rows.Count
End With
src.Close False
Next i
'格式化结果表
sht.Cells.EntireColumn.AutoFit
sht.Cells(1, 1).Select
MsgBox "合并完成!"
End Sub
```
你只需要将上述代码复制到一个新的模块中,然后运行它即可。请注意,代码中的文件名和工作表名称可能需要根据你的实际情况进行修改。
阅读全文