Running a Macro from a Batch File

azizrasul

Well-known Member
Joined
Jul 7, 2003
Messages
1,304
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Is their a way of creating a batch file which will open an Excel workbook and run a single macro?
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
With Excel 2003, you could add the 'Auto_Open' macro. When Excel opens a spreadsheet that has macros, it looks for one called 'Auto_Open'. If it finds this macro, it automatically runs. This macro is stored with the spreadsheet, so each spreadsheet can have its own 'Auto_Open' macro. To avoid the 'enable macros' prompt when the spreadsheet is opened, you would need to set the macro security to 'low'.
 
Upvote 0
Aziz,

you can create a vbscript file which opens your Excel file.

If you put your macro code into the workbook_open it will rin in the same manner which pgmer mentioned.

I DO NOT recommend setting macro security to low. Doing this mean you could open a file with a macro which will harm your computer without you even knowing it was doing so.

Create your xls file with the workbook_open code doing wat you want it to then launch it by typing the following code into notepad then saving it as filename.VBS (this makes the file a vb script which windows will run if it's double clicked)

Code:
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open _
    ("C:\Test\book1.xls")
objExcel.Quit
change the path to point to your excel file.

Lauching your excel file in this way will not ask you to enable macros, but still lets you keep macro security set to medium
 
Upvote 0
random noise, when I double click the vb script file, the Excel file opens but it's not visible?

I'll look at the code in your link John_W.
 
Upvote 0
aziz,

yes the code I gave you is to just open, run a macro & then close it.

You can change the last line to
Code:
objExcel.Visible = True
if you want Excel to be visible (note workbook_open macro will run before excel appears).
 
Upvote 0
OK that did the job.

What additional code can I add to run a macro called 'Macro 1'?
 
Upvote 0
Hi Aziz,

Before the quit line you should be able to call a macro from within the code like this.

Call Macro 1

Or Simply

Macro 1

I assume this will work with RandomNoise's code. I don't know if its important where the macro is stored or not off the top of my head.
 
Upvote 0
It didn't like that.

Also I've realised you can't have a macro called 'Macro 1' as it will not allow any spaces.
 
Upvote 0
Hi,

I assume you've also tried calling a macro with a valid name in the same way and that hasn't worked also?

Generally you can call other macro's from within the current one.
 
Upvote 0

Forum statistics

Threads
1,215,883
Messages
6,127,553
Members
449,385
Latest member
KMGLarson

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