TaskMaster
Board Regular
- Joined
- Oct 15, 2020
- Messages
- 75
- Office Version
- 365
- 2016
- Platform
- Windows
Hi all, im trying to save myself time by running a macro from another workbook to open all files in a specified file and then run the macro in newly opened file. The issue im finding is I dont know how to reference the macro in the newly opened workbook so getting the error "Sub or Function not defined". Does anyone have any ideas?
VBA Code:
Sub AllFiles()
Dim folderPath As String
Dim filename As String
Dim wb As Workbook
'Fill in the path\folder where the files are
folderPath = GetFolder
'cancel pressed
If Len(folderPath) = 0 Then Exit Sub
'Add a slash at the end if the user forget it
If Right(folderPath, 1) <> "\" Then
folderPath = folderPath & "\"
End If
'If there are no Excel files in the folder exit the sub
FilesInPath = Dir(folderPath & "*.xlsm*")
If FilesInPath = "" Then
MsgBox "No files found"
Exit Sub
End If
filename = Dir(folderPath & "*.xlsm")
Do While filename <> ""
Application.ScreenUpdating = False
Set wb = Workbooks.Open(folderPath & filename)
'Call a subroutine here to operate on the just-opened workbook
Call Copy
' Save and Close the file
wb.Save
wb.Close
filename = Dir
Loop
Application.ScreenUpdating = True
End Sub