Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 58
Default calculationstate

there is a code:

..
..
..
application.calculate
..
..
..
..


application.calculate initiates calculation end execution of vba code is
continued.
How to hold code execution until calculation is finished?





  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default calculationstate

to the best of my knowledge, Calculate doesn't operate asynchronously, so it
shouldn't be an issue.

If it is for some reason, then break your code into two pieces (calculate at
the end of the first). Then use Application.OnTime to start the second
section of code with an appropriate delay.

--
Regards,
Tom Ogilvy


"Kris" wrote in message
...
there is a code:

.
.
.
application.calculate
.
.
.
.


application.calculate initiates calculation end execution of vba code is
continued.
How to hold code execution until calculation is finished?







  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default calculationstate

another approach would be

.
.
.
application.calculate

DoEvents
.
.
.
.


But again, I don't think this is necessary.

--
Regards,
Tom Ogilvy


af
"Kris" wrote in message
...
there is a code:

.
.
.
application.calculate
.
.
.
.


application.calculate initiates calculation end execution of vba code is
continued.
How to hold code execution until calculation is finished?







  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 58
Default calculationstate

Tom Ogilvy wrote:
to the best of my knowledge, Calculate doesn't operate asynchronously, so it
shouldn't be an issue.


When I put break after calculate and I look at call stack I have


vbaproject.sheet1.worksheet.change
<non basic code
vbaproject.udf.myfun


udf is my module

it means that myfun which is a udf function was called by excel from one
of cells, this function was interrupted by excel and my code in
worksheet_change event is continued.
When worksheet_change is finished excel goes back and finishes calculation.

But I can't continue worksheet_change without calculated values in cells.








If it is for some reason, then break your code into two pieces (calculate at
the end of the first). Then use Application.OnTime to start the second
section of code with an appropriate delay.


Calculation time is now 5 seconds, I can't add any second more.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 703
Default calculationstate

Maybe try

Application.EnableEvents = False
Application.Calculate
Application.EnableEvents = True


"Kris" wrote:

Tom Ogilvy wrote:
to the best of my knowledge, Calculate doesn't operate asynchronously, so it
shouldn't be an issue.


When I put break after calculate and I look at call stack I have


vbaproject.sheet1.worksheet.change
<non basic code
vbaproject.udf.myfun


udf is my module

it means that myfun which is a udf function was called by excel from one
of cells, this function was interrupted by excel and my code in
worksheet_change event is continued.
When worksheet_change is finished excel goes back and finishes calculation.

But I can't continue worksheet_change without calculated values in cells.








If it is for some reason, then break your code into two pieces (calculate at
the end of the first). Then use Application.OnTime to start the second
section of code with an appropriate delay.


Calculation time is now 5 seconds, I can't add any second more.




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 58
Default calculationstate

Charlie wrote:
Maybe try

Application.EnableEvents = False
Application.Calculate
Application.EnableEvents = True


No.
I doesn't work. Still the same.


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 58
Default calculationstate

Tom Ogilvy wrote:
another approach would be


.
.
.
application.calculate


DoEvents



No. It doesn't help.

There are no events waiting.

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 703
Default calculationstate

Are saying you want all sheet calculations to complete first, then the
Worksheet_Change event to fire last? (But the calculations are firing it
prematurely?) Yea? Nay?

Maybe you can re-fire the Worksheet_Change event after the
Application.EnableEvents = True statement. I don't know how to do that.
Tom? Jim? Chip?

Or you may need to rewrite your logic to move or duplicate what it is you
are doing in the Worksheet_Change sub to your main code.

That's about all I can think of.

"Kris" wrote:

Charlie wrote:
Maybe try

Application.EnableEvents = False
Application.Calculate
Application.EnableEvents = True


No.
I doesn't work. Still the same.



  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default calculationstate

So I see

--
Regards,
Tom Ogilvy


"Kris" wrote in message
...
Tom Ogilvy wrote:
another approach would be


.
.
.
application.calculate


DoEvents



No. It doesn't help.

There are no events waiting.



  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default calculationstate

How can a calculate fire a Change event. Are there DDE links?

More likely a change event fires a calculate.

--
Regards,
Tom Ogilvy

"Charlie" wrote in message
...
Are saying you want all sheet calculations to complete first, then the
Worksheet_Change event to fire last? (But the calculations are firing it
prematurely?) Yea? Nay?

Maybe you can re-fire the Worksheet_Change event after the
Application.EnableEvents = True statement. I don't know how to do that.
Tom? Jim? Chip?

Or you may need to rewrite your logic to move or duplicate what it is you
are doing in the Worksheet_Change sub to your main code.

That's about all I can think of.

"Kris" wrote:

Charlie wrote:
Maybe try

Application.EnableEvents = False
Application.Calculate
Application.EnableEvents = True


No.
I doesn't work. Still the same.







  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 58
Default calculationstate

Charlie wrote:


Are saying you want all sheet calculations to complete first, then the
Worksheet_Change event to fire last? (But the calculations are firing it
prematurely?) Yea? Nay?


Worksheet_change is fired by a user who changes something on a sheet.
Because calculation is set to manual I must recalculate sheet.

Calcualate can fire worksheet_change once again, but I exit it if target
is out of specific region. It can happen when calcuate change something
of that sheet. But it doesn't and I quit worksheet_change if this is out
of region I am interested in.


application.calculate doesn't wait.
so the rest of code doesn't make sense, because data is not updateed on
the rest of sheets.




Or you may need to rewrite your logic to move or duplicate what it is you
are doing in the Worksheet_Change sub to your main code.


The only thing which I can do is to remove the rest of code from
worksheet_change, put it as a separate procedure and add button to
worksheet. Now user can finish code manualy, but I don't want to do that.

  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 58
Default calculationstate

Tom Ogilvy wrote:
How can a calculate fire a Change event.


You are right calculate doesn't fire worksheet_change, but it doesn't
change my situation



More likely a change event fires a calculate.


in automatic calculation yes

in manual you must do this manualy by calling application.calculate or
activesheet.calculate or similar.
but the problem is if code in worksheet_change needs updated data.
There is no way to wait until calculation is finished.

  #13   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 58
Default calculationstate

Tom Ogilvy wrote:
to the best of my knowledge, Calculate doesn't operate asynchronously, so it
shouldn't be an issue.



I checked it on simple example.
Calculate waits if it is a simple workbook.
But my is much much more complicated and I have much much more
dependecies than 65535 which causes full calculation all time.
Excel is lost in that case and calucalte method works different, but who
knows how.
  #14   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default calculationstate


It would be easier to help if I could see your change sub section but
this is a example of one. You have to be careful or you can make an
endless loop where the user makes a change and then the application
makes a change which fires the change event again....


Code:
--------------------
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.EnableEvents = False
Application.DisplayAlerts = False
Application.Calculation = xlManual
' Code to execute for processing of user data
' I use this area to add formulas and
' validate user inputs prior to accepting them
Application.Calculate
Application.DisplayAlerts = True
Application.EnableEvents = True
Application.Calculation = xlAutomatic

End Sub
--------------------
Kris Wrote:
Tom Ogilvy wrote:
How can a calculate fire a Change event.


You are right calculate doesn't fire worksheet_change, but it doesn't
change my situation



More likely a change event fires a calculate.


in automatic calculation yes

in manual you must do this manualy by calling application.calculate or
activesheet.calculate or similar.
but the problem is if code in worksheet_change needs updated data.
There is no way to wait until calculation is finished.



--
CodeJunky
------------------------------------------------------------------------
CodeJunky's Profile: http://www.excelforum.com/member.php...o&userid=28896
View this thread: http://www.excelforum.com/showthread...hreadid=494985

  #15   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 58
Default calculationstate

CodeJunky wrote:
It would be easier to help if I could see your change sub section but
this is a example of one. You have to be careful or you can make an
endless loop where the user makes a change and then the application
makes a change which fires the change event again....



Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("RangeName")) Is Nothing Then
ActiveSheet.Calculate
CollectData
End If


CollectData simply copy data from other sheets and put into this one.
Three nested loops.
CollectData calls CollectSegmentData with different paramters

SheetsName is a list of sheets with data
SegmentName is a name of range with data to copied
DestinationName is a range on active sheet where data should go

number of columns in DestinationName and SegmentName are the same

number of rows of destinationName is defined by NumberOfSegmentsVisible
and equals size of SheetsName(s).




Sub CollectSegmentData(SheetsName As String, SegmentName As String,
DestinationName As String, NumberOfSegmentsVisible As Integer)

Dim iRows As Integer
Dim iCols As Integer
Dim iRow As Integer
Dim iCol As Integer
Dim Sh As Worksheet

iCols = Range(DestinationName).Columns.Count
Range(DestinationName).ClearContents
For iRow = 1 To NumberOfSegmentsVisible
If Not IsError(Range(SheetsName)(iRow).Value) Then
Dim wksName As String
wksName = Range(SheetsName)(iRow).Value
Dim wks As Worksheet
Set wks = ActiveWorkbook.Sheets(wksName)
For iCol = 1 To iCols
Dim SourceValue As Double
SourceValue = wks.Range(SegmentName)(1, iCol).Value
Dim DestCell As Range
Set DestCell = Range(DestinationName)(iRow, iCol)
DestCell.Value = SourceValue
Next iCol
End If
Next iRow
Exit Sub





The problem is that CallectData is fired before calculation is finished.
It doesn't happen on simple examples. it happens on this one.
usually
DestCell.Value = SourceValue
fails with an error.
I don't know why by destination sheet can't be modified.





  #16   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 703
Default calculationstate

It's like a circle: can't calculate without data, can't get data without
calculating. Sounds like it's time to re-examine the logic flow and design a
new method of solving the problem.

"Kris" wrote:

Charlie wrote:


Are saying you want all sheet calculations to complete first, then the
Worksheet_Change event to fire last? (But the calculations are firing it
prematurely?) Yea? Nay?


Worksheet_change is fired by a user who changes something on a sheet.
Because calculation is set to manual I must recalculate sheet.

Calcualate can fire worksheet_change once again, but I exit it if target
is out of specific region. It can happen when calcuate change something
of that sheet. But it doesn't and I quit worksheet_change if this is out
of region I am interested in.


application.calculate doesn't wait.
so the rest of code doesn't make sense, because data is not updateed on
the rest of sheets.




Or you may need to rewrite your logic to move or duplicate what it is you
are doing in the Worksheet_Change sub to your main code.


The only thing which I can do is to remove the rest of code from
worksheet_change, put it as a separate procedure and add button to
worksheet. Now user can finish code manualy, but I don't want to do that.


  #17   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 58
Default calculationstate

Charlie wrote:
It's like a circle: can't calculate without data, can't get data without
calculating. Sounds like it's time to re-examine the logic flow and design a
new method of solving the problem.

It's not a circle. There is no loop there.
Summary sheet collect data from other sheets and other sheet doesn't
need any data from summary sheet to be calculated.

  #18   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 58
Default calculationstate

Tom Ogilvy wrote:
to the best of my knowledge, Calculate doesn't operate asynchronously, so it
shouldn't be an issue.

If it is for some reason, then break your code into two pieces (calculate at
the end of the first). Then use Application.OnTime to start the second
section of code with an appropriate delay.


As a final answer:

Worksheet_change event doesn't work correctly if cell which changes its
value has data validation and you use drop down box to change that value.


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
Stop until Calculationstate = xldone Tornados[_4_] Excel Programming 0 September 28th 04 12:48 PM
Stop until Calculationstate = xldone Tornados[_3_] Excel Programming 1 September 28th 04 12:18 PM


All times are GMT +1. The time now is 08:48 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"