Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 3457|回复: 4

[已解决](加大难度)如何对若干个EXCEL文件按科目内容汇总?

[复制链接]
发表于 2014-2-26 22:00 | 显示全部楼层 |阅读模式
15学分
本帖最后由 shenlong2006 于 2014-2-27 20:30 编辑

上午发了一个帖子(http://www.excelpx.com/thread-319786-1-2.html),当时为了举例方便,所有的示例表格数据区域的大小都是一样的,但实际工作中各个表格数据区域的大小事不一样的,所以那个帖子选为“最佳答案”的代码就无法满足要求了,下面重新编辑了各个文件,难度更大了,重新发下!

有若干个地区的报表文件(如下图)

01.jpg

每个报表都是按科目分类统计的(格式如下图)

02.jpg

我现在想把所有的地区按科目进行汇总,科目相同的汇在一起,比如我将北京、天津汇在一起效果如下:

03.jpg

请问这个怎么通过VBA实现呢?
附件上传供大家研究:

汇总.rar (54.38 KB, 下载次数: 3)

最佳答案

查看完整内容

增加一句代码就行了 Sub Macro1() Dim wb As Workbook, rng As Range Dim x&, y&, mypath$, myfile$, s&, s2& Application.ScreenUpdating = False Application.DisplayAlerts = False Application.StatusBar = "正在汇总………" ActiveSheet.UsedRange.Clear mypath = ThisWorkbook.Path & "\" myfile = Dir(mypath & "*.xls") Do While myfile "" If myfile ThisWorkbook.Name Then Set wb = GetO ...
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2014-2-26 22:00 | 显示全部楼层
增加一句代码就行了


Sub Macro1()
Dim wb As Workbook, rng As Range
Dim x&, y&, mypath$, myfile$, s&, s2&
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.StatusBar = "正在汇总………"
ActiveSheet.UsedRange.Clear
mypath = ThisWorkbook.Path & "\"
myfile = Dir(mypath & "*.xls")
Do While myfile <> ""
    If myfile <> ThisWorkbook.Name Then
        Set wb = GetObject(mypath & myfile)
        x = Range("j65536").End(xlUp).Row
        n = wb.Sheets(1).Range("l65536").End(xlUp).Row
       wb.Sheets(1).Range("a5:l" & n).Copy Cells(IIf(x = 1, 1, x + 1), 1)
        wb.Close 0
    End If
    myfile = Dir
Loop
ActiveSheet.UsedRange.UnMerge
y = ActiveSheet.UsedRange.Rows.Count
[b1].Resize(y, 1).SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
ActiveSheet.UsedRange.Sort Key1:=[b1], Order1:=xlAscending, Header:=xlGuess
Set rng = [b1]: [a1] = 1
s2 = 1: [c1] = 1
For i = 2 To y + 1
    If Cells(i, 2) = Cells(i - 1, 2) Then
        s2 = s2 + 1
        Cells(i, 3) = s2
        Set rng = Union(rng, Cells(i, 2))
    Else
        s2 = 1: s = s + 1
        rng.Offset(, -1).Merge
        rng.Merge
        Set rng = Cells(i, 2)
        Cells(i, 3) = s2
        rng.Offset(, -1) = s + 1
    End If
Next
Cells(y + 1, 1) = ""
Cells(y + 1, 3) = ""
[1:4].Insert Shift:=xlDown
Set wb = GetObject(mypath & "北京.xls")
wb.Sheets(1).Range("A1:L4").Copy [a1]
wb.Close 0
Application.StatusBar = "已完成"
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
回复

使用道具 举报

发表于 2014-2-27 16:42 | 显示全部楼层
………………

汇总.zip

71.95 KB, 下载次数: 15

回复

使用道具 举报

 楼主| 发表于 2014-2-28 16:56 | 显示全部楼层
dsmch 发表于 2014-2-26 22:00
增加一句代码就行了

老师,我若是想在后面一列填上各个文件的名称,请问该怎么弄呢?

QQ图片20140228165236.jpg
回复

使用道具 举报

发表于 2014-2-28 19:01 | 显示全部楼层    本楼为最佳答案   
Sub Macro1()
Dim wb As Workbook, rng As Range
Dim x&, y&, mypath$, myfile$, s&, s2&
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.StatusBar = "正在汇总………"
ActiveSheet.UsedRange.Clear
mypath = ThisWorkbook.Path & "\"
myfile = Dir(mypath & "*.xls")
Do While myfile <> ""
    If myfile <> ThisWorkbook.Name Then
        Set wb = GetObject(mypath & myfile)
        x = Range("j65536").End(xlUp).Row
        n = wb.Sheets(1).Range("l65536").End(xlUp).Row
       x2 = IIf(x = 1, 1, x + 1)
        wb.Sheets(1).Range("a5:l" & n).Copy Cells(x2, 1)
       Cells(x2, "m").Resize(n - 4, 1) = wb.Name
        wb.Close 0
    End If
    myfile = Dir
Loop
ActiveSheet.UsedRange.UnMerge
y = ActiveSheet.UsedRange.Rows.Count
[b1].Resize(y, 1).SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
ActiveSheet.UsedRange.Sort Key1:=[b1], Order1:=xlAscending, Header:=xlGuess
Set rng = [b1]: [a1] = 1
s2 = 1: [c1] = 1
For i = 2 To y + 1
    If Cells(i, 2) = Cells(i - 1, 2) Then
        s2 = s2 + 1
        Cells(i, 3) = s2
        Set rng = Union(rng, Cells(i, 2))
    Else
        s2 = 1: s = s + 1
        rng.Offset(, -1).Merge
        rng.Merge
        Set rng = Cells(i, 2)
        Cells(i, 3) = s2
        rng.Offset(, -1) = s + 1
    End If
Next
Cells(y + 1, 1) = ""
Cells(y + 1, 3) = ""
[1:4].Insert Shift:=xlDown
Set wb = GetObject(mypath & "北京.xls")
wb.Sheets(1).Range("A1:L4").Copy [a1]
wb.Close 0
Application.StatusBar = "已完成"
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-20 16:08 , Processed in 0.315322 second(s), 11 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表