Link to home
Start Free TrialLog in
Avatar of Master Work
Master Work

asked on

I need to run maco in Excel using command line or VBS.

I need to run maco in Excel using command line or VBS. Please find attached the excel file with the maco and the vbs I created but it was not working.

Regards,

Dallag

'Create an instance of Excel
Set xlsApp = CreateObject("Excel.Application")
'Remove the following line to open Excel in the background
xlsApp.Visible = true
'Open your file
Set xlsWB = xlsApp.Workbooks.Open("D:\0 Aramco\0 Prpjects\0 UR\NA\System-A-B1-new-format_test_v3.xlsm")
'You can call macros connected to sheets or the workbook
Set xlsSheet = xlsWB.Worksheets(1)

'Call xlsWB.MakeFinal() 'To call a macro in ThisWorkbook
'Call xlsSheet.macroname() 'To call a macro in Sheet1

'False here just makes it not prompt to save
xlsWB.Close(False)
xlsApp.Quit

Open in new window

new-format_test_v3.xlsm
Avatar of Brian B
Brian B
Flag of Canada image

Make the macro run on open: https://support.office.com/en-us/article/automatically-run-a-macro-when-opening-a-workbook-1e55959b-e077-4c88-a696-c3017600db44

Then launch excel from the command line or script and have it open that book:
excel /t <filename>
Avatar of Master Work
Master Work

ASKER

I have many macros and I need to select.
The basic concept is
  Dim xlApp 
  Dim xlBook 

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

  Set xlBook = Nothing
  Set xlBook = Nothing 

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you so much