|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
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
With ThisWorkbook.ActiveSheet
.UsedRange.Clear
Do While MyName <> ""
If MyName <> AWbName Then
Set wb = Workbooks.Open(MyPath & "\" & MyName)
Num = Num + 1
mc = Left(MyName, Len(MyName) - 4)
r = .Cells(Rows.Count, 1).End(xlUp).Row + 1
If r = 2 Then
r = 1
Else
r = r
End If
For G = 1 To Sheets.Count
m = m + 1
If m = 1 Then
wb.Sheets(G).UsedRange.Copy .Cells(r, 2)
rs = .Cells(Rows.Count, 2).End(xlUp).Row
.Range("a" & r + 1 & ":a" & rs) = mc
.[a1] = "来源"
Else
wb.Sheets(G).UsedRange.Offset(1).Copy .Cells(r, 2)
rs = .Cells(Rows.Count, 2).End(xlUp).Row
.Range("a" & r & ":a" & rs) = mc
End If
Next
WbN = WbN & Chr(13) & wb.Name
wb.Close False
End If
MyName = Dir
Loop
.Range("A1").Select
End With
Application.ScreenUpdating = True
MsgBox "共合并了" & Num & "个工作薄下的全部工作表。如下:" & Chr(13) & WbN, vbInformation, "提示"
End Sub
|
评分
-
1
查看全部评分
-
|