|
5学分
本帖最后由 rglxjrglxj 于 2014-5-3 20:25 编辑
因来源是word文档无法执行,菜鸟不会断句修改,求助大虾。
DimarrFiles(1000)AsString DimcntFilesAsInteger SubcountCharsInDir() Dimi,posAsInteger,S~mFold,kerAsString DimfsoAs”NewFileSystemObject,fdAsFolder DimxlAppAsObject DimxlBookAsObject DimxlSheetAsObject DimtotalChars(1O00)AsInteger DimtotalrowsAsInteger DimtotalcolumnsAsInteger DimselectedFileAsString ‘显示文件选择对话框,获取用户选择的文件 Sheet1.CommonDialog1.一 GotFocus selectedFile=Sheet1.CommonDialog1.Filename ‘从用户选择的文件中,获得文件所在的目录路径(不含文 件的名字) pos=InStrRev(selectedFile,”Len(selectedFile),. vbTextCompare) StartFolder=Left(Sheet1.CommonDialog1.Filename,pos-1) Debug.PrintStartFolder ?cntFiles=0 Setfd:fso.GetFoldert~StartFolder) SearchFilesfd’将文件夹中的所有exeel-I-作簿都存放到 数组arrFiles中 ?从数组中读出这些excel工作簿,并创建excel对象,进 行统计字数操作 SetxlAppCreateObject(“Exce1.Application”1 Fori=1ToentFiles SetxlBook=xlApp.Application.Workbooks.Open(arrFiles(i)) toudChars(i’)=0 ‘遍历工作簿中的每个工作表 DimjAsInteger j=xlBook.Sheets.Count Fork=1T0j SetxlSheet=xlBook.Workshee~(k) totalrows=xlSheetUsedRangeBowsCount’获取最大行数 totalcolumns=xlSheet.UsedRange.Cohmns.Count’获取最大 列数 Iftotalrows>1Ortotalcolumns>1Then xlSheet.Range(“A”&totalrows+1).FormulaArray.”=SUM 0~EN(R[“&一totalrows&”】C:R卜1】C【..&totalcolumns一1&”]))” totaEhars(i)=totalChars(i)+xlSheetaRange~’A”&totaLrows+1) EndIf Nextk xlBook.Saved=True xlBook.Close Nexti ‘创建新的工作簿,然后将统计的数据写入到新创建的工作 表中 SetxlBook=xlApp.Application.Workbooks.Add SetxlSheet=xlBook.Worksheets(1) Fori=1TocntFiles xlSheet.Range(“A”&i)=arrFiles(i) xlSheet.Range(“B”&i)=totalChars(i) Nexti savefilename=Application.GetSaveAsFilename(“total”,” Excelfiles(*.xls),.】(1s”) x.1Book.SaveAsFilename:=savefilename.FileFormat:= xlWorkbookNormal xlBook.Close xlApp.Quit EndSub SubSearchFiles(ByValfdAsFolder) DimflAsFile DimsfdAsFolder ForEachflInfd.Files ⅡInStrRev(Ⅱ.Name,”.xls”,Len(n.Name),vbTextCompare)> 0卟en cntFiles=cntFiles+1 arrFiles(entFiles)=f1.Path EndIf Nextfl Ⅱfd.SubFolders.Count=OThen ExitSub EndIf ForEachsfdInfd.SubFolders SearchFilessfd Next EndSub |
|