批量合并同一文件夹下工作簿-1

描述:同一文件夹下有多个工作簿,且表结构相同,需要把文件夹下的各个工作表的内容合并到同一工作簿中。

在文件夹下新建工作表,在新建工作表下插入宏。

 1 sub hbwb()
 2 
 3 dim r as integer,c as interger,r1 as integet,c1 as integer
 4 
 5 dim filename as string,filepath as string,workbook_name as string
 6 
 7 dim wb as workbook
 8 
 9 application.screenupdating=False
10 
11 filename=thisworkbook.name
12 
13 filepath=thisworkbook.path
14 
15 workbook_name=thisworkbook.name
16 
17 'msgbox(filename & chr(13) & filepath)
18 
19 na=dir(thisworkpath & "/.xlsx")
20 
21 do while na <> ""
22 
23   if na <> filename then
24 
25   in_row=sheets(1).range("a65536").end(xlup).row+1   '取得空行行号
26 
27   filepath_na=filename & '\' &na
28 
29   set wb = getobject(filepath_na)
30 
31   set sht =wb.sheets(1)
32 
33   r=1
34 
35   c=2
36 
37   r1=sht.range('a65536').end(xlup).row
38 
39   c1=sht.cells(1,200).end(xltoleft).column
40 
41   copy_cont=sht.range(sht.cells(1,1),sht.cells(r1,c1))
42 
43   sheets(1).cells(in_row).resize(r1,c1)=copy_cont
44 
45   end if 
46 
47   na=dir
48 
49 loop
50 set wb = nothing
51 application.screenupdating=True
52 
53 end

运行宏合并数据

 注:本次合并前提为合并内容表头相同,下节出示表头不同的解决方法。

posted @ 2019-04-08 20:21  竹心_兰君  阅读(1494)  评论(1编辑  收藏  举报