Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Run Macro on cell exit

Hi group I am using the Office 2000 package

I am looking ofr a way to run a macro on the exit of a
cell.


Example I want to run the code each time the target leaves
A1


I know you can run a macro each time there is a change of
cell address that is not equal to A1 by using

"Worksheet_SelectionChange"

If Target.Address < A1 Then

Bla Bla Bla

But I would prefer to not run this entire code each time
the cell target changes. Just when it leaves A1

One idea I had was to have part of a code run each time
the address was equal to A1 then just hold until the
address was not equal to A1 and then run. I am not sure
how to do this though.

Make sense? Other ideas?

Kelly


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,885
Default Run Macro on cell exit

Hi
try something like the following:


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim Oldselection as range
If Intersect(Oldselection, Me.Range("A1")) is not nothing then
'run your code
end if

set oldselection = target
End Sub


--
Regards
Frank Kabel
Frankfurt, Germany


Kelly wrote:
Hi group I am using the Office 2000 package

I am looking ofr a way to run a macro on the exit of a
cell.


Example I want to run the code each time the target leaves
A1


I know you can run a macro each time there is a change of
cell address that is not equal to A1 by using

"Worksheet_SelectionChange"

If Target.Address < A1 Then

Bla Bla Bla

But I would prefer to not run this entire code each time
the cell target changes. Just when it leaves A1

One idea I had was to have part of a code run each time
the address was equal to A1 then just hold until the
address was not equal to A1 and then run. I am not sure
how to do this though.

Make sense? Other ideas?

Kelly

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,885
Default Run Macro on cell exit

Hi
sorry. Change the line
Dim Oldselection as range

to
Static Oldselection as range


--
Regards
Frank Kabel
Frankfurt, Germany


Frank Kabel wrote:
Hi
try something like the following:


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim Oldselection as range
If Intersect(Oldselection, Me.Range("A1")) is not nothing then
'run your code
end if

set oldselection = target
End Sub



Kelly wrote:
Hi group I am using the Office 2000 package

I am looking ofr a way to run a macro on the exit of a
cell.


Example I want to run the code each time the target leaves
A1


I know you can run a macro each time there is a change of
cell address that is not equal to A1 by using

"Worksheet_SelectionChange"

If Target.Address < A1 Then

Bla Bla Bla

But I would prefer to not run this entire code each time
the cell target changes. Just when it leaves A1

One idea I had was to have part of a code run each time
the address was equal to A1 then just hold until the
address was not equal to A1 and then run. I am not sure
how to do this though.

Make sense? Other ideas?

Kelly

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Run Macro on cell exit

Frank -

OldSelection needs to be either declared as a global variable, or else
as a static variable.

The "Not" needs to come before Intersect - not requires an expression
that can be evaluated as boolean rather than Nothing.


In article ,
"Frank Kabel" wrote:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim Oldselection as range
If Intersect(Oldselection, Me.Range("A1")) is not nothing then
'run your code
end if

set oldselection = target
End Sub

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,885
Default Run Macro on cell exit

Hi JE
saw the missing Static also :-)

the 'not' part though is a little bit embarassing (should not code
within Outlook Express...9
thanks for the correction!


--
Regards
Frank Kabel
Frankfurt, Germany


JE McGimpsey wrote:
Frank -

OldSelection needs to be either declared as a global variable, or

else
as a static variable.

The "Not" needs to come before Intersect - not requires an expression
that can be evaluated as boolean rather than Nothing.


In article ,
"Frank Kabel" wrote:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim Oldselection as range
If Intersect(Oldselection, Me.Range("A1")) is not nothing then
'run your code
end if

set oldselection = target
End Sub




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Run Macro on cell exit

One way:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Static rOldCell As Range
If Not rOldCell Is Nothing Then
If Not Intersect(rOldCell, Range("A1")) Is Nothing Then
'your code here
End If
End If
Set rOldCell = Target
End Sub


In article ,
"Kelly" wrote:

Hi group I am using the Office 2000 package

I am looking ofr a way to run a macro on the exit of a
cell.


Example I want to run the code each time the target leaves
A1


I know you can run a macro each time there is a change of
cell address that is not equal to A1 by using

"Worksheet_SelectionChange"

If Target.Address < A1 Then

Bla Bla Bla

But I would prefer to not run this entire code each time
the cell target changes. Just when it leaves A1

One idea I had was to have part of a code run each time
the address was equal to A1 then just hold until the
address was not equal to A1 and then run. I am not sure
how to do this though.

Make sense? Other ideas?

Kelly

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Run Macro on cell exit


-----Original Message-----
Hi group I am using the Office 2000 package

I am looking ofr a way to run a macro on the exit of a
cell.


Example I want to run the code each time the target

leaves
A1


I know you can run a macro each time there is a change of
cell address that is not equal to A1 by using

"Worksheet_SelectionChange"

If Target.Address < A1 Then

Bla Bla Bla

But I would prefer to not run this entire code each time
the cell target changes. Just when it leaves A1

One idea I had was to have part of a code run each time
the address was equal to A1 then just hold until the
address was not equal to A1 and then run. I am not sure
how to do this though.

Make sense? Other ideas?

Kelly


.
Thanks Frank and GE


That worked but I created a new problem.

Within my code I am changeing the interior color of
several diferent ranges and each time I have a ".select"
in my code the "Worksheet_SelectionChange" code runs
again. Sort of like a circular referance I guess?

Anyway is there a way to change the color of a named range
without actually selecting the range and setting off the
Worksheet_SelectionChange code every time?

Other ideas?

Thanks

Kelly
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Run Macro on cell exit

Hi Kelly!

How 'bout this:
- copy your procedure code into its own procedure name (e.g. 'foo')
- declare a global boolean variable (e.g. blnRun) and set you
procedure to run only if blnRun is True.
Whenever your code is ready to change a color, set blnRun to False
change your color, then reset blnRun to True again after

i.e.
Code
-------------------


:
:
blnRun = False
{change color code}
blnRun = True
:
:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
if blnRun then
call Foo(Target)
end if
End Sub

Private Sub Foo(ByVal targ as Range)
Static rOldCell As Range
If Not rOldCell Is Nothing Then
If Not Intersect(rOldCell, Range("A1")) Is Nothing Then
'your code here
End If
End If
Set rOldCell = targ
End Sub


-------------------




fendwick



*
That worked but I created a new problem.

Within my code I am changeing the interior color of
several diferent ranges and each time I have a ".select"
in my code the "Worksheet_SelectionChange" code runs
again. Sort of like a circular referance I guess?

Anyway is there a way to change the color of a named range
without actually selecting the range and setting off the
Worksheet_SelectionChange code every time?

Other ideas?

Thanks

Kelly


--
Message posted from http://www.ExcelForum.com

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Run macro on exit Steve Excel Discussion (Misc queries) 12 October 25th 09 01:43 PM
Macro on exit cell nonshedders Excel Discussion (Misc queries) 1 April 12th 06 12:41 PM
How do I exit a macro subroutine? John Excel Worksheet Functions 1 January 15th 06 02:08 AM
Exit via macro only? Brisbane Rob Excel Discussion (Misc queries) 1 October 13th 05 01:16 PM
on exit macro ditchy Excel Discussion (Misc queries) 2 May 3rd 05 12:11 AM


All times are GMT +1. The time now is 06:44 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"