I am using a script to open a bunch of Excel files, copying two or more sheets into a new file and saving this new file. It sure beats doing it manually, but I think it could be faster.
Here's my script:
Sub Flujo()
Const FilePath = "A path..."
Const Destination = "Another path..."
Dim app As New Excel.Application
app.Visible = False
app.ScreenUpdating = False
app.DisplayAlerts = False
Dim str As String
str = Dir(FilePath & "\*.xlsx")
Do Until str = ""
Dim WrkBook As Workbook
Set WrkBook = app.Workbooks.Open(Filename:=FilePath & "\" & str, UpdateLinks:=0)
str = Dir()
Dim NewBook As Workbook
Set NewBook = app.Workbooks.Add
WrkBook.Sheets("SheetName").Copy After:=NewBook.Sheets(3)
WrkBook.Sheets("AnotherSheetName").Copy After:=NewBook.Sheets(3)
NewBook.Sheets(1).Delete
NewBook.Sheets(1).Delete
NewBook.Sheets(1).Delete
NewBook.SaveAs Filename:=Destination & "flujo_" & WrkBook.Name
NewBook.Close False
WrkBook.Close False
Loop
app.DisplayAlerts = True
app.Quit
Set app = Nothing
End Sub
Is there a way I could make it work faster?
-Edit- The script takes more or less 3 - 6 seconds per file. That's not too much but I feel it could be much faster. Or is this the fastest I can expect to get with VBA?
Workbooks.Add(xlWBATWorksheet)
], so then you have fewer sheets to delete later. Otherwise there don't seem to be any clear ways to really speed this up unless you're opening/saving on a slow network and not a local drive. – Tim Williams May 27 at 17:57Application.Calculation = xlCalculationManual
, use theWorkSheets
collection rather thanSheets
. Also, if the workbooks are saved to a remote location, you might save them locally then, afterwards, copy them to the remote location. – Andy G May 27 at 18:00