I’m trying to create and run a VBScript to run a macro I’ve created (PVT_Paste_Macro.xlsm) on Excel files without opening each individual Excel file (I have 528 files!) Here is my code:
Set objExcel = CreateObject("Excel.Application") objExcel.Application.Run "'C:\Users\Owner\Desktop\PVT_Paste_Macro.xlsm'!Module1.PVT_Paste_Macro" objExcel.DisplayAlerts = False objExcel.Application.Quit Set objExcel = Nothing
After running the code, I get the following error:
Script: C:\Users\Owner\Desktop\VBA for macros - PVT.vbs Line: 2 Char: 1 Error: Unknown runtime error Code: 800A03EC Source: Microsoft VBScript runtime error
I run the script while the macro file is open. The macro file, the (test) Excel file I’d like to run the macro on, and the VBS script are all located on the Desktop.
Can anyone help? Thanks.
The code does not work, because it does not like the fact, that the excel file, containing the code open before the code is executed:
Using your code, try this simple steps and make sure it works:
- Created a
Script.xlsmfile on the Desktop.
- Added the following in
Sub FromExcel() MsgBox "FROM EXCEL" End Sub
- Called it from a
Set objExcel = CreateObject("Excel.Application") objExcel.Application.Run "'C:\Users\UserName\Desktop\Script.xlsm'!Modul1.FromExcel" objExcel.Visible = True 'Or "False", depending on what you want. objExcel.Application.Quit Set objExcel = Nothing
As a way around, if you want to run the “macro” from an opened Excel file, then something like GetObject would be useful:
Set objExcel = GetObject("C:\Users\UserName\Desktop\xl.xlsm") objExcel.Application.Run "Modul1.FromExcel" objExcel.Application.Quit Set objExcel = Nothing
However, this is probably an XY problem – there should be a better way of doing the whole story, e.g. running the “macro” from a single independent Excel file, which opens the other Excel files in the desktop and processes them, without the need of VBScript. Probably check these: