# Translating VBA code to Python code



## ostrdevi (May 24, 2022)

Does anyone know if it is possible to translate VBA code to Python code? I have a lot of VBA code that I was using for a while and now I unfortunately need it to be in Python. Could anyone help with this?


----------



## Dan_W (May 24, 2022)

Is it possible to translate VBA code to Python code? Of course.

As to whether or not it would be difficult / straight-forward, that will depend largely on what it is your VBA code does. For example, if the code involves a lot of operations on the worksheet (e.g., formulas, reports, etc) there are python libraries which allow you to automate Excel in much the same way as VBA does (*a list of excel-related python resources*). If the code involves a lot of system API calls, there is a library that allows python to also make Win32 API calls (PyWin32).

I'm in the middle of trying to write my own rudimentary code converter from another language VBA, so I have some simple appreciation for the difficulties in coming up with an automated converter. I think you need to work on the basis that, depending on the quality of the source code, it is likely that whatever code you give it, the output will need to be reviewed and parts of it rewritten. That said, there is software for VBA and Python that will 'improve' the code base (or make suggestions for improvements) - if you were to run that over the VBA first, then have it converted, and then have the Python 'output 'improved' it _*might*_ do a lot of the hard work for you. Alternatively, it may also be the case that it could come back completely indistinguishable from the original. If you have professional, well-structured and short sized functions, you will have a better chance of a decent translation.

To that end, I had previously come across vb2py (vb2py Online Conversion), and I thought it was pretty good when it came to non-Excel specific code (ie., not dealing with Excel Application object model, worksheet, etc). You might notice that it says that it translates from VB6 / VB.NET to Python, but that it has plans to include VBA in future. For your reference, VBA and VB6 are the same-ish language. There are some very important differences, but the syntax (at least) is for the most part, identical.

I wasn't sure if you asking if someone might be able to help you by translating the VBA code for you - if you were, you might want to avoid using the word 'a lot' - There may be some people who would generously agree to help out, but I can't imagine that there will be *a lot* of people who will volunteer to do *a lot* of work. I think maybe your best approach would be to explain broadly what it is that you're trying to accomplish with all this code (is it all part of a single project that does X?, etc) and give a estimate of how many userforms, standard/class modules, and how many lines of code overall. If you can provide a sample of the code you're looking to port to python, I'm sure that would be useful too.

Hope that's helpful. Let me know if you have any questions.


----------



## Dan_W (May 24, 2022)

I completely forgot to mention my key point - if what you ultimately want is to accomplish in Python what it is you can currently do with your VBA code, it might be that a translation of the code is not the best approach. A line by line literal translation from one language to the next might be ok for the most part, but Python has methods of doing things that have no equivalent in VBA and vice versa. So with that in mind, it might be better just to work out what the given VBA function does, 'ignore' the VBA code, and then work out how best to accomplish the same end goal in Python (i.e., "is it Pythonic?"). Just a thought.


----------



## jay_py (Jun 10, 2022)

I'm in a similar situation now that I might need to convert some Python code into R. I don't know if I should use a converter, but my initial plan is to understand the Python code and then re-write the logic in R (only if I must do this). Definitely a lot of work, but I don't know if there's any other way.

You might need to do the same, i.e. figure out what the VBA does, then replicate the same stuff in Python... good luck!


----------

