明细代码如下所示(直接复制可用):
Sub 合并当前工作簿下的所有工作表()
On Error Resume Next
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set st = Worksheets.Add(before:=Sheets(1))
st.Name = "合并"
For Each shet In Sheets:
If shet.Name <> &#34;合并&#34; Then
i = st.Range(&#34;A&#34; & Rows.Count).End(xlUp).Row + 1
shet.UsedRange.Copy
st.Cells(i, 1).PasteSpecial Paste:=xlPasteAll
End If
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
MsgBox &#34;已完成&#34;
End Sub方法三 Power Query
Power Query是Excel2016及其以上才有的功能,如果你的Excel版本较低,强烈建议安装较高版本的,可看历史文章,有详细的安装教程。
首先我们需要将Excel表格文件另存为CSV文件,这一步可以通过VBA批量操作。
打开任意工作簿,调出Visual Basic 界面,输入以下代码(如何插入代码上面有介绍,这里不再赘述)
Sub xlsxtocsv()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
t = ActiveWorkbook.Name
mypath = ActiveWorkbook.Path & &#34;\&#34;
myfile = Dir(mypath & &#34;*.xlsx&#34;)
Do Until Len(myfile) = 0
If myfile <> t Then
Workbooks.Open Filename:=mypath & myfile
ActiveWorkbook.SaveAs Filename:=mypath & Left(myfile, InStr(myfile, &#34;.&#34;) - 1) & &#34;.csv&#34;, FileFormat:=xlCSV
End If
If myfile <> t Then ActiveWorkbook.Close
myfile = Dir
Loop
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub执行完毕后,将会将每一个表格生成一份csv文件。
VBA运行方法都是一致的。 新建一个工作簿,打开,进入Visual Basic 界面,输入以下代码,点击运行,等待程序完成之后,工作簿完成合并。
Sub 合并当前目录下所有工作簿()
Dim Wb As Workbook
Application.ScreenUpdating = False
MyPath = ActiveWorkbook.Path
MyName = Dir(MyPath & &#34;\&#34; & &#34;*.xlsx&#34;)
AWbName = ActiveWorkbook.Name
Do While MyName <> &#34;&#34;
If MyName <> AWbName Then
Set Wb = Workbooks.Open(MyPath & &#34;\&#34; & MyName)
c = ThisWorkbook.Sheets(&#34;sheet1&#34;).UsedRange.SpecialCells(xlCellTypeLastCell).Row
If c = 1 Then &#39;防止合并的工作簿第一行空着
c = 0
End If
ActiveSheet.UsedRange.Copy ThisWorkbook.Sheets(&#34;sheet1&#34;).Cells(c + 1, 1) &#39;合并工作簿的第一个sheet名字为:sheet1
Wb.Close False
End If
MyName = Dir
Loop
Application.ScreenUpdating = True
MsgBox &#34;已完成&#34;
End Sub方法三 Power Query