5

I have an excel spreadsheet with a massive amount of VBA and macros that include a button.

How do I execute VBA code in Excel (specifically to clicking the button and trigger its onclick event) outside excel from python (for example)?

Note: I'm willing to take answers in different languages like C++, C#, or Java; but, by far I would prefer them in python since it'll more smoothly connect with the remainder of my python applications.

Note 2: I may need to manipulate the excel spreadsheet with python using one of the python excel libraries available

Version Numbers:

Microsoft Excel Office 365 Version 1708 Build 8431.2079
python 2.7

3 Answers 3

5

You can use the win32com library to interact with COM Objects through Python. You can to install the win32com library with pip. That's how I did it at least. Essentially, you are not clicking the button on your worksheet, but instead calling the subroutine embedded in your VBA code to run on your worksheet.

Also, I used Python 3.6 for this, but I believe Python 2.7 should be compatible. Here is a basic outline.

Install Win32Com

Open up and command prompt and type:

pip install pypiwin32

Code

import win32com.client as win32

excel = win32.Dispatch("Excel.Application") # create an instance of Excel
#excel.Visible = 1 #Uncomment this to show Excel working through the code
book = excel.Workbooks.Open(Filename=r'C:\YourBookHere.xlsm')
excel.Run("YourBookHere.xlsm!Sheet1.MacroName") # This runs the macro that is on Sheet1
book.Save()
book.Close()
excel.Quit()

Hope this helps, this is my first post. Edit: Cleaned up things a bit

Sign up to request clarification or add additional context in comments.

1 Comment

Hey, you use "Sheet1" to locate the macro. Is there a way to specify the actual sheet name? I tried Worksheets("name"). No dice.
2

To expand upon dylan's excellent answer: you can also accomplish the same using the python xlwings package. In essence xlwings is a fancy wrapper around pywin32 that allows control of an excel application with even simpler syntax. The following python code should do the same as the code from dylan's answer:

import xlwings as xw

# Connect to existing workbook containing VBA macro
wb = xw.Book(r'C:\YourBookHere.xlsm')

# Run the VBA macro named 'MacroName'
wb.macro('MacroName')()

Comments

0

In addition, you can also use Windows Task Scheduler to fire the event. If a button-click-event fires the code, you can do exactly the same thing, by following the instructions in the link below.

https://www.sevenforums.com/tutorials/11949-elevated-program-shortcut-without-uac-prompt-create.html

Also, you will need a Workbook-open-event, so, something like this...

Private Sub Workbook_Open()
Msgbox "Welcome to ANALYSIS TABS"
End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.