Open all files in a folder and run macro

TaskMaster

Board Regular
Joined
Oct 15, 2020
Messages
75
Office Version
  1. 365
  2. 2016
Platform
  1. 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
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Hi there,

Untested but try this:

VBA Code:
wb.Application.Run("Copy")

Regards,

Robert
 
Upvote 0
Solution

Forum statistics

Threads
1,224,747
Messages
6,180,719
Members
452,995
Latest member
isldboy

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top