Excel - The worksheet calculates too often
Asked By Stefano
20-Nov-09 05:20 PM

While investigating the performances of a workbook I added a Debug.Print N
inside a function so I can see when the function code runs. N is a static
long that is increased every time. It prints also a text that allows me to
see what cell is calling the function (useful when the same function is used
in many cells.)
I use the function 5 times in 2 sheets, for a total of 10 times.
First weird thing: when I open the workbook the Immediate window shows more
than executions. The function is executed once for some cells, but 2 or 3
times for others.
I tried to close Excel and open the workbook twice: once I had 22
executions, then 20. I was expecting 10.
Second weird thing: the functions are calculated when I change a cell
referred in one function (good,) when I change a cell not referred in any
function (bad,) and when I create a new workbook and change a cell in the new
workbook (worse.)
For example: I press Ctrl+N to create a new workbook (nothing appears in the
immediate window) and press the Delete button, the Immediate window shows 14
calculations.
Thanks,
Stefano
SheetStockList.Address
(1)
Excel
(1)
Worksheet
(1)
Workbook
(1)
OFFSET
(1)
Macro
(1)
VBA
(1)
Debug.Print
(1)
Charles Williams replied to Stefano
Its quite common for a function to be calculated multiple times.
see
http://www.decisionmodels.com/calcsecretsj.htm
for some possible explanations and advice on what to do about it.
Charles
___________________________________
The Excel Calculation Site
http://www.decisionmodels.com
Stefano replied to Charles Williams

Charles, the document you mentioned was very useful. I knew most of the
things described, but it was nice to read all of them in a single well
organized document.
After reading all the points suggested in the document, I only did one
change: I added the IsEmpty() check, but the the problem is still there.
For example if I open the file with the macro, then open the VBA editor,
then create a new Excel workbook, then press "1", then arrow down, "1", arrow
down, etc., every 3-4 cells modified in the new workbook the formulas in the
other workbook are recalculated. Sometimes only a few, sometimes all of them
many times.
I cannot think of any event triggered by the change of a formula in a just
created workbook that would fire the calculation of another workbook.
Any idea?
This is an example of my function:
Function GetUsedMaterial(SheetStockList As Range) As String
CheckMultipleRuns "Before GetUsedMaterial('" &
SheetStockList.Worksheet.Name & "'!" & SheetStockList.Address & ")"
If IsEmpty(SheetStockList) Then Exit Function
CheckMultipleRuns "After GetUsedMaterial('" &
SheetStockList.Worksheet.Name & "'!" & SheetStockList.Address & ")"
[...]
End Function
This is the function that checks for multiple runs. It prints on the debug
window when a function with the same range runs twice in the same second (I
could do better, but it is good enough for a quick debug test):
Sub CheckMultipleRuns(Txt As String)
Static N As Long, AllRuns As String, ThisRun As String, TLastRun As Single
If Timer - TLastRun > 1 Then
TLastRun = Timer
AllRuns = ""
End If
ThisRun = Time & Txt
If InStr(AllRuns, ThisRun) Then
Debug.Print ThisRun, N
Else
AllRuns = AllRuns & ThisRun
End If
End Sub
Thanks,
Stefano
PS: Why when I search for "The worksheet calculates too often" this post
does not come up? Is there isomething I need to know about how to search in
this forum?
Charles Williams replied to Stefano
Excel does not calculate at workbook level: it calculates all open workbooks
not just the active workbook.
So if you have 2 workbooks open and trigger a recalculation in one of them
the other workbook will also be recalculated, even if there are no
references from the other workbook to the first workbook.
see http://www.decisionmodels.com/calcsecretsg.htm
Charles
___________________________________
The Excel Calculation Site
http://www.decisionmodels.com
Stefano replied to Charles Williams

Thanks Charles, I went through many of your pages, but I did not find anything
that would explain what I see.
The weird thing is that if I press the Delete button on any cell:
- press once, wait one second, press once again -> all my functions are
executed once
- press twice, very quickly -> the volatile functions are executed once, the
other functions are executed twice
- press twice, quickly, but not as before -> some of the non volatile
functions are executed once, others twice.
I was not able to reproduce the problem with a smaller model.
I tried creating some slow functions, either volatile or not, and they make
Excel non responsive, that is the calculation always runs once, and through
the end.
In the real case intead, it looks like the calculations starts in
background, the interface is responsive, and:
- If it has time to finish it runs only once;
- If it has no time to finish (because i press a key) it interrupts and
restarts.
Does it make sense?
Thanks,
Stefano
Charles Williams replied to Stefano
Yes calculation restarts after an interruption (you can use VBA to mask
against an interruption).
Charles
___________________________________
The Excel Calculation Site
http://www.decisionmodels.com
Stefano replied to Charles Williams
Thanks Charles, I spent one day studying this weird behavior, and now I feel
better because:
1) My UDFs are calculated at every change (regardless of the current
workbook) because they refer to cells containing the volatile function
OFFSET()
2) The range referred by the OFFSET changes very seldom (it is in a hidden
sheet,) so I will make my own non volatile MyOffset that will recalculate
only when I really need it.
3) I know Excel better
4) Using IsMissing() saves 5-10% of recalculations
Thanks,
Stefano
Win7 VirtualStore Weirdness with Excel 2002 and earlier Excel I thought I'd share a weird experience I had today in case it saves installer for it - - which registers it as an add-in for all installed versions of Excel - - and tested it. Fine in Windows 2000 in all installed versions of Excel. Boot to Windows XP, fine there too. Boot to Windows 7. Fine in Excel 2010, 2007, and 2003. But Excel 2002 and 2000 were still loading the old version of the add-in. I uninstalled the add-in, made sure it was gone, and reinstalled it. Same problem in Excel 2002 and earlier; still works fine in Excel 2003 and later. I opened the VB editor in Excel 2003 and in Excel 2002
VBA required to total numbers to match closest to target (complex one. . .) Excel I have a range of numbers as follows (sheet2, A1:A30): 1, 2, 2, 5 to even begin working it out. If anyone can help I'd be really grateful. Excel Discussions Interior (1) Borders (1) LineStyle (1) Decimal (1) Module (1) VBA (1) Application (1 Range("Target").Value For J = 1 To xItems.Count If XtargetVal = xItems(J) Then xTarget.Offset(1, 0) = "x1 = " & XtargetVal xTarget.Offset(2, 0) = "x2 = " xTarget.Offset(3, 0) = "Nearest sum = " & XtargetVal xTarget.Offset(4, 0) = "Distance = " & Format(0, "0.00000") Exit Sub End If Next J If XtargetVal xDistance Then xDistance = Abs(xelem(J, 1) - XtargetVal) xIndex = J End If Next J xTarget.Offset(1, 0) = "x1 = " & xelem(xIndex, 2) xTarget.Offset(2, 0) = "x2 = " & xelem(xIndex, 3) xTarget.Offset(3, 0) = "Nearest sum = " & xelem(xIndex, 1
32 bit DLL with 64 bit Excel 2010 Excel We have an app which creates large Excel workbooks. For example, one workbook has 1, 000 worksheets. In other cases there are fewer worksheets but the Excel file can be 80 MB or larger. Since this sometimes crashes Excel 2007 and 2003, I am considering using 64 bit Excel 2010. Would 64-bit Excel be more reliable with workbooks this size than 32-bit Excel? Is 32-bit Excel
Evitare che sullo sfondo si vedano i fogli di lavoro Excel Finalmente ho terminato il file, vorrei migliorarlo un po' , prima cosa ad esempio vorrei che dei suggerimenti su come potrei ottenere gli stesi risultati con un metodo pi = F9 serio Excel - Italian Discussions UserForm4.TextBox1.Value (1) Application.DisplayFormulaBar (1) Application.DisplayFullScreen (1) Application.CutCopyMode (1) ComboBox1.RowSource (1) TextBox1.SetFocus (1) TextBox2.SetFocus (1) Excel (1) il mio escamotage è sto quello di riempire i fogli tutti dello stesso colore sub pu = F2 essere scritta molto meglio cos = EC: Public Sub m() Dim sh1 As Worksheet Dim sh2 As Worksheet Set sh1 = 3D ThisWorkbook.Worksheets("Foglio1") Set sh2 = 3D ThisWorkbook.Worksheets("Foglio2") sh1.Range("A1 Nel 99 per centto dei casi non servono a nulla, fanno solo *perdere tempo* ad Excel e creano dei problemi a te. Copia / incolla, lettura dati, riordini e filtri tabelle, ecc tamite i metodi Protect e Unprotect del foglio stesso. Vedi la guida del vb di Excel per chiarimenti e *poi* chiedi se non ti ? chiaro. Puoi utilizzare e gestire il parametro UserInterfaceOnly del metodo Protect. Vedi sempre la guida del vb di Excel per chiarimenti e se non ti ? chiaro qualcosa, chiedi. Per nascondere il foglio, utilizza la
i VBA excel 97, 2003, 2007 sono diversi? Excel Salve a tutti, ho fatto una macro con excel 2003. Ho voluto poi utilizzarla con il 2007 sia in formato .xls, sia convertendola in nel 2007 passano minuti (parte veloce e e poi continua in na lentezza incredibile!) Nell'excel 97 pare si blocchi subito, ma devo ancora provare meglio Qualcuno da darmi la dritta oggetto: "Macro per fare dei calcoli su dati importati" del 27 / 09 / 2010) Grazie. SiNi Excel - Italian Discussions False .SaveData True .AdjustColumnWidth True .RefreshPeriod (1) TextFilePromptOnRefresh False .TextFilePlatform (1) FormatConditions (1) QueryTable (1) Row sFormula IF C (1) If Len .Value (1) Worksheets (1) Excel 2007 (1) ola le 7 ) io ho constatato che il 2007 = E8 pi = F9 lento soluzione. Per inciso, le mie macro vanno che ? un piacere su tutte le versioni di Excel. Se(se) implemento funzionalit? che le versioni precedenti non hanno, intercetto la versione in uso a farlo, io mica sono nel modulo. . .;-) ). Posta il codice, grazie. - - - -- -- -- -- -- -- -- -- -- -- -- -- -- Mauro Gamberini Microsoft MVP - Excel http: / / www.riolab.org / http: / / www.maurogsc.eu / Buongiorno a tutti voi. Eccolo: (il codice