Event which fires when moving from one cell to another in Excel

Sathyaish
08-29-2003, 09:11 PM
How do I trap the lost focus event of a cell in Excel. In other words, which is the event that is fired when one moves from one cell to a different cell? Is it the Worksheet object's Change event?

italkid
08-30-2003, 03:24 AM
The "Selection_Change" event fires when you select or change a cell.
Since i understand that you want to detect the lost focus (leaving) of
a cell you gonna need some additional code.
This code uses also the "Selection_Change" event to store a cell address
as static.
When leaving that cell the code gives you the address of the lost focus cell.
Bad thing is when opening the workbook the "Static" is still empty so
it wont react on the first "Selection_Change".
After selecting a second cell the "Static" is loaded.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Static R As Range, S As String

If Not R Is Nothing Then
MsgBox "Leaving cell " & S
End If

Set R = ActiveCell
S = ActiveCell.Address

End Sub

tboltfrank
08-30-2003, 07:15 AM
That look's awesome, Italkid

Just in case a user wants to be able to recall the last cell that had focus, but needs to be able to do so, upon closing and re-opening a WorkBook, the following code that I just wrote, will do that. Provided that the WorkBook has been saved.

This is a bit of a crude method, I know, but I don't yet have great programming skills. Maybe Italkid, or someone else will come back and give you a modification of his code, to do this. If so, that would very-very likely be a much preferred method, over this one.

Private Sub CommandButton1_Click()
On Error GoTo ErrorHandler
If Range("IU65536").Value = "First" Then
Range(Range("IT65536").Value).Select
GoTo EndHandler
End If
If Range("IU65536").Value = "Second" Then
Range(Range("IV65536").Value).Select
GoTo EndHandler
End If
Exit Sub
ErrorHandler:
MsgBox ("E r r o r - Likely caused by, literally the very 1st use of the code")
'as it try's to get the last address, from Range("IV65536"), but it's empty
Exit Sub
EndHandler:
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Range("IU65536").Value = "First" Then
GoTo EndHandler
End If
Range("IV65536").Value = ActiveCell.Address
Range("IU65536").Value = "First"
Exit Sub
EndHandler:
Range("IT65536").Value = ActiveCell.Address
Range("IU65536").Value = "Second"
End Sub

tboltfrank
08-30-2003, 08:52 AM
The code below I believe is a better version of my 1st selection change code, that I last posted. - The command button code, needs no changes.

This code is not limited to recaling just the last ActiveCell, as it will recall the last entire range, that may have been selected. (ie: Columns, & Rows, or adjacent cell ranges, etc.. I even tried this on a special range selection. That was where I selected "Visible cells only" on a range that included several hidden rows. That worked great

I also think that I didn't really need to use such out of the way cells, as I did, to store cell address's, etc. I think that the last three cells of Row one, would be perfectly adequate, as there really is no issue of it being maintaining their values, as every selection, restablishes that.

Also, again let me point out, that a more talented progammer, could re-write my code and it's ranges, etc, with(what THEY call, cleaner, code)

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Range("IU65536").Value = "First" Then
GoTo EndHandler
End If
Range("IV65536").Value = Selection.Address
Range("IU65536").Value = "First"
Exit Sub
EndHandler:
Range("IT65536").Value = Selection.Address
Range("IU65536").Value = "Second"
End Sub

Both of these variations, allow you toggle back and forth, between the current selection and the previous selection(-:

italkid
08-30-2003, 01:37 PM
A maybe little longwinded solution to give the address of the first cell
you leaved after opening the workbook :

'In the Selection_Change event of the sheet
Option Explicit
Dim Exitcell As Range
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

On Error Resume Next
If Starter = True Then
MsgBox "leaving cell " & Range("IV65536").Value
Starter = False
GoTo verder
End If

If Not Exitcell Is Nothing And Target.Address <> Exitcell.Address Then
MsgBox "leaving cell " & Exitcell.Address
End If

verder:

Set Exitcell = Target
Range("IV65536").Value = Target.Address
End Sub

'in a module
Option Explicit
Global Starter As Boolean
Sub StartCell()
Starter = True
End Sub

'in the Workbook_Beforeclose and open events
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Save
End Sub

Private Sub Workbook_Open()
StartCell
End Sub

but it shure works.... :-\

Insomniac
08-30-2003, 10:39 PM
I would store the last cell address in a hidden named range which is saved with the workbook and won't effect the used range of the sheet.

This will create a new name range for each sheet the 1st time selection is changed, from then on you will always know what the last selection was.
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Excel.Range)
On Error GoTo AddShNm
MsgBox "leaving cell " & Names("LastCell_" & Sh.Name)
AddShNm:
ThisWorkbook.Names.Add Name:="LastCell_" & Sh.Name, RefersTo:= _
"=" & Sh.Name & "!" & Target.Address, Visible:=False
On Error GoTo 0
End Sub
(Set the visible to true so names can be viewed in Insert>>Name>>Define>>)

italkid
08-31-2003, 03:36 AM
Great idea Insomniac.... :)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum