Page 1 of 1

How can you include VBA code to handle worksheet events??

Posted: Tue Mar 04, 2008 12:01 pm
by bendaniel
I wish to output a spreadsheet (from scratch) that includes VBA code to do something when the user changes a cell in the spreadsheet. I've included a very basic example console app below but I cannot get it working. Please help.

program VBATest;

{$APPTYPE CONSOLE}

uses
SysUtils,
XLSReadWriteII2,
XLSVBA2;

var xls: TXLSReadWriteII2;
begin
xls := TXLSReadWriteII2.Create(nil);
try
xls.Filename := 'C:\VBA.xls';
xls.VBA.EditVBA := True;
with xls.VBA.AddModule(xls.Sheet[0].Name, vmtDocument).Source do
begin
Add('Private Sub Worksheet_Change(ByVal Target As Range)');
Add(' MsgBox "Something Changed"');
Add('End Sub');
end;
xls.Write;
finally
FreeAndNil(xls);
end;
end.