PC Review


Reply
Thread Tools Rate Thread

Detecting a change in a cell

 
 
Brad Stone
Guest
Posts: n/a
 
      14th Mar 2008
Hi all,

I'm fairly new to VBA programming. I'm using Excel 2003. I need to
know when a particular cell (it's actually a single cell "range" called
MYRANGE) has changed. When I detect the change, I need to see if the
new numeric value in that cell is not zero; if it's non-zero then I need
to activate some code. Unfortunately that cell is changed via a
formula - so using Worksheet_Change is apparently not going to work.
That is, Worksheet_Change doesn't get triggered for cells of this type.

I tried using .OnTimer to repeatedly check the value of that cell, but
I couldn't get it to work very well.

It seems like there has got to be an easy way to trigger some code to
activate when a cell has changed (even for a cell that changes via a
formula). Thanks for any help!

-Brad

 
Reply With Quote
 
 
 
 
Mike H
Guest
Posts: n/a
 
      14th Mar 2008
Brad,

You can still use the worksheet change event but you must use the cell that
changes your cell to zero. For example this on worksheet2 uses the change
event for cell A1 but then goes on to look at Sheet 1 a1

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$1" Then
If Sheets("Sheet1").Range("A1").Value = 0 Then
MsgBox "A1 on sheet 1 just changed to " &
Sheets("Sheet1").Range("A1").Value
' do something
End If
End If
End Sub


Mike

"Brad Stone" wrote:

> Hi all,
>
> I'm fairly new to VBA programming. I'm using Excel 2003. I need to
> know when a particular cell (it's actually a single cell "range" called
> MYRANGE) has changed. When I detect the change, I need to see if the
> new numeric value in that cell is not zero; if it's non-zero then I need
> to activate some code. Unfortunately that cell is changed via a
> formula - so using Worksheet_Change is apparently not going to work.
> That is, Worksheet_Change doesn't get triggered for cells of this type.
>
> I tried using .OnTimer to repeatedly check the value of that cell, but
> I couldn't get it to work very well.
>
> It seems like there has got to be an easy way to trigger some code to
> activate when a cell has changed (even for a cell that changes via a
> formula). Thanks for any help!
>
> -Brad
>
>

 
Reply With Quote
 
Brad Stone
Guest
Posts: n/a
 
      14th Mar 2008
Unfortunately the cell I'm interested in is changed by the summation of dozens
of other cells (i.e. a formula is used). And those cells in turn are changed
by other formulas. And since Worksheet_Change doesn't work on cells with
formulas, it's very difficult to figure out how to use Worksheet_Change to
figure out if the cell I'm interested is currently a zero or not.

Seems like Excel should provide a way to sense if a cell has changed,
regardless if that cell was modified manuall or via a formula. If I could
sense that my cell has changed then I could see if it's a zero or not and
execute code accordingly. Worksheet_Change would be perfect if it worked for
cells that change via formulas.

Thanks,
Brad



Mike H wrote:

> Brad,
>
> You can still use the worksheet change event but you must use the cell that
> changes your cell to zero. For example this on worksheet2 uses the change
> event for cell A1 but then goes on to look at Sheet 1 a1
>
> Private Sub Worksheet_Change(ByVal Target As Range)
> If Target.Address = "$A$1" Then
> If Sheets("Sheet1").Range("A1").Value = 0 Then
> MsgBox "A1 on sheet 1 just changed to " &
> Sheets("Sheet1").Range("A1").Value
> ' do something
> End If
> End If
> End Sub
>
> Mike
>
> "Brad Stone" wrote:
>
> > Hi all,
> >
> > I'm fairly new to VBA programming. I'm using Excel 2003. I need to
> > know when a particular cell (it's actually a single cell "range" called
> > MYRANGE) has changed. When I detect the change, I need to see if the
> > new numeric value in that cell is not zero; if it's non-zero then I need
> > to activate some code. Unfortunately that cell is changed via a
> > formula - so using Worksheet_Change is apparently not going to work.
> > That is, Worksheet_Change doesn't get triggered for cells of this type.
> >
> > I tried using .OnTimer to repeatedly check the value of that cell, but
> > I couldn't get it to work very well.
> >
> > It seems like there has got to be an easy way to trigger some code to
> > activate when a cell has changed (even for a cell that changes via a
> > formula). Thanks for any help!
> >
> > -Brad
> >
> >


 
Reply With Quote
 
Peter T
Guest
Posts: n/a
 
      14th Mar 2008
Just to add if Brad is new to the topic - Worksheet event code goes in the
relevant sheet module. Rt-click the sheet tab - View code takes you directly
into it.

Look at the top-mid drop down and select worksheet, then select the desired
event from the top right dropdown.

Although normally it's best to qualify a range to a particular sheet, in the
case of code in the sheet's object module it's best not to, unless the
intention is to refer to a range on another sheet..
Range("A1") in a worksheet module will always refer to it's own sheet, not
necessarily the activesheet or even a sheet in activeworkbook.

> > Unfortunately that cell is changed via a
> > formula - so using Worksheet_Change is apparently not going to work.
> > That is, Worksheet_Change doesn't get triggered for cells of this type.


You need to trap the change of one or more constant cells directly or
indirectly referenced by the formula.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
On Error GoTo errExit
Application.EnableEvents = False
Set rng = Range("C1").Precedents

If Not Intersect(Target, rng) Is Nothing Then

If Range("C1").Value <> 0 Then
' do something
MsgBox Range("C1").Value
End If
End If

errExit:
Application.EnableEvents = True
End Sub

The above is looking for any change in cell(s) linked to C1. Big caveat,
unfortuantely rng.Precedents does not return cells on other sheets, more
work to do if necessary

It might be worth experimenting with similar code in the sheet's Calculate
event (not code in each of course)

Regards,
Peter T


"Mike H" <(E-Mail Removed)> wrote in message
news:FDB6B3B8-1556-4466-A4DF-(E-Mail Removed)...
> Brad,
>
> You can still use the worksheet change event but you must use the cell

that
> changes your cell to zero. For example this on worksheet2 uses the change
> event for cell A1 but then goes on to look at Sheet 1 a1
>
> Private Sub Worksheet_Change(ByVal Target As Range)
> If Target.Address = "$A$1" Then
> If Sheets("Sheet1").Range("A1").Value = 0 Then
> MsgBox "A1 on sheet 1 just changed to " &
> Sheets("Sheet1").Range("A1").Value
> ' do something
> End If
> End If
> End Sub
>
>
> Mike
>
> "Brad Stone" wrote:
>
> > Hi all,
> >
> > I'm fairly new to VBA programming. I'm using Excel 2003. I need to
> > know when a particular cell (it's actually a single cell "range" called
> > MYRANGE) has changed. When I detect the change, I need to see if the
> > new numeric value in that cell is not zero; if it's non-zero then I need
> > to activate some code. Unfortunately that cell is changed via a
> > formula - so using Worksheet_Change is apparently not going to work.
> > That is, Worksheet_Change doesn't get triggered for cells of this type.
> >
> > I tried using .OnTimer to repeatedly check the value of that cell, but
> > I couldn't get it to work very well.
> >
> > It seems like there has got to be an easy way to trigger some code to
> > activate when a cell has changed (even for a cell that changes via a
> > formula). Thanks for any help!
> >
> > -Brad
> >
> >



 
Reply With Quote
 
JLGWhiz
Guest
Posts: n/a
 
      14th Mar 2008
This is just an outline of a possible method:

1. Capture the value of the target cell at the beginning of the procedure
and store it in a variable, exmpl: compVal = Range("D10").Value.

2. Incorporate an If ... Then statement at the end of each sub routine that
might change the value of cell D10, exmpl:

If Range("D10").Value <> compVal Then
Opt = MsgBox "Range("D10") value has changed to " _
& Range("D10").Value, Run Macro?, vbYesNo, "Choose"
If Opt = vbYes Then
myMacro
End If
End If

3. If you will need to check the value again before the porcedure ends then
you could just reset the compVal value within the If...Then statement.

It is kind of a rustic way to do it. But it can work.

"Brad Stone" wrote:

> Hi all,
>
> I'm fairly new to VBA programming. I'm using Excel 2003. I need to
> know when a particular cell (it's actually a single cell "range" called
> MYRANGE) has changed. When I detect the change, I need to see if the
> new numeric value in that cell is not zero; if it's non-zero then I need
> to activate some code. Unfortunately that cell is changed via a
> formula - so using Worksheet_Change is apparently not going to work.
> That is, Worksheet_Change doesn't get triggered for cells of this type.
>
> I tried using .OnTimer to repeatedly check the value of that cell, but
> I couldn't get it to work very well.
>
> It seems like there has got to be an easy way to trigger some code to
> activate when a cell has changed (even for a cell that changes via a
> formula). Thanks for any help!
>
> -Brad
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Detecting the change of a certain cell Fan Zhang Microsoft Excel Programming 0 25th Jul 2008 07:45 AM
Detecting value change in a datagrid cell =?Utf-8?B?TWlrZQ==?= Microsoft Dot NET 2 1st Feb 2007 04:24 AM
Cell value - detecting change tgmjf Microsoft Excel Worksheet Functions 2 20th Jul 2006 08:12 PM
Detecting a change in a cell =?Utf-8?B?U3Rldm9XYXJieQ==?= Microsoft Excel Misc 2 1st Jul 2004 04:48 PM
script detecting the change of active cell Jack Microsoft Excel Programming 1 12th Feb 2004 01:59 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:42 AM.