|
根据网上大师提供的代码,修改了一个多簿多表合并的代码,但在运行时总是有如图提示,不知如何取消?还清各位大师出手相助。
方便的话请在原MsgBox增加一个运行时间的提示,已合并多少工作簿,多长时间。
代码如下:
Sub 合并()
Dim MyPath, MyName, AWbName
Dim Wb As Workbook, WbN As String
Dim G As Long, k As Integer
Dim Num As Long
Dim BOX As String
Dim i As Integer, x As Integer
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("B" & Rows.Count).End(xlUp).Row + 2, 1) = Left(MyName, Len(MyName) - 4)
For G = 1 To Sheets.Count
'k = k + 1
If Sheets(G).Name <> "代码" Then
'If k = 1 Then '如果第一次复制,就进行复制标题
' Wb.Sheets(G).UsedRange.Copy .Cells(.Range("B" & Rows.Count).End(xlUp).Row + 1, 1)
' Else '否则就下移一行.Offset(1),1为标题实际行数
Wb.Sheets(G).UsedRange.Offset(1).Copy .Cells(.Range("B" & Rows.Count).End(xlUp).Row + 1, 1)
End If
Next
WbN = WbN & Chr(13) & Wb.Name
Wb.Close False
End With
End If
MyName = Dir
Loop
Range("B1").Select
Application.ScreenUpdating = True
i = Range("b1500").End(xlUp).Row
For x = i To 2 Step -1
If Range("b" & x).Value = "合计" Then
Range("b" & x).EntireRow.Delete
End If
If Range("b" & x).Value = "" Then
Range("b" & x).EntireRow.Delete
End If
Next x
MsgBox "共合并了" & Num & "个工作薄下的全部工作表。如下:" & Chr(13) & WbN, vbInformation, "提示"
End Sub
测试.rar
(118.43 KB, 下载次数: 76)
|
|