Open workbooks in folder and run Macro in each book

glynn1969

Board Regular
Joined
Nov 24, 2018
Messages
88
Office Version
  1. 365
Platform
  1. Windows
Hello grateful for any help here.

I have a folder ("Workbooks") which contains 5 xlsm files (this number will increase to about 62).
i have a second folder called "Master" in which i have a workbook called "Master Macro"

I have tried to create a macro in the "Master Macro" workbook which will open each file in the "Workbooks" folder then run a macro which is contained in each workbook called "refreshWS" (although refresWS is in all workbooks it is bespoke to each workbook itself so i can't have this outside of each workbook) and then close the workbook and save it - then move onto the next workbook.

However it is falling over because i am trying to run the macro in the 5 workbooks and it is saying that the macros are not enabled in the workbook- how would i get past this.

My only other thought was to open all the workbooks in one phase one of a macro and so enabling macros in each book and then once all opened run a second macro to select each open workbook and run the required "refreshWS" macro and close and save - then move onto the next open workbook (but i would somehow need to omit my "Master Macro" workbook.

Any help from you wizards with either solution would be much appreciated.

Copy of my macro below:-

Sub OpenAndRunMacroInAllWorkbooksIncludingNew()
Dim folderPath As String
Dim fileName As String
Dim wb As Workbook

' Specify the folder path where your workbooks are located
folderPath = "C:\Users\1\Workbooks\"

' Get the first file name in the folder
fileName = Dir(folderPath & "*.xls*")

' Loop through all files in the folder
Do While fileName <> ""
' Open the workbook
Set wb = Workbooks.Open(folderPath & fileName)


' Run your macro (replace "YourMacroName" with the actual macro name)
Application.Run wb.Name & "!refreshWS"

' Close the workbook (you can choose to save changes or not)
'wb.Close SaveChanges:=True

' Get the next file name
fileName = Dir
Loop

End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
However it is falling over because i am trying to run the macro in the 5 workbooks and it is saying that the macros are not enabled in the workbook- how would i get past this.

Try making each of the 5 workbooks a 'Trusted Document'

 
Upvote 0
Try making each of the 5 workbooks a 'Trusted Document'

Thank you for your reply - unfotunately this is a work computer and thay have locked down this access
 
Upvote 0
Thank you for your reply - unfotunately this is a work computer and thay have locked down this access
If so my friend, you have problems. But most corporations don't lock that down for internal users so I would double check if I were you. People at work still need to designate trusted documents or folders.
 
Upvote 0
If so my friend, you have problems. But most corporations don't lock that down for internal users so I would double check if I were you. People at work still need to designate trusted documents or folders
 
Upvote 0
Ok....il have another look on monday.

Is it not posibble a second macro to pick up the open books and then apply their own internal macro?
 
Upvote 0
Ok....il have another look on monday.

Is it not posibble a second macro to pick up the open books and then apply their own internal macro?

Not if macro execution is disabled. You would be better off pulling all macros in to your master workbook.
 
Upvote 0
Apologies, i may have misled you, i can open the 5 workbooks and select to enable the macros from the top bar, and then can run macros. My issue is that my vba is trying to run my macro before i have been able to allow macros manually. Whuch is why i thought that with the books opened manually then there coukd be a macro to select each open workbook, run my refreshWS macro and close.
 
Upvote 0

Forum statistics

Threads
1,223,711
Messages
6,174,020
Members
452,542
Latest member
Bricklin

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