如你所愿,一篇文章搞定8种Excel多表统计!( 四 )


如你所愿,一篇文章搞定8种Excel多表统计!
文章图片


每一个工作簿 , 里面有N个工作表 , 全部需要合并起来 。
如你所愿,一篇文章搞定8种Excel多表统计!
文章图片


将代码所在工作簿放在同一个文件夹 。
如你所愿,一篇文章搞定8种Excel多表统计!
文章图片


点击头像运行 , 稍等片刻即可导入 , 调整列宽即可 。
如你所愿,一篇文章搞定8种Excel多表统计!
文章图片


源代码:
Sub 合并当前目录下所有工作簿的全部工作表()
Dim MyPath, MyName, AWbName
Dim Wb As Workbook, WbN As String
Dim G As Long, ShRcou As Long, ShCol As Integer
Dim Num As Long, WbSht As String

Application.ScreenUpdating = False
MyPath = ActiveWorkbook.Path
MyName = Dir(MyPath & "\" & "*.xls")
AWbName = ActiveWorkbook.Name
Num = 0

Do While MyName <> ""
If MyName <> AWbName Then
Set Wb = Workbooks.Open(MyPath & "\" & MyName)
Num = Num + 1
With Workbooks(1).ActiveSheet
【如你所愿,一篇文章搞定8种Excel多表统计!】
For G = 1 To Sheets.Count
WbSht = Replace(Left(MyName, Len(MyName) - 4), ".", "") & ";" & Wb.Sheets(G).Name
If .UsedRange.Count = 1 Then
ShCol = Wb.Sheets(G).UsedRange.Columns.Count
ShRcou = Wb.Sheets(G).UsedRange.Rows.Count
.Cells(1, ShCol + 1) = "合并名称"
.Cells(1, ShCol + 1).Font.Bold = 1
.Cells(.Cells(Rows.Count, 1).End(xlUp).Row + 1, ShCol + 1).Resize(ShRcou - 1) = WbSht
Wb.Sheets(G).UsedRange.Copy .Cells(.Cells(Rows.Count, 1).End(xlUp).Row, 1)
Else
ShRcou = Wb.Sheets(G).UsedRange.Rows.Count
.Cells(.Cells(Rows.Count, 1).End(xlUp).Row + 1, ShCol + 1).Resize(ShRcou - 1) = WbSht
Wb.Sheets(G).Cells(2, 1).Resize(ShRcou, ShCol).Copy .Cells(.Cells(Rows.Count, 1).End(xlUp).Row + 1, 1)
End If
Next
WbN = WbN & Chr(13) & Wb.Name
Wb.Close False
End With
End If
MyName = Dir
Loop
ActiveSheet.UsedRange.Borders.LineStyle = 1
Range("A1").Select
Application.ScreenUpdating = True

MsgBox "共合并了" & Num & "个工作薄下的全部工作表 。 如下:" & Chr(13) & WbN, vbInformation, "提示"
End Sub

卢子能想到的所有多表都汇总都在这里了 。
VIP
888
元 , 一次报名 , 所有视频课程 , 终生免费学 , 提供一年在线答疑服务 。


如你所愿,一篇文章搞定8种Excel多表统计!


推荐阅读