直接使用请粘贴如下的代码:
SubRun()
Dimtar_wbAsWorkbook
Settar_wb=CreateWorkbook
CallMergeContent(tar_wb)
EndSub
‘函数名:CreateWorkbook
‘接受参数:无
‘返回值:Workbook(返回创建的Workbook)
‘说明:创建一个Excel文件,存放合并的数据
PrivateFunctionCreateWorkbook()AsWorkbook
DimfileNameAsString
DimfilePathAsString
DimnowDateAsString
nowDate=CDate(Now())
nowDate=Replace(nowDate,”:”,””)
nowDate=Replace(nowDate,”/”,””)
nowDate=Replace(nowDate,””,”_”)
filePath=ThisWorkbook.path&”\”
fileName=filePath&nowDate&”_汇总表.xlsx”
DimnewBookAsWorkbook
SetnewBook=Workbooks.Add
WithnewBook
.SaveAsfileName
EndWith
SetCreateWorkbook=newBook
EndFunction
‘函数名:MergeContent
‘接受参数:targetWorkbook(合并后的数据存放的Workbook对象)
‘返回值:无
‘说明:将数据依次粘贴到目标Workbook对象、即EXCEL中。
PrivateFunctionMergeContent(targetWorkbookAsWorkbook)
Sheet1.Range(Sheet1.Cells(1,1),Sheet1.Cells(1,1).End(xlToRight)).CopytargetWorkbook.Sheets(“Sheet1”).Range(“A65536”).End(xlUp)
ForEachshtInThisWorkbook.Worksheets
sht.Range(“A1”).CurrentRegion.Offset(1,0).CopytargetWorkbook.Sheets(“Sheet1”).Range(“A65536”).End(xlUp).Offset(1,0)
Next
targetWorkbook.CloseTrue
EndFunction
代码贴上来真得好丑,强烈建议悟空问答优化一下。。TT