Is there a vba code for when I exit a Target.address?

ArnMan

Board Regular
Joined
Aug 30, 2017
Messages
69
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hello,
I have a vba script that uses the Worksheet_SelectionChange(ByVal Target As Excel.Range) sub

in this sub I have multiple cells given a Target.Address if statement. like this.

VBA Code:
       ElseIf Target.Address = "$F$21" Then

            Call DCmVoltsSection
         
        ElseIf Target.Address = "$F$22" Then

            Call ACmVoltsSection
When I go into a cell it will activate another sub somewhere. Then when I hit enter, or click in a different cell it would do something else.
Is there a way to put a check in each ElseIf section to see when I am exiting the cell, and run a sub on the way out?
Like
VBA Code:
If onExit.Target.Address Then 
      do something
End IF
I've tried to search for something like that but was unable to find anything on leaving a target.address location.
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
This function will fire up if you exit cell $F$22:
VBA Code:
Dim previousAdress As String

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  If previousAdress = "$F$22" Then
    'Do something if you navigate from F22
  End If
  previousAdress = Target.Address
End Sub
Worksheet_SelectionChange event fires up when ever you navigate from one cell to another:
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
 'Do something when you navigate from-to any cell
End Sub
 
Upvote 0
This function will fire up if you exit cell $F$22:
VBA Code:
Dim previousAdress As String

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  If previousAdress = "$F$22" Then
    'Do something if you navigate from F22
  End If
  previousAdress = Target.Address
End Sub
Worksheet_SelectionChange event fires up when ever you navigate from one cell to another:
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
 'Do something when you navigate from-to any cell
End Sub
Thank you very much that is very simple and easy to understand. Probably gonna make my life easier now.

Thank you again
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,160
Members
453,021
Latest member
Justyna P

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