Consulting

View Poll Results: What is your preferred WinForms IDE?

Voters
3. You may not vote on this poll
  • Visual Basic

    1 33.33%
  • Borland Delphi

    1 33.33%
  • MS/Borland C++

    0 0%
  • .NET Platform IDE's

    1 33.33%
Results 1 to 2 of 2

Thread: OnExit/OnChange methods for Excel Cells

  1. #1
    VBAX Regular
    Joined
    May 2009
    Location
    Johannesburg
    Posts
    69
    Location

    OnExit/OnChange methods for Excel Cells

    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!!!
    Deyken
    DeezineTek
    South Africa

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You can create Event Macros in the Worksheet or Workbook module
    [VBA]
    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$C$4" Then
    'Do things
    End If
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •