Can I use Python instead of VBA in Excel?

How to automate Excel macros using Python?

In Excel 2026, we can now run a macro and save it as an .

Xlsm file. The macro then runs without the need for any user input.

I have had the need of executing many macros as part of my job and at home. Many times, I find macros created by other people that I want to use, but don't have the permission to do so. In such scenarios, I would first try to understand the macros as best as possible. If this still does not work out, I try to automate the execution of the macros.

To automate macros, I tried a couple of python scripts, with some success. But recently, I came across a sample script written using PLEXT (Excel macro scripting interface) that was pretty awesome and provided the desired functionality.

PLEXT is a module developed by Chris Walsh (on github here) that can be installed from PyPI. On installation, one also gets the capability to run custom add-ins without having to create a Visual Studio add-in project. The PLEXT code is really nice to write compared to the standard vba macros one gets from excel.

A few of the features include -. Automatic running of macros by default. Automatic saving of the macros as part of the .xlsm file Recovery of macros if the .xlsm file gets corrupted or misplaced Runs in non-GUI mode. It doesn't matter whether the .xlsm file is opened or not - the macros run always.

To automate the usage of the PLEXT API, there are a few pre-requisites. These include: Python 3.7.2 on github).

The Excel plugin should be installed in Excel. For example, the version currently available in Anaconda python distribution has the installer for both xlwings and plexxtools.

PLEXT documentation - After installing plexxtools, we can use the help() function to get details on using the module. The plexxtools documentation includes detailed information on running the macros.

Running plexxtools and testing the macro. The plexxtools can be used either directly in Python by importing the module.

Does Excel support Python macros?

Yes, it does!

You can find it under Tools -> Visual Basic Editor -> Macros. Excel does not support macros written in other languages - but I did not know this until a few years ago when I created my own macro for Excel based on Python scripts. I also discovered a bug in Excel (the one I was using) and the author of the Excel package was very helpful in fixing this bug and he even offered a free service. After that I only needed to rewrite my macro to run within Excel instead of calling a Python script running as a background process.

I also think it would be a nice feature of Excel to have the option to write macros in .NET or Java instead of writing them in VBA! It is possible to do it, but it is quite hard work.

Excel supports any language that does the right thing with Excel, which means if the language can open Excel files and execute code without messing anything up. You can have a .pyc or a .exe file. But if you call Python from within Excel it will not work.

Can I use Python instead of VBA in Excel?

I would like to be able to use Python for a VBA code, instead of VBIs it possible?

Thanks in advance. The short answer is no, you can't just run a script from within an excel file and expect it to behave like an Excel macro. There are a few ways to do this, but the more complex approach would be to write a custom add-in using VSTO (Visual Studio Tools for Office). I've never done it myself, but I believe you can get started with the following page:
Alternatively, if you want a simpler approach, you can write a script that does what you want inside the VBA IDE itself. For example: Sub Sample(). Dim Wb As Workbook. ' Create a new workbook. Set Wb = Workbooks.Add() ' Add a few sheets. Wb.Sheets(1).Cells(1, 1).Value = "foo"
' Run the script.RunMe() ' Close the workbook.Close() End Sub. If you're looking for a more robust way to store and track changes, you might want to look into MS Access databases.

Related Answers

Does Excel 2016 have Macros?

After I used the Microsoft Office Assistant tool to clean up my Excel spreads...

How do I disable macros in group policy?

This article provides a step-by-step guide to turn off macros in Of...

Is Excel macro a skill?

In the past couple of years I've noticed people refer to two...