Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Array formula does not calculate correctly when run from macro

Hi folks, hope someone can help with this little tear-your-hair-out
number.

I have an array formula that I use to calculate row-by-row totals from
a table range. It looks like this:
=SUM(OFFSET(TableWks,ROW(Wks_Total)-10,0,1,COLUMNS(TableWks)))
where TableWks is a multi-column, multi-row table, and Wks_Total is
the calculating column itself. There is no problem with the array
formula; it does exactly what I want it to do.

Except when I run a VBA procedure (what it does is not important, I
don't think); the result of the array formula comes up the same in
every row, corresponding to the sum of the first column in TableWks,
at procedure's end. A simple tap of the F9 key, however, and it
corrects itself.

I think this corresponds to some sort of array formula/volatility
problem as discussed here http://www.decisionmodels.com/calcsecretsj.htm
but none of the fixes there worked. Indeed, I've tried every fix I can
think of, including:
- not setting calculate to Manual at proc's start or restoring it to
Automatic at proc's end
- liberal use of DoEvents
- liberal use of .Calculate and .EnableCalculation
- various forms of copying/pasting the .FormulaArray of Wks_Total
- various forms of manually setting .FormulaArray at runtime
and none of them work. I have tried this in both Excel 2007 and 2003,
both running on Windows XP.

As I say, simply invoking calculate on the sheet--nothing deeper--
causes the array results to right themselves, as soon as the app state
has returned to user control. But something is keeping this formula
from working right while under VBA control.

Thanks in advance for any thoughts you might have.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Array formula does not calculate correctly when run from macro


You macro isn't letting the workbook fully update all the calculations.
this could be for a number of different reasons. You could be turing
off screen updating (or not trun screen updating) off.


Are you using a UDF? A UDF for the macro to run porperly must have all
the cell used in the function passed as a parameter. A UDF only gets
run when a parameter in in the function call get changed.
Here is a simply UDF

Function Myfunction()
Myfunction = Range("A1")
end function


This function will never get run because the workbook only call
functions when the cells in the parameter list get changed. A function
should look like this

Function Myfunction(Target)
Myfunction = Target
end function


Then call the function like this
=MyFunction(A1)


--
joel
------------------------------------------------------------------------
joel's Profile: 229
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=180681

Microsoft Office Help

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Array formula does not calculate correctly when run from macro

No, there are no in-cell UDFs. (This is much of what the link I
pointed to discusses, and as I already stated, nothing discussed there
as a solution has worked for me.)

In 13 years of Excel programming, I have never heard of turning screen
updating off as a cause for calculation incompleteness. But
nevertheless, I have now tried disabling it completely, and can
add .ScreenUpdating to the list of workarounds that don't work around.

On Feb 19, 1:03*am, joel wrote:
You macro isn't letting the workbook fully update all the calculations.
*this could be for a number of different reasons. *You could be turing
off screen updating (or not trun screen updating) off. *

Are you using a UDF? *A UDF for the macro to run porperly must have all
the cell used in the function passed as a parameter. *A UDF only gets
run when a parameter in in the function call get changed.
Here is a simply UDF

Function Myfunction()
Myfunction = Range("A1")
end function

This function will never get run because the workbook only call
functions when the cells in the parameter list get changed. *A function
should look like this

Function Myfunction(Target)
Myfunction = Target
end function

Then call the function like this
=MyFunction(A1)

--
joel
------------------------------------------------------------------------
joel's Profile: 229
View this thread:http://www.thecodecage.com/forumz/sh...d.php?t=180681

Microsoft Office Help


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Array formula does not calculate correctly when run from macro

One more piece of information that may be useful: Excel 2003 (filed
saved as .xls) and 2007 (as .xlsm) do not actually produce the same
result as the code call ends. In 2007, I receive #N/A errors, whereas
2003 produces the single-column-sum result described in my original
post. A manual calculation call fixes the wks in both versions
however.

I have also now tried deleting and re-adding range names at runtime.
Still no luck.
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Array formula does not calculate correctly when run from macro


Just some more thoughts. Pressing the F9 buton and getting the correct
results means that the calculations didn't complete, not that the
formula is wrong which is what you are saying. so you have to start
thinking why the formula didn't complete. The obvious answers is there
some error or the dependencies chain of events is being broken.


Lets see if there are errors. A common problem when soembody goes from
one version of excel to another on the same PC or even differnt PC is
the break option is different. Try changing the following VBA menu


Tools - Options - General - Error Trapping.


Set the error trapping to "Break on all Errors". Maybe this will give
some clues. I can't tell if your formula is giving valid rows and
column locations. Maybe you are getting an error becuse your row/column
is less than 1, or you column reference is greater than 256.


--
joel
------------------------------------------------------------------------
joel's Profile: 229
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=180681

Microsoft Office Help



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Array formula does not calculate correctly when run from macro

On Thu, 18 Feb 2010 19:42:13 -0800 (PST), downwitch
wrote:

Except when I run a VBA procedure (what it does is not important, I
don't think);


Since it works when you don't run the VBA procedure, and doesn't work when you
do run it ... it might be a whole lot easier to find your problem if you post
the code.

There are so many possibilities ..
--ron
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
Why doesn't this array formula calculate properly using VBA? downwitch Excel Worksheet Functions 0 February 22nd 10 02:28 AM
functions do not calculate correctly petew812 Excel Worksheet Functions 6 August 8th 08 08:08 PM
How come Excel can't calculate any =SIN(x) or =COS(y) correctly? Shinygaia Excel Discussion (Misc queries) 6 February 14th 06 09:03 PM
Excel won't calculate my formulas correctly. Shelfish Excel Worksheet Functions 2 March 18th 05 05:29 PM
Formula doesn't calculate correctly OhMarty Excel Worksheet Functions 2 November 24th 04 01:15 AM


All times are GMT +1. The time now is 03:02 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"