Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 49
Default Worksheet 'on Change' event

How do I refer to the cell I just changed?

I need to run the on change event but only if the cell I just changed was
one specific cell.


  #2   Report Post  
Posted to microsoft.public.excel.programming
KL KL is offline
external usenet poster
 
Posts: 201
Default Worksheet 'on Change' event

Hi Damien,

The way to refer to the changed range (look out it can be one or multiple
cells) is by using the variable Target, which is declared as the argument
for this event procedure.
This example may help you:

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.[a1]) Is Nothing Then _
MsgBox "Hello!"
End Sub

Regards,
KL


"Damien McBain" wrote in message
...
How do I refer to the cell I just changed?

I need to run the on change event but only if the cell I just changed was
one specific cell.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Worksheet 'on Change' event

One way:

Assume your "specific cell" is B4:

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Target.Address(False, False) = "B4" Then
'Do stuff
End If
End Sub

In article
,
"Damien McBain" wrote:

How do I refer to the cell I just changed?

I need to run the on change event but only if the cell I just changed was
one specific cell.

  #4   Report Post  
Posted to microsoft.public.excel.programming
KL KL is offline
external usenet poster
 
Posts: 201
Default Worksheet 'on Change' event

or a slightly shorter version:

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Target.Address = "$B$4" Then
'Do stuff
End If
End Sub

:-)

Regards,
KL


"JE McGimpsey" wrote in message
...
One way:

Assume your "specific cell" is B4:

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Target.Address(False, False) = "B4" Then
'Do stuff
End If
End Sub

In article
,
"Damien McBain" wrote:

How do I refer to the cell I just changed?

I need to run the on change event but only if the cell I just changed was
one specific cell.



  #5   Report Post  
Posted to microsoft.public.excel.programming
JMB JMB is offline
external usenet poster
 
Posts: 2,062
Default Worksheet 'on Change' event

Maybe this will help. Let's say the specific cell you want to watch for is
C5. You could check the address of the range that was just changed (first
macro). It is not very flexible in that if you insert any rows/columns or
move cell C5, it will no longer work. Also, if you changed a range (say
C3:C6) it will not recognize cell C5 was changed. If either of these issues
are problematic in your situation, the second one assumes I've named cell C5
"NamedRange" and tests to see if this cell intersects the range that was
changed.

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address = "$C$5" Then MsgBox "hello"

End Sub



Private Sub Worksheet_Change(ByVal Target As Range)
Dim Isect As Range

Set Isect = Application.Intersect(Target, Range("NamedRange"))
If Not Isect Is Nothing Then MsgBox "hello"

End Sub


"Damien McBain" wrote:

How do I refer to the cell I just changed?

I need to run the on change event but only if the cell I just changed was
one specific cell.





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Worksheet 'on Change' event

Or, without the slower function call:

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("A1")) Is Nothing Then _
MsgBox "Hello!"
End Sub



In article ,
"KL" wrote:

The way to refer to the changed range (look out it can be one or multiple
cells) is by using the variable Target, which is declared as the argument
for this event procedure.
This example may help you:

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.[a1]) Is Nothing Then _
MsgBox "Hello!"
End Sub

  #7   Report Post  
Posted to microsoft.public.excel.programming
KL KL is offline
external usenet poster
 
Posts: 201
Default Worksheet 'on Change' event

Not the same, I am afraid. The "slower function call" covers both the
change-in-the-monitored-cell-only and
multiple-cells-where-the-monitored-cell-is-one-of-them, while Target.Address
= "$B$4" only covers a one-cell change. And, honestly, you can't tell the
speed difference in this context (unless you measure it with special
software), so speed is not a factor in this equation.

Regards,
KL


"JE McGimpsey" wrote in message
...
Or, without the slower function call:

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("A1")) Is Nothing Then _
MsgBox "Hello!"
End Sub



In article ,
"KL" wrote:

The way to refer to the changed range (look out it can be one or multiple
cells) is by using the variable Target, which is declared as the argument
for this event procedure.
This example may help you:

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.[a1]) Is Nothing Then _
MsgBox "Hello!"
End Sub



  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Worksheet 'on Change' event

In article ,
"KL" wrote:

Not the same, I am afraid. The "slower function call" covers both the
change-in-the-monitored-cell-only and
multiple-cells-where-the-monitored-cell-is-one-of-them, while Target.Address
= "$B$4" only covers a one-cell change. And, honestly, you can't tell the
speed difference in this context (unless you measure it with special
software), so speed is not a factor in this equation.


Huh?

What's not the same? What does Target.Address = "$B$4" have to do with
it?
  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,758
Default Worksheet 'on Change' event

The slower function call that JE was referring to was: Me.[a1]

It turns out that [a1] is slower than range("a1").

(Nothing to do with the Intersect() part of your code.)


KL wrote:

Not the same, I am afraid. The "slower function call" covers both the
change-in-the-monitored-cell-only and
multiple-cells-where-the-monitored-cell-is-one-of-them, while Target.Address
= "$B$4" only covers a one-cell change. And, honestly, you can't tell the
speed difference in this context (unless you measure it with special
software), so speed is not a factor in this equation.

Regards,
KL

"JE McGimpsey" wrote in message
...
Or, without the slower function call:

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("A1")) Is Nothing Then _
MsgBox "Hello!"
End Sub



In article ,
"KL" wrote:

The way to refer to the changed range (look out it can be one or multiple
cells) is by using the variable Target, which is declared as the argument
for this event procedure.
This example may help you:

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.[a1]) Is Nothing Then _
MsgBox "Hello!"
End Sub


--

Dave Peterson
  #10   Report Post  
Posted to microsoft.public.excel.programming
KL KL is offline
external usenet poster
 
Posts: 201
Default Worksheet 'on Change' event

OK, sorry - got it wrong.

KL

"JE McGimpsey" wrote in message
...
In article ,
"KL" wrote:

Not the same, I am afraid. The "slower function call" covers both the
change-in-the-monitored-cell-only and
multiple-cells-where-the-monitored-cell-is-one-of-them, while
Target.Address
= "$B$4" only covers a one-cell change. And, honestly, you can't tell the
speed difference in this context (unless you measure it with special
software), so speed is not a factor in this equation.


Huh?

What's not the same? What does Target.Address = "$B$4" have to do with
it?



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
Worksheet Change event DoctorG Excel Discussion (Misc queries) 4 February 15th 06 12:53 PM
Worksheet Change event LAF Excel Discussion (Misc queries) 3 January 4th 06 02:08 AM
Change Cell from Validated List Not Firing Worksheet Change Event [email protected] Excel Programming 3 October 4th 04 03:00 AM
worksheet change event gautamvt Excel Programming 1 December 10th 03 05:15 PM
help with worksheet change event Mike NG Excel Programming 4 September 15th 03 11:46 PM


All times are GMT +1. The time now is 11:26 PM.

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"