VBA to run a macro when leaving a specific active cell.

EssKayKay

Active Member
Joined
Jan 5, 2003
Messages
407
Office Version
  1. 2007
Platform
  1. Windows
I’m not positive this is going to do exactly what I want but I’d like to start here.

I’m looking for a routine that runs after leaving a specific cell (K7). That is – when the active cell is K7, and I click on some other cell or even hit enter (because K7 is no longer active) I want to run a macro. I only want this to run when leaving K7 and not run any other time.

Thanks for viewing,
Steve K.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Right-click on the sheet tab name at the bottom of the screen, select View Code, and paste this VBA code in the VB Editor that pops up:
VBA Code:
Public cellAddress As String


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

'   See if previous cell selected was K7
    If cellAddress = "$K$7" Then
        'Call Macro here
    End If
    
'   Set cell address for next run
    cellAddress = ActiveCell.Address
    
End Sub
Replace the
VBA Code:
'Call Macro here
part with the name of the code you want to run.

Now, as select cell K7 and then you move off of cell K7, it should run your code.
 
Upvote 0
Solution
Thank you very much Joe. This is what I was looking for. Now let me see if I can get this to do what I have in mind.

Again, much appreciated,
Steve
 
Upvote 0
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,182
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top