How to call Excel VBA functions and subs using Python win32com?

calendar_today Asked Feb 6, 2013
thumb_up 6 upvotes
history Updated April 16, 2026

Question posted 2013 · +4 upvotes

My Excel workbook contains VBA subs and macros similar to those below; they sit in Module1.

How to call them using Python win32com module?

Public Sub setA1(ByVal s As String)
    ThisWorkbook.ActiveSheet.Range("A1").Value = s
End Sub

Public Function getA1() As String
    getA1 = ThisWorkbook.ActiveSheet.Range("A1").Value
End Function

Many thanks in advance!

Accepted answer +6 upvotes

import win32com.client
xl=win32com.client.Dispatch("Excel.Application")
xl.Workbooks.Open(Filename="c:\temp\book1.xls",ReadOnly=1)
xl.Application.Run("setA1", '4')
res = xl.Application.Run("getA1")
print res
xl = 0

Just as simple as this ….

VBA Core objects referenced (4)

  • Application — Using events with the Application object
  • Application — Working with Other Applications
  • Range — Refer to Cells by Using a Range Object
  • Range — Delete Duplicate Entries in a Range

Top vba Q&A (6)

+6 upvotes ranks this answer #66 out of 81 vba solutions on this site .
vba