用vba把50个相同格式的excel文件汇总求和到一个excel文件里?
发布网友
发布时间:2024-07-03 02:10
我来回答
共2个回答
热心网友
时间:2024-10-27 06:37
新建立一个文件 夹,把这50个表格文件放进去,然后你在这个文件夹里再新建一个表格文件 打开 然后在sheet1那个标签处,右键 查看代码 然后把下面的代码复制进去,然后你点上面的运行 运行子过程,就可以了(注意这个方法只适用 于你准备合并的50个表中,不得有合并过的单元格,不然会出现错误的)
Sub 合并当前目录下所有工作簿的全部工作表()
Dim MyPath, MyName, AWbName
Dim Wb As Workbook, WbN As String
Dim G As Long
Dim Num As Long
Dim BOX 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
.Cells(.Range("B65536").End(xlUp).Row + 2, 1) = Left(MyName, Len(MyName) - 4)
For G = 1 To Sheets.Count
Wb.Sheets(G).UsedRange.Copy .Cells(.Range("B65536").End(xlUp).Row + 1, 1)
Next
WbN = WbN & Chr(13) & Wb.Name
Wb.Close False
End With
End If
MyName = Dir
Loop
Range("B1").Select
Application.ScreenUpdating = True
MsgBox "共合并了" & Num & "个工作薄下的全部工作表。如下:" & Chr(13) & WbN, vbInformation, "提示"
End Sub追问我每个文件都有12个工作表,其中表1是对其他11个表的汇总,如何能生产和原表一样的汇总表,能加您QQ请教吗,我的116636685,谢谢
热心网友
时间:2024-10-27 06:37
汇总表
数据文件夹
Sub 汇总()
Dim MyFiles, myPath, wb As Object
myPath = ThisWorkbook.Path & "\数据文件夹\"
MyFiles = Dir(myPath & "\*.xls")
Application.ScreenUpdating = False
Do While MyFiles <> ""
If MyFiles <> ThisWorkbook.Name Then
Set wb = GetObject(myPath & "/" & MyFiles)
'分表D5单元格
wb.Sheets(1).Range("d5").Copy
ThisWorkbook.Activate
'汇总表D5单元格
Range("d5").PasteSpecial Paste:=xlPasteValues, Operation:=xlAdd
wb.Close False
End If
MyFiles = Dir
Loop
Application.ScreenUpdating = True
End Sub追问我每个表格都有11个工作表,按您的代码执行还是不行,能加您QQ请教吗,我的116636685,谢谢