1、新建一个文件夹、将要合并的表格放到里面、新建一个表格、用excel打开、右击sheet1
2、选择查看代码(ps:excel有着一项,wps没有)
3、将下列代码复制到文本框中:
sub 合并当前目录下所有工作簿的全部工作表()
dim mypath, myname, awbname
dim wb as workbook, wbn as string
dim g as long
dim num as long
dim box as string
application.screenupdating = false
mypath = activeworkbook.path
myname = dir(mypath & "\" & "*.xls")
awbname = activeworkbook.name
num = 0
do while myname <> ""
if myname <> awbname then
set wb = workbooks.open(mypath & "\" & myname)
num = num + 1
with workbooks(1).activesheet
.cells(.range("b65536").end(xlup).row + 2, 1) = left(myname, len(myname) - 4)
for g = 1 to sheets.count
wb.sheets(g).usedrange.copy .cells(.range("b65536").end(xlup).row + 1, 1)
next
wbn = wbn & chr(13) & wb.name
wb.close false
end with
end if
myname = dir
loop
range("b1").select
application.screenupdating = true
msgbox "共合并了" & num & "个工作薄下的全部工作表。如下:" & chr(13) & wbn, vbinformation, "提示"
end sub
4、点击运行、一段时间(取决于表格的大小和多少)后,合并完成了。 |