r/vba • • 6d ago

Discussion Do you use Python instead of VBA?

I have not understood exactly the difference between xlite and pyXLL but I am wondering if someone uses Python to code and why?

21 Upvotes

23 comments sorted by

10

u/HeavyMaterial163 5d ago

I prefer Python, but there are projects where VBA makes more sense. Data handling and transformation is easier with a longer list of better implemented data structures, I have more control of nearly everything, better OOP implementation, and a lot more freedom in GUI design. Not to mention less verbose.

13

u/Joelle_bb 6d ago edited 5d ago

I do now for 2 reasons:

I have to because of my job (my team opposes use of vba since its not common in our tech stack, and I'd be the only person who can service the mid tier and complex builds I have made)

I can do more with it than I ever could in vba, including everything I did in vba

I've ported most of my substantial macros into either python apps or .bats that can be executed if the end user has python set up

0

u/Normal_Glass_5454 5d ago

How r u running python on excel? Do u use the python IDE or in excel with =python

2

u/Joelle_bb 5d ago

Predominantly running python; and if excel is needed, feeding into one, otherwise goes into a different BI tool

On occasion I'll =py(), but not usually needing complexity at that stage of my workflows. It tends to live more in the semantics model/power query. So =py() tends to mainly be fore visualizations

I've gone from working in spreadsheets in my former role to working in SQL/Oracle/Snowflake for data, so excel doesn't have the same value as it once did. If I never pivoted to where I'm at now, I'd likely use: VS code outside of excel, in power query, and =py() quite frequently tbh

1

u/Intelligent_List2504 3d ago

I just miss the good old days of Olap. I managed Olap for dozens of companies in the Pacific Northwest.

Dating back to the year-2000 when M$ introduced Office Web Components, it allowed better pivottables in a web interface than what Excel can do.

1

u/Normal_Glass_5454 5d ago

Sorry for asking you to elaborate but your first line "predominantly running python" what does it mean? I'm not tech savvy enough to fill in the gaps of that phrase. Like where r u running python for ur excel when u say running python?

4

u/Joelle_bb 5d ago edited 5d ago

No worries! I mostly write and run the python code first in Visual Studio Code (my IDE of choice), and then excel comes after (python feeds data into an excel)

1

u/Normal_Glass_5454 5d ago

The part I'm confused on is how your python can interact with excel when you are running it from an IDE. Does your python in the IDE automatically allow for interactions with excel files? Or what is the medium that connects your IDE to Excel (plug in, another software, etc).

1

u/sancarn 9 11h ago

You can connect to Excel directly via the COM API - the same way VBA does. Alternatively you can connect via the C API (via compiled XLLs). And finally, if you don't need to interact with the live version of Excel, but only need to output reports you can use openpyxl.

Alternatively you can use a free package I recently released

4

u/RemoteEmployee094 5d ago

Excel is a program for working in excel files, xlsx etc. using Python on Excel per your statement, I believe you are questioning how accessing and editing excel files work from the Python environment. Probably some line of python grabs the xml that describes the tables in the Excel file .. go open an Excel file in winzip or 7zip. See what the underlying is.

1

u/Normal_Glass_5454 5d ago

yes this is the part I'm confused on

1

u/jd31068 62 5d ago

Have you check out this https://support.microsoft.com/en-us/excel/python/get-started-with-python-in-excel

There are a number of YouTube videos on the subject as well.

4

u/welktickler 5d ago

I have been using VBA as one of my tools at work for over ten years. I recently started learning python as it is better suited to the data stuff I do.I love and hate VBA. Its very powerful and you can do some really cool stuff. But things like having to late bind so you can use a dictionary is very poor in modern times. I am liking python but i feel like I have less control over things than VBA. That may be a good thing for work so i can just do the data bits.
Another reason I want to learn python is because of stuff like this; I recently went for a job interview and the manager rubbished VBA as just "macros and we hate macros here" He just wouldnt listen when I explained that it was OPP (not fully I know but,,) and really powerful or that it was proper coding.

3

u/justanothersnek 5d ago

Have you checked out xlwings?

3

u/JennaSys 5d ago

I started using VBA in the 90s but use Python now unless I'm needing to work with Access or MS Office automation specifically. Python just supports a richer and more flexible ecosystem.

1

u/keith-kld 4d ago

I used VBA since 1990s. Now, if I want to run the Code outside Ms Word or Ms Excel, I use Powershell 7.7. I don’t know if Python can be better than them or not.

1

u/AFRIENDlyyGuy 3d ago

I was a "main" VBA lol. Did an incredible system with it. Generated Invoices, to stock control to integrations with bank. All with pure logic and userforms.
I personally love it and always will. It was my entry door to progamming as well.

But when it comes the time to escalate, python is so much better. With AI i did basicallly
everthing in a few months and MUCH more. And that was the worst part, realizing my old best friend VBA was slipping trought my finger was like a grief to me ahhhahaha

1

u/jackofspades123 5d ago edited 5d ago

I use vba and python (usually not together).

Vba if developing stuff for the office where no one knows python and python at home for my own. Each are good in their own way.

What is it you're wanting to do?

Edit: Pyxll does RTD if that matters for your use case.

1

u/Frequent-Complaint-6 5d ago

Yes python all the way.

1

u/pdf_to_excel_nerd 5d ago

I use Python for most of it now. The big one for me is that you're not limited to the classes and methods Excel gives you, so anything outside the workbook (PDFs, CSVs, APIs, other files) is just another library away.

Also, having one common script dynamically pick up whatever files land in a folder and process them is a real pain in VBA. In Python it's a few lines with pathlib plus openpyxl or pandas.

0

u/TapNo3926 4d ago

AI writes all my code now. It prefers python unless I explicitly ask it for VBA. Python is more robust.

-3

u/Thadrea 3 5d ago

I don't use VBA any more. Anything that VBA can do can be done better, more maintainably, in Python.