Excel VBA JavaScript: Run JS Macros in Excel
In this article
You cannot run JavaScript directly inside Excel VBA, but you can bridge the two by using the Microsoft Script Control (Windows only) or by calling Node.js from VBA. The simplest working method is to add the Microsoft Script Control reference, create a ScriptControl object, and evaluate JavaScript strings. This guide shows the exact steps, what to watch out for, and how to troubleshoot when the bridge fails.
What You'll Need
- Excel for Windows (the Script Control is not available on Mac).
- Trust access to the VBA project object model enabled (File > Options > Trust Center > Trust Center Settings > Macro Settings).
- Microsoft Script Control installed (comes with some Office versions; if missing, download the official installer from Microsoft).
- Basic familiarity with the VBA editor (Alt+F11).
- For Node.js method: Node.js installed and added to PATH.
Step-by-Step
- Open the VBA editor. Press Alt+F11 in Excel. In the Project Explorer, right-click your workbook and choose Insert > Module.
- Add the Script Control reference. In the VBA editor, go to Tools > References. Scroll down and check "Microsoft Script Control 1.0". If you don't see it, the control is not installed; download and install it from Microsoft's official site, then restart Excel.
- Write a simple test macro. In the new module, paste this code:
Run it with F5. A message box showing 3 confirms the bridge works.Sub RunJS() Dim sc As Object Set sc = CreateObject("ScriptControl") sc.Language = "JScript" MsgBox sc.Eval("1 + 2") End Sub - Pass Excel data into JavaScript. Use the AddCode method to define functions, then call them with parameters. Example:
This returns 42.Sub RunJSWithData() Dim sc As Object Set sc = CreateObject("ScriptControl") sc.Language = "JScript" sc.AddCode "function double(x) { return x * 2; }" MsgBox sc.Run("double", 21) End Sub - Return results to cells. Capture the return value in a VBA variable and write it to a cell:
Ensure A1 contains a number.Dim result As Variant result = sc.Run("double", Range("A1").Value) Range("B1").Value = result - Handle errors. Wrap calls in On Error Resume Next and check the ScriptControl's Error property. For example:
On Error Resume Next result = sc.Run("double", "abc") If Err.Number <> 0 Then MsgBox "JS error: " & sc.Error.Description On Error GoTo 0 - Alternative: call Node.js from VBA. If you need modern JavaScript (ES6+) or npm packages, use the Windows Shell:
This opens a console window. To capture output, redirect to a file and read it back with VBA's FileSystemObject.Sub RunNodeJS() Dim wsh As Object Set wsh = CreateObject("WScript.Shell") wsh.Run "node -e ""console.log(1+2)""", 1, True End Sub - Save and test. Save your workbook as .xlsm (macro-enabled). Test each macro with sample data before relying on it.
Common Mistakes
- Forgetting to set sc.Language = "JScript" (the default may be VBScript).
- Using ScriptControl on Mac — it does not exist there.
- Passing cell references instead of values (use .Value).
- Not handling JavaScript exceptions, which surface as VBA errors.
- Assuming Node.js output is captured automatically; you must redirect it.
- Leaving the ScriptControl object in memory without setting it to Nothing.
Tips
- Keep JavaScript functions small and pure for easier debugging.
- Use JSON to exchange complex data: JSON.stringify in JS, parse in VBA with a JSON library.
- For repeated calls, create the ScriptControl once at module level and reuse it.
- Test JavaScript logic in a browser console first.
- Document which parts require Windows.
Troubleshooting
If you get "ActiveX component can't create object", the Script Control is not registered. Reinstall it using an elevated command prompt: regsvr32 msscript.ocx. If you see "Object required", check that the reference is ticked and the object is created with CreateObject. If JavaScript errors occur, inspect sc.Error.Description. For Node.js, ensure node is in PATH by running node -v in Command Prompt. If output file is empty, check that the Node script writes to stdout and the redirection path is correct. Antivirus may block ScriptControl; add an exception for Excel if needed.
Frequently Asked Questions
Can I run JavaScript in Excel on Mac?
Not with ScriptControl. You can use Office Scripts (TypeScript) in Excel for the web, but that is not VBA. For desktop Mac, there is no supported VBA-to-JavaScript bridge.
Is ScriptControl safe?
It runs code with the same privileges as Excel. Only run trusted scripts. Microsoft has deprecated it in some contexts, so test on your Excel version.
Can I use modern JavaScript features like arrow functions?
ScriptControl uses an old JScript engine (ES3). For ES6+, use the Node.js method.
How do I pass arrays between VBA and JavaScript?
Convert VBA arrays to JSON strings and parse in JavaScript, or use ScriptControl's AddObject to expose VBA objects.
Why does my Node.js call hang?
The wsh.Run method with wait=True waits for the process to exit. If Node waits for input, it hangs. Ensure your script terminates.
Can I call VBA from JavaScript?
Yes, via ScriptControl's AddObject: expose a VBA object, then call its methods from JavaScript.