Question : How do we intecept events in excel VIA VBA

We would like to have a way of editing the contents of an excel work sheet cell after the user exits the cell.

Is there some way to get a "lost focus" event (or something similar) when the user exits a cell on a sheet?

250 pts for an answer

Answer : How do we intecept events in excel VIA VBA

Hi cschene,
The Worksheet_SelectionChange event sub is triggered when the user changes the cell selection. If you have previously stored the old selection in a static variable, then you can have the effect of a sub that runs on a "lost focus" event.

This sub must go in the code pane for the worksheet being watched. It will display a messagebox with the address of the previous selection.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Static oldTarget As Range
If oldTarget Is Nothing Then
    Set oldTarget = Target
Else
    MsgBox oldTarget.Address    'Your "lost focus" sub could be called here
    Set oldTarget = Target
End If
End Sub

To install a sub in the code pane for a worksheet:
1) Right-click the sheet tab for the worksheet
2) Choose View Code from the resulting pop-up
3) Paste the suggested code in the resulting module sheet
4) ALT + F11 to return to the worksheet

If the above procedure doesn't work, then you need to change your macro security setting. To do so, open the Tools...Macro...Security menu item. Choose Medium, then click OK.

Cheers!

Brad
Random Solutions  
 
programming4us programming4us