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

Questions and answers on how to use XLSReadWriteII 3/4/5.
Post Reply
bendaniel
Posts: 3
Joined: Thu Jan 03, 2008 1:44 am

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

Post 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.
Post Reply