|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
请教各位大神,工作中,多次从软件导出表格,里面有很多子文件夹,汇总很不方便,也看了liulang0808的帖子,看到可以用字典进行汇总,但是我没有这个需求,只需要合并即可,改了几次代码也无法成功,还请高手赐教,谢谢大家附上他的代码参考
附件
万州区12个污水处理厂项目.rar
(147.26 KB, 下载次数: 27)
- 五、汇总当前文件夹及子文件夹下所有excel文件内容
- 增加了红色字体部分
- Public d
- Sub 按钮1_Click()
- Application.ScreenUpdating = False
- ActiveSheet.UsedRange.ClearContents
- Cells(1, 1) = "编号"
- Cells(1, 2) = "数量"
- Set d = CreateObject("scripting.dictionary")
- Getfd (ThisWorkbook.Path) 'ThisWorkbook.Path是当前代码文件所在路径,路径名可以根据需求修改
- Application.ScreenUpdating = True
- If d.Count > 0 Then
- ThisWorkbook.Sheets(1).[a2].Resize(d.Count) = WorksheetFunction.Transpose(d.keys)
- ThisWorkbook.Sheets(1).[b2].Resize(d.Count) = WorksheetFunction.Transpose(d.items)
- End If
- d.RemoveAll
- End Sub
- Sub Getfd(ByVal pth)
-
- Set Fso = CreateObject("scripting.filesystemobject")
- Set ff = Fso.getfolder(pth)
- For Each f In ff.Files
- Rem 具体提取哪类文件,还是需要根据文件扩展名进行处理
- If InStr(Split(f.Name, ".")(UBound(Split(f.Name, "."))), "xl") > 0 Then
- If f.Name <> ThisWorkbook.Name Then
- Set wb = Workbooks.Open(f)
- For Each sht In wb.Sheets
- If WorksheetFunction.CountA(sht.UsedRange) > 1 Then
- arr = sht.UsedRange
- For j = 2 To UBound(arr)
- d(arr(j, 1)) = d(arr(j, 1)) + arr(j, 2)
- Next j
- End If
- Next sht
- wb.Close False
- End If
- End If
- Next f
- For Each fd In ff.subfolders
- Getfd (fd)
- Next fd
- End Sub
复制代码 链接 http://club.excelhome.net/forum.php?mod=viewthread&tid=1165866&page=1&authorid=238368
|
|