1
Answer

How to execute from C# a VBA macro in an open Excel workbook?

jeffrey zalc

jeffrey zalc

17y
2.8k
1

I have a C# executable that opens an existing Excel workbook as follows:

string workbookPath = "c:/SomeWorkBook.xls";

Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath,

0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",

true, false, 0, true, false, false);

This works fine and I can read from and write data to cells on various worksheets in that workbook.

One of the worksheets, say SHEET9, has associated with it (i.e. if you open the VBA Editor, navigate to the worksheet, you see the routine) a VBA routine that does some calculations.  I want to call this routine from the C# code.

Suppose the VBA routine looks like:

Private Sub Run_BLAH_Click()

  Call PREP_OUTPUT

  Call SIMEXECUTE

End Sub

How do it call this Run_BLAH_Click routine on SHEET9 from C# after that workbook has been opened?  Is it a problem that the VBA routine is on the sheet rather than in a module?


Thanks in advance.

Answers (1)