方法一:VBA代码(代码写在统计工作簿的模块里):
优点:
一键完成
运算速度快
缺点:
代码不亲民导致修改困难(PS,这段代码其实还是把各分工作簿打开后提取的,如果完全不打开,需要创建ADO引用,不然代码无法运行,这一做法更不亲民。)
适用:
有一定VBA代码基础的使用者 Sub subtotal()
Dim MyFind As Range
Dim MyBook As Workbook
Dim MySheet As Long, i As Long, x As Long
Dim MyBName As String
Dim MyArr, MyRes Application.DisplayAlerts = False x = 2
Do
MyBName = Sheets("sheet1").Cells(2, x)
MyArr = Sheets("sheet1").Range("a3:a10")
Set MyBook = GetObject(ThisWorkbook.Path & "\" & MyBName & ".xls")
With Windows(MyBook.Name)
.Visible = True
.Activate
End With
ReDim MyRes(1 To 8, 0)
For i = 1 To 8
For MySheet = 1 To Sheets.Count
Set MyFind = Sheets(MySheet).Range("a:a").Find(what:=MyArr(i, 1), lookat:=xlWhole)
If Not MyFind Is Nothing Then
MyRes(i, 0) = MyRes(i, 0) + Sheets(MySheet).Cells(MyFind.Row, 7)
End If
Next MySheet
Next i
MyBook.Close
Sheets("sheet1").Range(Cells(3, x), Cells(10, x)) = MyRes
x = x + 1
Loop Until Sheets("sheet1").Cells(2, x) = "总计"
Set MyFind = Nothing
Set MyBook = Nothing
Application.DisplayAlerts = True
End Sub