Running macro every files in a folder

jaik22

Board Regular
Joined
Sep 23, 2016
Messages
102
Code:
Sub MacroRun()
Dim file
Dim path As String




path = "C:\Users\Desktop\TEST\"




file = Dir(path & "*.xlsm")
Do While file <> ""
Documents.Open Filename:=path & file




 Call Format




ActiveDocument.Save
ActiveDocument.Close


file = Dir()
Loop
End Sub


This is my code for running a macro for entire excel files in a folder, and Format is the macro that I want to call. It seems correct, but macro does not work at all. Is there any mistake that I made?

Thank you!
 
Last edited:

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
In dealing with Excel files, you should use "ActiveWorkbook", not "ActiveDocument" (that sounds more like a Microsoft Word thing).
 
Upvote 0
Thanks for the reply Joe4,
Good catch, that was a typo. However, that wasn't the issue. I can't call the module to run the macro... Do you have any idea?
 
Upvote 0
Is the macro a public procedure in the same workbook?

Also, you should never used reserved words like "Format" for the name of your procedures, functions, and variables. Using reserved words can cause confusion and unexpected results, as Excel may not be able to determine if you are referring to your procedures, functions, and variables or their functions, properties, and objects. Try renaming your procedure to something like "MyFormat".
 
Upvote 0
Thanks for reply,
I changed format to Myformat but it still gives me an error. "Expected variable or procedure, not module" are you familiar with this error?
 
Upvote 0
Please answer my first question in the previous post.
 
Upvote 0
Yes, macro is in same the workbook with public procedure like this

Code:
 Public Sub MyFormat()
 
Upvote 0
Have you put it in a Standard Module, or one of the Sheet or Workbook modules?

I would recommend using F8 to step through your code line-by-line to see what is happening each step of the way. If you resize your VB Editor screen, you can watch both the sheet and VBA code at the same time to see what is happening.
 
Upvote 0

Forum statistics

Threads
1,214,534
Messages
6,120,084
Members
448,943
Latest member
sharmarick

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