Excel VBA多工作簿多工作表汇总实例集锦.docx
《Excel VBA多工作簿多工作表汇总实例集锦.docx》由会员分享,可在线阅读,更多相关《Excel VBA多工作簿多工作表汇总实例集锦.docx(127页珍藏版)》请在课桌文档上搜索。
1、1,多工作表汇总(ConSOlidate)两种写法都要求地址用RlCI形式,各个表格的数据布置有规定。SubConsolidatcWorkbookODimRangeArrayOAsStringDimbksWorksheetDimshtAsWorksheetDimWbCountAsIntegerSetbk=SheetS(汇总WbCount=Sheets1CountReDimRangeArrayI1ToWbCount-1)ForEachshtInSheetsIfsht.Name汇总Theni=i+1RangeArrayfi)=&sht.Name&_sht.Range(Al)CurrentRegio
2、n.Address(ReferenceStyle:=xlRlCl)EndIfNextbk.Range(A1).ConsolidateRangeArray,xlSum,True,Trueal.Value=姓名ForEachbkInWorkbooks在全部工作簿中循环IfNotbkIsThisWorkbookThen非代码所在工作簿Setsht=bk.Workshcets引用工作簿的第一个工作表i=i+1RangeArray(i)=&bk.Name&sht.Name&,!&_sht.Range(A).CurrentRegion.Address(ReferenceStyle=xlRlCl)EndIf
3、NextWorksheets(I).Range(A1n).Consolidate_RangeArray,xlSum,True,TrueEndSub3,多工作傅汇总O*2007-1-1.html#help汇总表.xlsSubpld11vbO5310汇总表xls导入指定文件的数据DimmyFsAs.=*.xlsIf.Execute(SortByi=TnsoSortBy)0Thenn=.Foundcoll=2RcDimmyfile(1Ton)AsStringFori=1Tonmyfile(i)=.FoundFiles(i)=myfile(i)aa=InStrRev(,)nm=Right(,1.en(
4、)-aa)nml=1.eft(nm,1.en(nm)-4)Ifnmlv”汇总表ThenWorkbooks.Openmyfilc(i)DimwbAsWorkbookSetwb=ActiveWorkbookForEachshInSheetsS=s&sh.Name&JNextS=1.eft(s,1.en(三)-1)ar=Split(s,UserForm1.ShowForj=OToUBound(arl)IfErr.Number=9ThenGoTo100Setsh=wb.Sheets(arl(j)sh.Activatem=sh.a65536.End(xlUp).Rowarr=Range(CelIS(3,
5、3),Cells(m,3)Sht1.Activatecoil=coll+1Cells(2,coll)=sh.alCells(3,colIJ.FormulaRlCl=&nm&T&arl(j)&!RC3显示引用的工作簿工作表与单元格地址Cells(3,coll).AutoFillRange(Cells(3,coll),Cells(UBound(arr)+2,coll)iCells(3,coll).Resize(UBoundfarr),1)=arrNextj100:wb.CloseSaVeChanges:=FalSeSetwb=NothingS=IfVarType(arl)=8200ThenEras
6、earlEndIfElseMsgBox该文件夹里没有任何文件”EndIfEndWitha1.SelectSetmyFs=NothingApplication-ScreenUpdating=TrueEndSubPrivateSubCommandButtonl_Click()Fori=OTo1.istBoxl.1.istCount-1If1.istBox1.Selectedfi)=TrueThens=s&1.istBoxl.1.ist(i)&,EndIfNextiIfsThens=1.eft(s,1.en(三)-1)arl=Split(s,MsgBox你选择了&sUnloadUserFormlEl
7、semg=MsgBoXr你没有选择任何工作表!须要重新选择吗?,VbYesNo,”提示)Ifmg=6ThenElseUnloadUserFormlEndIfEndIfEndSubPrivateSubCommandBUtton2_CliCk()UnloadUserForm1EndSubPrivateSubUserFormJnitializeOWithMe.1.istBoxl.1.ist=ar文本框赋值.1.istStyle=1文本前加选择小方框.MultiSelect=1设置可多选EndWithMe.1.abell.Caption=Me.1.abell.Caption&nmEndSub4,多工作
8、表汇总(字典、Mffi)*Data多表汇总0623.xlsSubdbhz()多表汇总DimShtlAsWorksheet,Sht2AsWorksheet,ShtAsWorksheetDimdfk,t,Myr&,Arr,xApplication-ScreenUpdating=FalseApplication.DisplayzMerts=FalseSetd=CreateobjectCScripting.Dictionary)ForEachShtInSheets删除同名的表格,获得要增加的汇总表格不重且名字IfInStr(Sht.Name,OThenSht.Delete:GoTo100nm=Mid(
9、Sht.a3,7)d(nm)=100:NextShtApplication-DisplayAlerts=Truek=d.keysFori=0ToUBound(k)Sheets.Addafter:=Sheets(Sheets.Count)SetShtl=ActiveSheetShtkName=Replace(k(i),增加汇总表,把名字中的“(不能用作表名的)改为-“NextiErasekSetd=NothingForEachShtInSheetsWithSht.ActivateIfInStr(.Name,0Thennm=RCPlaCe(MidUa3,7),Myr=.h65536.End(xlU
10、p).RowArr=.RangefdlOrh&Myr)Setd=CreateObject(Scripting.Dictionary)Fori=1ToUBound(Arr)x=Arr(i,1)IfNotd.exists(x)Thend.Addx,Arr(i,5)Elsed(x)=d(x)+Arr(i,5)EndIfk=d.keyst=d.itemsSetSht2=Sheets(nm)Sht2.Activatemyr2=a65536.End(xlUp).Row+1Ifmyr2OThenn=.FoundReDimmyfile(lTon)AsStringFori=1Tonmyfile(i)=.Foun
11、dFiles(i)=myfile(i)nml=Split(Mid(,InStrRev(,)+D,)(0)Ifnml=wbnmThenGoTo200Workbooks.Openmyfile(i)DimwbAsWorkbookSetwb=ActiveWorkbookForEachshInSheetsIfInStr(sh.Name,aa)Thensh.Activatemm=mm+1Brrbz(mm,1)=b2.ValueForj=2To18Step2Ifj10ThenBrrbz(mm,j)=CellsQ/2+34,1l).ValueElseBrrbz(mm,j)=Cells(j/2+34,9).Va
12、lueEndIfNextGoTo100ElseIfIb2=ThenGoTo50mm=mm+1Brrgr(mm,1)三b2.ValueBrrgr(mm,2)=e38).ValueBrrgr(mm,3)=i38.ValueForj=4To18Step2Ifj12ThenBrrgr(mm,j)=Cells(j/2+38,8).ValueElseBrrgr(mm,j)=Cells(j/2+38,7).ValueEndIfNextForj=20To23Brrgr(mm,j)=Cells(j+28,8).ValueNextEndIfEndIf50:Next100:wb.CloseSavechangesi=
13、FalseSetwb=Nothing200:NextElseMsgBox”该文件夹里没有任何文件”EndIfEndWitha2.Resize(mm,19)=BrrbzElsea2.Rcsize(mm,23)=BrrgrEndIfa1.SelectSetmyFs=NothingEndSub*2011-7-15OThenn=.FoundReDimBrr(lTon,1To2)ReDimmyfile(lTon)AsStringFori=1Tonmyfile(i)=.FoundFilesfi)=myfile(i)aa=InStrRev(,)nm=Right(,1.en()-aa)带后缀的Excel文件名
14、Ifnmnm2Thenj=j+1Workbooks.Openmyfile(i)DimwbAsWorkbookSetWb=ActiveWorkbookSetsh=wb.Shcets(Sheet1n)Brr(j,1)=nmApplication.ScreenUpdating=FalseSetShtl=ActiveSheet:nn=5Shtl.(b5:e27|=SetmyFs=Application.myPath=ThisWorkbook-Path&data指定的子文件夹内搜寻WithmyFs.NewSearch.1.ookIn=myPath.=mso.=*.xls.SearchSubFolders
15、=TrueIf.ExecutefSortBy:=msoSortBy)OThenn=.FoundRcDimmyfile(1Ton)AsStringFori=1Tonmyfilc(i)=.FoundFilcs(i)=myfile(i)nml=split(mid(,)+l),.)(O)一句代码代替以下3句aaHInStrReV(,、)带后缀的Excel文4nm=Right(,1.enO-aa)件名EndIfme=d65536.End(xlUp).RowIfme7Then第7行是表头Ifme11Thenme=11只要取4行数据Forii=8TomeShtl.Cclls(nn,2).Resized,3)
16、=Cells(ii,4).Resize(l,3).ValueShtl.Cells(nn,5)=Cells(ii,8).Valuenn=nn+1NextiiGoTo100ElseGoTo100EndIf100:Nextshwb.CloseSaVeChanges:=FaISeSetwb=NothingEndIfNextElseMsgBox”该文件夹里没有任何文件”EndIfEndWitha1.SelectSetmyFs=NothingApplication-ScreenUpdating=TrueEndSubsum.xlsSubpldrsj0724()批地导入指定文件的数据DimmyFsAs,myf
17、ile,Myrl&,ArrDimmyPath$,$,nm2$Dimi&,j&,n&,nn&,aa$,nm$,nml$DimShtlAsWorksheet,shAsWorksheetApplication-ScreenUpdating=FalseSetShtl=ActiveSheetMyrl=Sht1.a65536.End(xlUp).RowArr=Shtl.Rangc(a3:b&Myrl)Sht!.RangeCbSib*&Myr1).ClearContentsnm21.eft(ctiveWorkbook.Name,1.en(ActiveWorkbook-Name)-4)SetmyFs=Appl
18、ication.myPath三ThisWorkbook-PathWithmyFs.NewSearch.1.ookIn=myPath.=mso.=*.xlsIf.Execute(SortByi=InsoSortBy)OThenn=.FoundReDimmyfile(1Ton)AsStringFori=1Tonmyfile(i)=.FoundFiles(i)=myfile(i)aa=InStrRev(,)nm=Right(,()-aa)带后缀的Excel文件名nml=1.eft(nm,1.en(nm)-4)去除后缀的Excel文件名Ifnmlnm2ThenEndWithSht1.Selectb3)
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Excel VBA多工作簿多工作表汇总实例集锦 VBA 工作 汇总 实例 集锦

链接地址:https://www.desk33.com/p-1504550.html