I am using the following code to open a group of workbooks and run a macro inside and then save and close the workbook:
Sub UpdateAll()
Dim folderPath As String
Dim filename As String
Dim wb As Workbook
folderPath = "K:\Location\"
If Right(folderPath, 1) <> "\" Then folderPath = folderPath + "\"
filename = Dir(folderPath & "*.xlsm")
Do While filename <> ""
Application.ScreenUpdating = False
Set wb = Workbooks.Open(folderPath & filename)
Application.Run "'" & filename & "'!UpdateData"
wb.Close SaveChanges:=True
filename = Dir
Loop
Application.ScreenUpdating = True
End Sub
I added the below to the end of each individual macro within each workbook and now UpdateAll() won't go through each workbook in the folder and run the workbooks, it stops after opening one:
Dim Output As Workbook
Dim Current As String
Dim FileName As String
Set Output = ThisWorkbook
Current = ThisWorkbook.FullName
Application.DisplayAlerts = False
Dim SH As Worksheet
For Each SH In Output.Worksheets
SH.UsedRange.Copy
SH.UsedRange.PasteSpecial xlPasteValues, _
Operation:=xlNone, SkipBlanks:=True, Transpose:=False
Next
FileName = ThisWorkbook.Path & "S:\Location\" & "165.xlsx"
Output.SaveAs FileName:="S:\Location\165.xlsx", _
FileFormat:=xlOpenXMLWorkbook
Workbooks.Open Current
Output.Close
Application.DisplayAlerts = True
End Sub
Bookmarks