View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.misc
Dave Peterson Dave Peterson is offline
external usenet poster
 
Posts: 35,218
Default VBA Created Worksheet Function Not Recalculating

Make sure you have excel in automatic calculation mode.

And make sure that your function gets all its ranges passed to when you write
the formula in the cell.

=myFunc(a1,a2,a3)

Option Explicit
Function myFunc(Rng1 as range, rng2 as range, rng3 as range) as double
dim myTot as double
mytot = 0
if isnumeric(rng1.value) then
mytot = mytot + rng1.value
end if
if isnumeric(rng2.value) then
mytot = mytot + rng2.value
end if
if isnumeric(rng3.value) then
mytot = mytot + rng3.value
end if
myFunc = mytot
End function

Not...
=myFunc2(a1)

Option Explicit
Function myFunc2(Rng1 as range) as double
dim myTot as double
mytot = 0
if isnumeric(rng1.value) then
mytot = mytot + rng1.value
end if
if isnumeric(rng1.offset(1,0).value) then
mytot = mytot + rng1.offset(1,0).value
end if
if isnumeric(rng1.offset(2,0).value) then
mytot = mytot + rng1.offset(2,0).value
end if
myFunc2 = mytot
End function

Excel only knows when to recalc the myfunc2 with the first cell changes--not
when A2 or A3 change.

Worst still, you won't/shouldn't be able to trust the results unless you look
right after a recalc.


Falcon Feet wrote:

I created my own Excel worksheet function in VBA. The function (formula)
works correctly when entered in a cell, but it does not recalculate when the
cell that the formula references changes. FYI, the workbook's "Calculation"
option IS set to Automatic.


--

Dave Peterson