Link to home
Start Free TrialLog in
Avatar of deedub84
deedub84Flag for United States of America

asked on

Trigger excel vba routine only when cursor leaves range?

Hi Experts,

I'm creating a spreadsheet that has a number of input ranges that the user will need to fill in.

For each of these ranges, I'd like to trigger a routine that runs only when the cursor moves outside that particluar range, rather than on every change within each range.

Not sure how I might accomplish that.

Thanks,
Deedub84
Avatar of Anthony Berenguel
Anthony Berenguel
Flag of United States of America image

When you say "cursor" do you mean the mouse cursor or the cell focus? I just want to be sure I understand exactly what you want.
Avatar of deedub84

ASKER

You're right, the cell focus.  Thanks for clarifying...
Avatar of Saqib Husain
You might try something like this. This will not work the first time the worksheet is opened. Thereafter it will work. This particular routine checks for F12

Public ptarget As Range

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If ptarget Is Nothing Then
    Set ptarget = Target
Else
    If ptarget.Address = Range("F12").Address Then Call abc
    Set ptarget = Target
End If
End Sub

Sub abc()
'Your processing goes here
End Sub

Open in new window

Here's a solution that might work for you. Try it out.
deedub84-Trigger-excel-vba-routi.zip
ASKER CERTIFIED SOLUTION
Avatar of dlmille
dlmille
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
NFP:

You might also want:
Private Sub Worksheet_Activate()
    Set pTarget = ActiveCell
End Sub

Open in new window


and something in Workbook_Open to check if that sheet is active, and if so assign the active cell.
Perfect thanks!