Know which cell was just left

J

JerryH

I have a sub I want to run each time a cell is changed. However, I need to
know which cell was active when the enter key is pressed. When I look at
active cell on Worksheet_Change it gives me the current cell with focus but
not the one the data was changed on.

thanks,
Jerry
 
T

Tom Hutchins

Here is one way...

In a general VBA module in your workbook, add a Global variable (PrevCell in
this example):
Global PrevCell As Range

In the ThisWorkbook module, add the following event code:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
'Free the object variable PrevCell
Set PrevCell = Nothing
End Sub

Private Sub Workbook_Open()
'Initialize the object variable PrevCell
Set PrevCell = ActiveCell
End Sub

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target
As Range)
On Error Resume Next
'Do something with PrevCell
MsgBox "PrevCell was " & PrevCell.Address
'Store the activecell as PrevCell
Set PrevCell = Target
End Sub

Hope this helps,

Hutch
 
A

AltaEgo

Store it in a variable and update after you run your code:

Dim LastAddr
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

thisAddr = ActiveCell.Address

'your code

LastAddr = thisAddr

End Sub
 
J

Jim Cone

Target is the cell that was changed in the Worksheet_Change event.
Paste this in a sheet module and change some cells...
'--
Private Sub Worksheet_Change(ByVal Target As Range)
MsgBox Target.Address
End Sub
--
Jim Cone
Portland, Oregon USA



"JerryH"
<[email protected]>
wrote in message
I have a sub I want to run each time a cell is changed. However, I need to
know which cell was active when the enter key is pressed. When I look at
active cell on Worksheet_Change it gives me the current cell with focus but
not the one the data was changed on.
thanks,
Jerry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top