PC Review


Reply
Thread Tools Rate Thread

Code jumps to WS_Calculate on a different sheet

 
 
JMay
Guest
Posts: n/a
 
      26th Mar 2009
I'm stepping through a Standard module on my Sheet2, but suddenly my code
jumps to a Private Sub Worksheet_Calculate which is a part of my Worksheet7.
Why is that?
 
Reply With Quote
 
 
 
 
Bernie Deitrick
Guest
Posts: n/a
 
      26th Mar 2009
If you changed a cell value that would trigger a recalc, then that happens
all the time.

It is best to preceed your code with this is you make and changes to cell in
code:

Dim myCalc As Excel.XlCalculation
With Application
.EnableEvents = False
myCalc = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With

'and finish with
With Application
.EnableEvents = True
.Calculation = myCalc 'xlCalculationAutomatic is the usual setting
.ScreenUpdating = True
End With

HTH,
Bernie
MS Excel MVP

"JMay" <(E-Mail Removed)> wrote in message
news:A8203E4D-5595-4434-BEA1-(E-Mail Removed)...
> I'm stepping through a Standard module on my Sheet2, but suddenly my code
> jumps to a Private Sub Worksheet_Calculate which is a part of my
> Worksheet7.
> Why is that?


 
Reply With Quote
 
JMay
Guest
Posts: n/a
 
      26th Mar 2009
E-X-P-A-N-D-I-N-G on your comment
"If you changed a cell value that would trigger a recalc, then that happens
all the time" -

might I embellish your statement by saying:

If you change a cell value ON ANY WORKSHEET - that triggers a recalc, AND
IF you have a Worksheet_Calculate on ANY Code-Sheet in your ActiveBook, This
Event Code Module Fires - and that happens all the time.

Right?????

Thanks, Jim

"Bernie Deitrick" wrote:


> If you changed a cell value that would trigger a recalc, then that happens
> all the time.
>
> It is best to preceed your code with this is you make and changes to cell in
> code:
>
> Dim myCalc As Excel.XlCalculation
> With Application
> .EnableEvents = False
> myCalc = .Calculation
> .Calculation = xlCalculationManual
> .ScreenUpdating = False
> End With
>
> 'and finish with
> With Application
> .EnableEvents = True
> .Calculation = myCalc 'xlCalculationAutomatic is the usual setting
> .ScreenUpdating = True
> End With
>
> HTH,
> Bernie
> MS Excel MVP
>
> "JMay" <(E-Mail Removed)> wrote in message
> news:A8203E4D-5595-4434-BEA1-(E-Mail Removed)...
> > I'm stepping through a Standard module on my Sheet2, but suddenly my code
> > jumps to a Private Sub Worksheet_Calculate which is a part of my
> > Worksheet7.
> > Why is that?

>
>

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      26th Mar 2009
Nope.

Try a small experiment.

Create a new workbook with 3 sheets.

Put this behind each worksheet:

Option Explicit
Private Sub Worksheet_Calculate()
MsgBox Me.Name
End Sub


Then go back to one sheet and type this in any cell:
=rand()
and hit enter.

Excel is pretty smart when it thinks, er, knows that it has to recalculate a
sheet.

JMay wrote:
>
> E-X-P-A-N-D-I-N-G on your comment
> "If you changed a cell value that would trigger a recalc, then that happens
> all the time" -
>
> might I embellish your statement by saying:
>
> If you change a cell value ON ANY WORKSHEET - that triggers a recalc, AND
> IF you have a Worksheet_Calculate on ANY Code-Sheet in your ActiveBook, This
> Event Code Module Fires - and that happens all the time.
>
> Right?????
>
> Thanks, Jim
>
> "Bernie Deitrick" wrote:
>
> > If you changed a cell value that would trigger a recalc, then that happens
> > all the time.
> >
> > It is best to preceed your code with this is you make and changes to cell in
> > code:
> >
> > Dim myCalc As Excel.XlCalculation
> > With Application
> > .EnableEvents = False
> > myCalc = .Calculation
> > .Calculation = xlCalculationManual
> > .ScreenUpdating = False
> > End With
> >
> > 'and finish with
> > With Application
> > .EnableEvents = True
> > .Calculation = myCalc 'xlCalculationAutomatic is the usual setting
> > .ScreenUpdating = True
> > End With
> >
> > HTH,
> > Bernie
> > MS Excel MVP
> >
> > "JMay" <(E-Mail Removed)> wrote in message
> > news:A8203E4D-5595-4434-BEA1-(E-Mail Removed)...
> > > I'm stepping through a Standard module on my Sheet2, but suddenly my code
> > > jumps to a Private Sub Worksheet_Calculate which is a part of my
> > > Worksheet7.
> > > Why is that?

> >
> >


--

Dave Peterson
 
Reply With Quote
 
Brandt
Guest
Posts: n/a
 
      3rd Jun 2009
I know it has been a while since this was posted, but I had the same problem
as you were describing (WS_Calculate is occurring even when a change is made
on another WS that should not trigger the event) and after searching I found
the following response from Jim Thomlinson which helped me out greatly. As
Jim describes, I had "volatile" functions on the sheet with the WS_Change
event and these volatile functions would calculate anytime a change was made
on the sheet or on any other sheet. Try an experiment similar to the one
Dave Peterson mentioned in this post and you will see that a =Now() or
=Today() function will calculate when any change on any sheet is made.
Hopefully it is helpful to someone else with the same problem.

Copied message as follows (from 8/30/2005)...

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

=Now() is a volatile function, meaning that it calculates every time that a
calculation is executed. If you can, try to find a way to replace the now
function with a constant. You can update the constant base on some other
event such as a sheet activate or workbook open or... Otherwise you can also
add a criteria to the calculation event that ThisWorkbook is the active
workbook. If it isn't then abort the rest of the procedure.
--
HTH...

Jim Thomlinson


"Alex" wrote:

> I'm usinin the following code to highlight the filtered column with a
> different collar.
> To make it works I've entered =Now() in some cell.
> Everything is fine. But, when I'm opening another spreadsheet the procedure
> is being automatically triggered producing the error "out of range".
>
> Private Sub Worksheet_Calculate()
> Dim af As AutoFilter
> Dim fFilter As Filter
> Dim iFilterCount As Integer
> 'Application.EnableEvents = False
> If Worksheets("Initiatives").AutoFilterMode Then ' gettin an error "out of
> range"
> Set af = Worksheets("Initiatives").AutoFilter
> iFilterCount = 1
> Worksheets("Initiatives").Unprotect ("donit")
> For Each fFilter In af.Filters
> If fFilter.On Then
> af.Range.Cells(1, iFilterCount) _
> .Interior.ColorIndex = 3 ' 15 - gray; 35 - mint green
> Else
> af.Range.Cells(1, iFilterCount) _
> .Interior.ColorIndex = 15
> End If
> iFilterCount = iFilterCount + 1
> Next fFilter
>
> Else
> Worksheets("Initiatives").Range("A1").AutoFilter
> Worksheets("Initiatives").Range("A1:H1").Interior.ColorIndex = 15
> End If
>
> How could I fix it?
>
> Thanks


 
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
First Click or Keystroke jumps you to the first sheet Netbones Microsoft Excel Misc 1 9th Dec 2009 01:49 AM
RE: Code Jumps Kassie Microsoft Excel Programming 1 14th Jan 2009 07:34 PM
On save excel jumps to another worksheet. How to fix VBA code? BillJenk Microsoft Excel Programming 2 9th Oct 2008 10:13 PM
Database Window Jumps to Tables when Running Code =?Utf-8?B?SmltIFBvY2ttaXJl?= Microsoft Access VBA Modules 3 28th Nov 2006 03:20 PM
excel - macro code to open a protected sheet, enter passowrd, and then protect sheet arunjoshi Microsoft Excel Programming 1 2nd May 2004 03:50 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:06 PM.