Translating VBA code to Python code

ostrdevi

New Member
Joined
Apr 25, 2022
Messages
9
Office Version
  1. 2019
Platform
  1. Windows
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?
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
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.
 
Upvote 0
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.
 
Upvote 0
Solution
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!
 
Upvote 0

Forum statistics

Threads
1,223,958
Messages
6,175,635
Members
452,661
Latest member
Nonhle

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top