Excel VBS/VBA Run macro on specified files

belen373

New Member
Joined
Mar 20, 2014
Messages
1
Hello, I have a VBScript (that means I don’t have excel open) that looks for a certain file (book1.xls in this case) in a folder I have specified, runs a macro from PERSONAL.XLSB and saves the file. Script works fine.

I want the script to run this macro on book1.xls, book2.xls, etc… Is there a way to do this without writing the same script for every file? I am using Excel 2010 and there are many files in my folder, I only need to run this macro on specific files.



Thank you

MY CODE:

Option Explicit

Dim xlApp, xlbook, xlApp2

Set xlApp = CreateObject ("Excel.Application")
Set xlBook = xlApp.Workbooks.Open ("C:\PATH TO MY FILES\book1", 0, False)
Set xlApp2 = xlApp.Workbooks.Open("C:\PATH TO Personal.xlsb FILE")

xlApp.DisplayAlerts = False
xlApp.Application.Visible = False

'Runs "XrefMacro1" Macro found in Personal.xlsb
xlApp.Run XlApp2.Name & "!XrefMacro1"

xlBook.Save

xlBook.Close
xlApp2.Close
xlApp.Quit

Set xlBook = Nothing
Set xlApp2 = Nothing
Set xlApp = Nothing

'WScript.Echo "Finished."
WScript.Quit

xlApp.DisplayAlerts = True
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.

Forum statistics

Threads
1,213,536
Messages
6,114,207
Members
448,554
Latest member
Gleisner2

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