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 objectApplication— Working with Other ApplicationsRange— Refer to Cells by Using a Range ObjectRange— Delete Duplicate Entries in a Range
Top vba Q&A (6)
- Difference between Visual Basic 6.0 and VBA +122 (2009)
- VBA – how to conditionally skip a for loop iteration +116 (2011)
- VBA: Test if string begins with a string? +53 (2013)
- html parsing of cricinfo scorecards +47 (2012)
- Code to loop through all records in MS Access +46 (2011)
- Access VBA | How to replace parts of a string with another string +44 (2011)
vba solutions on this site
.