|
KCCKSCF 发表于 2014-9-24 09:16
非常好用,十分感谢!
大侠,能否做到:只是将希望汇总的分表放到一个文件夹中,不用打开各分表即可汇总 ...
已弄完,参见附件
Sub 汇总()
Dim myPath As String
Dim myFile As String
Dim wk As Workbook
Dim sht As Worksheet
Dim Workbook_name As String
Dim Sheet_name As String
Dim ActSheet_name As String
Dim hmax As Integer
Dim cmax As Integer
Dim rng As Range
Dim i As Integer
Dim j1 As Integer
Dim j2 As Integer
Application.ScreenUpdating = False
Workbook_name = ActiveWorkbook.Name
ActSheet_name = ActiveSheet.Name
Sheet_name = "总表" '每个Workbook中有一个 Sheet(总表),程序会将其中的信息进行汇总
myPath = ThisWorkbook.Path & "\分表\"
myFile = Dir(myPath & "*.xls")
Do While myFile <> ""
If myFile <> Workbook_name Then
Set wk = Workbooks.Open(myPath & myFile)
For Each sht In Sheets
hmax = Workbooks(Workbook_name).Sheets(ActSheet_name).UsedRange.Rows.Count
If sht.Name = Sheet_name Then
cmax = ActiveSheet.UsedRange.Columns.Count
For i = 1 To cmax
If ActiveSheet.Cells(1, i) = "姓名" Then
j1 = i
ElseIf ActiveSheet.Cells(1, i) = "月度奖金" Then
j2 = i
End If
Next
Set rng = Union(ActiveSheet.Cells(2, j1).Resize(ActiveSheet.Range("A65535").End(xlUp).Row - 1, 1), ActiveSheet.Cells(2, j2).Resize(ActiveSheet.Range("A65535").End(xlUp).Row - 1, 1))
rng.Copy Workbooks(Workbook_name).Sheets(ActSheet_name).Range("A" & hmax + 1)
Set rng = Nothing
End If
Next
Workbooks(myFile).Close False
End If
myFile = Dir
Loop
Application.ScreenUpdating = True
MsgBox "汇总完毕!", 64, "提示"
End Sub
|
|