How can we wait for specific sheet to calculate?

ballgnm

New Member
Joined
Jan 16, 2023
Messages
12
Office Version
  1. 2021
  2. 2019
  3. 2016
  4. 2013
Platform
  1. Windows
Hello, I'm making work related excel macro and intead of using Application.Calculation = xlCalculationManual, I'm using Sheets("sheetname").EnableCalculation = False to avoid "You broke something" kind of words. Because if it crashes or stops unintended way when calculation was set to manual, it stays that way for whole Excel program.
It's used by many people so I don't want to access their computers or go to their rooms everytime their excel calculation has turned into Manual so I have to make it automatic manually.

But later doesn't seem to calculate automatically and I can't tell if my calculation is done or not.
Because of that calculation-heavy sheets are not calculated properly.

Application.CalculationState always gives me xlDone state as 0. So checking this after Sheets("sheetname").EnableCalculation = true doesn't really help.

Code:
Sheets("sheetname").EnableCalculation = True
Sheets("sheetname").Calculate
Debug.Print "sheetname: " & CStr(Application.CalculationState) 'gives me "sheetname: 0" even when calculation isn't finished yet.
Sheets("sheetname").EnableCalculation = False
Is there any way to check if calculation process has ended?

Sheets have lot of formulas. It takes a lot of time to calculate so I have to disable calculation
similar to this thread VBA to ensure that workbook/worksheet(s) are calculated fully?
But there doesn't seem to have actual solution there.
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
The solution is posted 3 times - use Do Events, although it is the link in the thread you linked to that indicates that worked.

VBA Code:
Do Until Application.CalculationState = xlDone
   DoEvents
Loop
or this
VBA Code:
If Not Application.CalculationState = xlDone Then
    DoEvents
End If
or this
VBA Code:
Do
    DoEvents
    Application.Calculate
Loop While Not Application.CalculationState = xlDone
or perhaps move some code to the Application.AfterCalculation event
 
Upvote 0
Hello, and thank you for your answer
It was condition check that was preventing the calculation.
It was my fault for not noticing it
Code was more like
VBA Code:
If condition = true then
    'bunch of lines of code
    Sheets("sheetname").EnableCalculation = True
    Sheets("sheetname").Calculate
    Debug.Print "sheetname: " & CStr(Application.CalculationState)
    Sheets("sheetname").EnableCalculation = False
    'bunch of lines of code
End If
Debug.Print "sheetname: " & CStr(Application.CalculationState)
I couldn't find "delete thread" or "edit thread" button after I found my mistake.
I'm sorry for misleading information for that.
 
Upvote 0
Solution
NP. Just mark your latest post as the solution if you didn't use anyone else's solution.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,286
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top