Way to run Excel macros from command line or batch file

0 votes
When accessing the file from a batch file, but not every time I open it, I need to run an Excel VBA macro (hence not using the open file event). Is it possible to execute the macro from a batch file or command line? I've never heard of such a command.

Any help would be appreciated.
Nov 14, 2022 in Others by Kithuzzz
• 38,000 points
6,877 views

1 answer to this question.

0 votes

Solution

  • Open the workbook and run the macro from a VBScript file.
  • Copy the code below into Notepad.
  • Update the 'MyWorkbook.xls' and 'MyMacro' parameters.
  • Save it with a VBS extension and run it.
Option Explicit

On Error Resume Next

ExcelMacroExample

Sub ExcelMacroExample() 

  Dim xlApp 
  Dim xlBook 

  Set xlApp = CreateObject("Excel.Application") 
  Set xlBook = xlApp.Workbooks.Open("C:\MyWorkbook.xls", 0, True) 
  xlApp.Run "MyMacro"
  xlApp.Quit 

  Set xlBook = Nothing 
  Set xlApp = Nothing 

End Sub 

The key line that runs the macro is:

xlApp.Run "MyMacro"

answered Nov 14, 2022 by narikkadan
• 63,600 points

Related Questions In Others

0 votes
1 answer

'npm' is not recognized as internal or external command, operable program or batch file

To your existing lines of code, include ...READ MORE

answered Feb 10, 2022 in Others by Rahul
• 9,680 points
11,692 views
0 votes
1 answer

'adb' is not recognized as an internal or external command, operable program or batch file

Set the path of adb into System ...READ MORE

answered Feb 16, 2022 in Others by Aditya
• 7,680 points
7,133 views
0 votes
1 answer

Retrieve epay.info Balance with VBA and Excel

This code should log you in, provided ...READ MORE

answered Sep 5, 2018 in Blockchain by digger
• 26,740 points
1,215 views
0 votes
1 answer

How to load file to Excel Power query from SFTP site

Currently, I don't think there is a ...READ MORE

answered Dec 3, 2018 in Power BI by Upasana
• 8,620 points
3,668 views
0 votes
1 answer

Using VBA Excel to create a gramatically correct list

The Excel AND function is a logical ...READ MORE

answered Feb 9, 2022 in Others by gaurav
• 23,260 points
913 views
0 votes
2 answers

How to copy a formula horizontally within a table using Excel VBA?

Hi so basically, create an adjacent column ...READ MORE

answered Feb 16, 2022 in Others by Edureka
• 13,690 points
1,057 views
0 votes
1 answer

Convert csv- or Excel-file (xlsx) to kml with custom markers

With some help from the community, I ...READ MORE

answered Sep 26, 2022 in Others by narikkadan
• 63,600 points
1,228 views
0 votes
1 answer

How can I convert excel to PDF by Libreoffice and keep all format from excel file?

"Times New Roman" typeface does not have ...READ MORE

answered Oct 3, 2022 in Others by narikkadan
• 63,600 points
1,650 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP