帮我写一个合并当前文件夹下所有excel表格到一个工作表的vba代码
时间: 2023-02-19 17:51:37 浏览: 187
以下是在VBA中合并当前文件夹下所有Excel表格到一个工作表的代码:
```
Sub MergeExcelFiles()
Dim path As String
Dim filename As String
Dim wb As Workbook
Dim ws As Worksheet
Dim ws1 As Worksheet
Dim lRow As Long
Application.ScreenUpdating = False
path = ThisWorkbook.Path
filename = Dir(path & "\*.xl*")
Set ws = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
ws.Name = "Merged Data"
Do While filename <> ""
If filename <> ThisWorkbook.Name Then
Set wb = Workbooks.Open(path & "\" & filename)
Set ws1 = wb.Sheets(1)
lRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
ws1.Range("A1:Z" & ws1.Rows.Count).Copy
ws.Range("A" & lRow + 1).PasteSpecial xlPasteValues
wb.Close False
End If
filename = Dir()
Loop
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
```
将此代码复制到Excel的VBA编辑器中,然后按 `Alt + F11` 运行即可。
阅读全文