View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Rick Rothstein \(MVP - VB\) Rick Rothstein \(MVP - VB\) is offline
external usenet poster
 
Posts: 2,202
Default Macro Activation

<<Damn, I hit Ctrl+Enter by mistake

I think the following code framework will let you do what you want.
Copy/Paste it into the code window for the sheet with your named range on it
(right-click the sheet's tab on the bottom and select View Code to get to
the VB editor window for that sheet)...

Dim WasInsideRange As Boolean

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim NamedRange As Range
Set NamedRange = Range("MyNamedRange")
If Application.Intersect(NamedRange, Target) Is Nothing Then
If WasInsideRange Then
MsgBox "Moved out of named range"
End If
WasInsideRange = False
Else
WasInsideRange = True
End If
End Sub

Just replace the Msgbox statement with the code you want to run when the
user leaves the named range. The code assumes your named range is called
MyNamedRange... change it to match your actual named range's name. The
WasInsideRange variable and testing being done on it is an attempt to run
your code only one time; otherwise, without it, the code would run each time
the user click a cell outside of the named range. As it is now, each time
the user click's into your range, the WasInsideRange variable is set to
True. As long as the user stays within the named range, nothing else
happens. As soon as they click outside the range, the WasInsideRange
variable is reset and your code is run. Any other clicks outside the range
will not run your code again. However, if the user clicks inside the named
range a second time, and then clicks outside it once more, your code will be
re-run.

Rick



"Rick B" wrote in message
...
Hi, I am relatively new to programming in Excel. Everything I know and
don't
know is a result of being self taught through reading and playing around
in
visual basic in Excel.
I am trying to make a macro run automatically in a spreadsheet when the
user
moves from a particular range of cells. I have recorded the macro as a
module. I realize that the code needs to be placed in the worksheet for
this
macro to work. I am just not exactly sure how to generate the necessary
code
to activate the macro.

My range of cells is named. When the user fills out their information
within
the named range of cells and they navigate from that range, the macro
would
initiate.

Any suggestions or help would greatly be appreciated.
--
Rick B