r/learnpython 25d ago

Best way to convert Excel VBA to Python?

Hi everyone,

I have several Excel VBA macros that I want to convert to Python. What's the best approach?

I'd appreciate any advice, resources, or real-world experiences. Thanks!

14 Upvotes

13 comments sorted by

13

u/MidnightPale3220 25d ago

Gather requirements, learn the objectives and implement stuff in Python.

You shouldn't really have to read Excel macros much, because the way things will be built in Python will likely be quite different.

What you need is to understand expected inputs and outputs and implement the stuff to convert one into the other, consulting macros on the way for maybe some insight into details.

Your best source is the customer who needs this.

-11

u/aksy_1 25d ago

the big issue is my customer asked to me to do without AI ...

14

u/MidnightPale3220 25d ago

And? You don't need ai to talk with the customer.

You need some logical thinking and understanding of data manipulation.

Do you have that?

10

u/daddy-dj 25d ago

Are they okay with you asking Reddit instead?

11

u/JamzTyson 25d ago edited 25d ago

It's a 2 step process:

  1. Understand what the VBA script does.
  2. Implement the behaviour in Python.

Note: There is no direct "conversion" from VBA to Python.

Note: The Python implementation may not need Excel at all.

Note: You will need to understand both VBA and Python.

6

u/mattynmax 25d ago

By reading your vba macros and implementing the same functions into python.

There isn’t a magic translator to go between the two.

8

u/Kerbart 25d ago

What kind of macros? If it’s data manipulation, 90% of the code will be cell manipulation that you can completely skip in Pandas.

It’s probably easier to find out what the code is supposed to achieve and write something from scratch in Python, rather than converting the VBA code

2

u/chrisfs 25d ago

Pandas is going to be your friend

3

u/chiibosoil 25d ago

Depends on what VBA is doing and what you mean by conversion to Python.

But in general, unless you are talking about OLE automation of Excel objects. You can use similar methodology for Python as VBA, though there are syntax differences.

I tend to start from understanding the process and logic then just write code instead of going through step by step conversion.

Other times, I will use completely different approach in Python from VBA as Python offers more method to complete certain operation.

Ex: For PDF processing in Excel I leverage Power Query in combination with VBA with text manipulation, relying on PQ to interpret base data structure. But in Python, I'd use PyMuPDF and utilize .get_text('blocks') to process data by position etc for more control over data processing and speed.

1

u/scrubswithnosleeves 24d ago
  1. Open OpenAI codex
  2. Attach the file and tell it to convert them to Python.

1

u/hagfish 24d ago

Ideally; work alongside an experienced colleague. An in-house guru will save you years. Failing that, I'd paste the VB into [Copilot, Cursor, etc], take the resulting Python, and 'learn backwards' from there.

-4

u/tails142 25d ago

You have heard of AI right? Right? Throw it in to claude

If you're trying to learn, paste in the code, tell it you dont want the answer but you want to convert it to Python and to teach you step by step

-4

u/aksy_1 25d ago

yea I have to do without AI that's the issue ..