我有一个VBA脚本,可以将sheet添加到大约500个Excel文件中。我在运行VBA脚本并添加简单的sheet时没有任何问题,但是当我尝试添加包含VBA脚本、图表和按钮的sheet时,它会运行一段时间然后冻结。
以下是代码。我知道它没有错误处理-有什么建议如何解决这个问题或者可能导致Excel冻结的原因吗?
Sub FindOpenFiles()
Const ForReading = 1
Set oFSO = New FileSystemObject
Dim txtStream As TextStream
Dim FSO As Scripting.FileSystemObject, folder As Scripting.folder, file As Scripting.file, wb As Workbook, sh As Worksheet
Dim directory As String
'The path for the equipement list. - add the desired path for all equipement or desired value stream only.
Set txtStream = oFSO.OpenTextFile("O:\SiteServices\Maintenance\Maintenance Support Folder\Maintenance Department Information\HTML for Knowledgebase\Excel for Knowledgebase\Equipement paths-all.txt", ForReading)
Do Until txtStream.AtEndOfStream
strNextLine = txtStream.ReadLine
If strNextLine <> "" Then
Set FSO = CreateObject("Scripting.FileSystemObject")
Set folder = FSO.GetFolder(strNextLine)
For Each file In folder.Files
If Mid(file.Name, InStrRev(file.Name, ".") + 1) = "xls" Then
Workbooks.Open strNextLine & Application.PathSeparator & file.Name
Set wb = Workbooks("Equipment Further Documentation List.xls")
For Each sh In Workbooks("Master File.xls").Worksheets
sh.Copy After:=wb.Sheets(wb.Sheets.Count)
Next sh
ActiveWorkbook.Close SaveChanges:=True
ActiveWorkbook.CheckCompatibility = False
End If
Next file
End If
Loop
txtStream.Close
End Sub
Application.ScreenUpdating = false
,并在End Sub
之前添加另一行代码:Application.ScreenUpdating = true
。 - Kazimierz JaworGetExtensionName
方法而不是Mid(file.Name, InStrRev(file.Name, ".") + 1) = "xls"
。如果您愿意,您可以将其更改为FSO.GetExtensionName(file.Name) = "xls"
。在我看来,这样会更加简洁。 - UberNubIsTrue