Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / New Users / February 2007

Tip: Looking for answers? Try searching our database.

Macros on exit

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Manuel Murieta - 13 Feb 2007 16:30 GMT
Is there a way to run a macro on exiting a cell. For example if I answer Yes
in cell A4, then I would like it to automatically go to A10 on exiting A4.
Gord Dibben - 13 Feb 2007 16:52 GMT
Manuel

Option Compare Text
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.Cells.Count > 1 Then Exit Sub
   If Intersect(Target, Me.Range("A4")) Is Nothing Then Exit Sub
   On Error GoTo CleanUp
   Application.EnableEvents = False
   With Target
       If .Value = "yes" Then
           Range("A10").Select
       End If
   End With
CleanUp:
   Application.EnableEvents = True
End Sub

This is event code.  Right-click on the sheet tab and "View Code".

Copy/paste the above into that sheet module.

Gord Dibben  MS Excel MVP

>Is there a way to run a macro on exiting a cell. For example if I answer Yes
>in cell A4, then I would like it to automatically go to A10 on exiting A4.
JE McGimpsey - 13 Feb 2007 17:04 GMT
one way:

Put this in your worksheet code module (right-click the worksheet tab
and choose View Code):

   Dim rOldActiveCell As Range
   
   Private Sub Worksheet_Activate()
       Set rOldActiveCell = ActiveCell
   End Sub
   
   Private Sub Worksheet_Deactivate()
       Set rOldActiveCell = Nothing
   End Sub

   Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
       If Not rOldActiveCell Is Nothing Then
           With Range("A4")
               If Not Intersect(.Cells, rOldActiveCell) Is Nothing Then
                   If LCase(.Value) = "yes" Then
                       On Error Resume Next
                       Application.EnableEvents = False
                       Range("A10").Activate
                       Application.EnableEvents = True
                       On Error GoTo 0
                   End If
               End If
           End With
       End If
       Set rOldActiveCell = ActiveCell
   End Sub

> Is there a way to run a macro on exiting a cell. For example if I answer Yes
> in cell A4, then I would like it to automatically go to A10 on exiting A4.
Manuel Murieta - 14 Feb 2007 00:04 GMT
One more wrinkle here.  What if I have several of these. For example here
you have a change in A4 moving the cursor to A10 when the word "yes" is put
in A4. What if I also want the same thing to happen in A10. In other words a
"yes" in A10 send the cursor to A20; a "yes" in A20 sends the cursor to A30,
a "no"in A20 sends the cursor to A30, and a "maybe" in A20 sends the cursor
to A40.

Thanks for the help here.
> one way:
>
[quoted text clipped - 32 lines]
>> in cell A4, then I would like it to automatically go to A10 on exiting
>> A4.
JE McGimpsey - 15 Feb 2007 04:09 GMT
One way would be to duplicate the code for A4 for the other cells.

If one has a large number of cells, it may be worth developing some
other method.

> One more wrinkle here.  What if I have several of these. For example here
> you have a change in A4 moving the cursor to A10 when the word "yes" is put
> in A4. What if I also want the same thing to happen in A10. In other words a
> "yes" in A10 send the cursor to A20; a "yes" in A20 sends the cursor to A30,
> a "no"in A20 sends the cursor to A30, and a "maybe" in A20 sends the cursor
> to A40.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.