2,503
社区成员




Application.Visible=False
[/quote]
谢谢,赵大牛..Application.Visible=False
Option Explicit
Sub union()
Dim fso As FileSystemObject, tFolder As Folder, tFile As File
Dim fName As String
' On Error GoTo hErr
Set fso = New FileSystemObject
Set tFolder = fso.GetFolder(ThisWorkbook.Path) ' 文件夹路径
Application.ScreenUpdating = False
For Each tFile In tFolder.Files
fName = tFile.Name
If Right(fName, 5) = ".xlsx" Then '判断条件
If InStr(fName, "test") > 0 Then '判断条件
Call CopySheets(tFile.Path, fName) '拷贝工作表
End If
End If
Next
Application.ScreenUpdating = True
Set tFile = Nothing
Set tFolder = Nothing
Set fso = Nothing
Exit Sub
'hErr:
' Set tFile = Nothing
' Set tFolder = Nothing
' Set fso = Nothing
' MsgBox "error in union()"
End Sub
Sub CopySheets(ByVal fPath As String, ByVal fName As String)
Dim tWB As Workbook, tWS As Worksheet
' On Error GoTo hErr
Application.ScreenUpdating = False
Set tWB = Workbooks.Open(fPath, True, True)
'循环拷贝工作表,并重命名
For Each tWS In tWB.Worksheets
tWS.Copy After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count).Name = Mid(fName, 1, InStr(fName, ".")) & "_" & tWS.Name
Next
tWB.Close
Application.ScreenUpdating = True
Set tWS = Nothing
Set tWB = Nothing
Exit Sub
'hErr:
' Set tWS = Nothing
' Set tWB = Nothing
' MsgBox "error in copysheets()"
End Sub