PDA

View Full Version : OnExit/OnChange methods for Excel Cells



deyken
10-06-2009, 03:11 AM
Good Day,

I am generally a GUI (WinForms) application programmer, but have found that, instead of trying to generate my own Reports within the context of my applications, it is actually much easier to create a "template" form in Excel and simply write macros to import the relevant information from my SQL Server database into this Excel template. It works beautifully, but now I need to take it up a notch:

Is there a way that I can either create a custom Function to fire on Cell Changes and/or on Cell Exits? In normal VB or Delphi, I would simply create an OnChange Event for a particular control (like a text box), but it does not seem to be that simple with Excel worksheet cells...

Typically, this is what I have in mind (VBA):

...
Sub CellExit(MyCell As Range)
with MyCell
{Do something}
end with
End Sub

This is what it would look like in Delphi:

function <control>OnExit(ShiftState : TRect) : Boolean;
begin
{Do some work here...}
end;

Or in C++:

bool __function(<control>OnExit) {
//Do something here
}

The reason I would like to do this is: As a user is entering a value into say Cell(4, "C") and Tabs or clicks into another Cell(x, y), the value from the Exited Cell is to be used in a SQL Query. The latter Recordset would bring back the address of a CustomerID (for instance) that was entered into Cell(4, "C"). Cells adjacent to the latter will then each be assigned a Field([Index]).Value from the Recordset.

Is this possible?

Thanks buds!!!

mdmackillop
10-07-2009, 08:12 AM
You can create Event Macros in the Worksheet or Workbook module

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$4" Then
'Do things
End If
End Sub