|
代码如下:- Sub 合并目录下所有工作簿()
- Dim MyPath, MyName, AWbName, titleRow
- Dim wb As Workbook, WbN As String
- Dim G As Long
- Dim Num As Long
- Dim T
- T = Timer
- Application.ScreenUpdating = False
- titleRow = InputBox("请输入标题行数", "默认行数", "3") '默认标题行数
- If titleRow = "" Then Exit Sub
- wjj = InputBox("请输入文件夹名称", "默认文件夹", "拆分文件") '需汇总的文件夹
- If wjj = "" Then Exit Sub
- Set ws = ThisWorkbook
- Set sh = ws.Sheets("汇总表")
- MyPath = ThisWorkbook.Path & "" & wjj
- If Len(Dir(MyPath, vbDirectory)) = 0 Then MkDir MyPath ' MkDir创建目录或文件夹
- MyName = Dir(MyPath & "" & "*.xls*")
- Sheets("汇总表").UsedRange.Offset(titleRow, 0).Clear '清除汇总表原数据保留标题
- Num = 0
- Do While MyName <> ""
- If MyName <> ws.Name Then
- Set wb = Workbooks.Open(MyPath & "" & MyName)
- Num = Num + 1
- With wb.Sheets(1)
- MaxRow = .UsedRange.Rows.Count
- MaxCol = .UsedRange.Columns.Count
- r = Cells(Rows.Count, 1).End(3).Row
- .Range("A" & titleRow + 1 & ":E" & MaxRow).Copy sh.Cells(r + 1, 1)
- WbN = WbN & Chr(13) & wb.Name
- wb.Close False
- End With
- End If
- MyName = Dir
- Loop
- With sh
- MaxRow2 = .Cells(Rows.Count, 1).End(3).Row '行数
- .Cells(MaxRow2 + 1, 1).Value = "合计"
- .Cells(MaxRow2 + 1, 2).Value = Application.WorksheetFunction.Sum(.Range(.Cells(titleRow + 1, 2), .Cells(MaxRow2 + 1, 2)))
- .Range("B1").Select
- Application.ScreenUpdating = True
- MsgBox "用时" & Format((Timer - T), "0.0000") & "秒,共合并了" & Num & "个工作薄如下:" & Chr(13) & WbN, vbInformation, "提示"
- ' 添加边框()
- MaxCol = .UsedRange.Columns.Count '获取列数
- Range(.Cells(titleRow, 1), .Cells(MaxRow2 + 1, MaxCol)).Select
- With Selection.Borders
- .LineStyle = xlContinuous
- .ColorIndex = xlAutomatic
- .TintAndShade = 0
- .Weight = xlThin
- End With
- End With
- End Sub
复制代码
|
|