描述:同一文件夾下有多個工作簿,且表結構相同,需要把文件夾下的各個工作表的內容合並到同一工作簿中。
在文件夾下新建工作表,在新建工作表下插入宏。
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
運行宏合並數據
注:本次合並前提為合並內容表頭相同,下節出示表頭不同的解決方法。