Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 1
Default VBA Created Worksheet Function Not Recalculating

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.
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 11,501
Default VBA Created Worksheet Function Not Recalculating

Hi,

Put this as the first line of your function

Application.volatile

--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.


"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.

  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 1,522
Default VBA Created Worksheet Function Not Recalculating

As ALWAYS, post your code for comments

application.volatile

as your FIRST LINE in the macro


--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"Falcon Feet" <Falcon
wrote in message
...
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.


  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 5,939
Default VBA Created Worksheet Function Not Recalculating

Post the function. There are a couple of ways to make it calculate. it
depends on the nature of the function which will be best.

1. you can add application.volatile to the start of the function. that makes
the function volatile similar to the now() function. The good new is that it
will always recaclualte. Teh bad new is that it recalc every time a calc runs
adding a lot of overhead.

2. Add a cell reference to as an argument to the function. If the ceel
referenced in the argument changes then the formula is dirtied and will recalc

public function SumStuff(byval MyCell as range) as double
SumStuff = MyCell + MyCell.Parent.Range("A1").value
end function

The function above would be used in a cell like =SumStuff(B1). It Adds B1 to
A1. If B1 changes then the formula will recalc. If A1 is changed then the
formula will not recalc as A1 is not one of the arguments being passed in.
--
HTH...

Jim Thomlinson


"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.

  #5   Report Post  
Posted to microsoft.public.excel.misc
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


  #6   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 7,247
Default VBA Created Worksheet Function Not Recalculating

Your function should get every value used in its calculation as an
input parameter to the function. E.g.,

Function GoodPlus(V1 As Double, V2 As Double) As Double
GoodPlus = V1+V2
End Function

If your function has cell references within it, Excel doesn't know
about them, so it will not calculate when one of those reference
values is changed. E.g.,

Function BadPlus(V1 As Double) As Double
BadPlus = V1 + Range("A1").Value
End Function

This function will not recalculate when A1 is changed, because Excel
has no knowledge that A1 is used by the function and doesn't see the
change in A1 to be a reason to recalculate the function.

Cordially,
Chip Pearson
Microsoft Most Valuable Professional,
Excel, 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com



On Fri, 26 Feb 2010 07:37:01 -0800, Falcon Feet <Falcon
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.

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
Function isn't recalculating Chris Excel Worksheet Functions 2 May 22nd 09 09:06 AM
TABLE function - not recalculating correctly Jacob Excel Worksheet Functions 7 November 16th 07 07:44 PM
Edit created worksheet function davidmac1954 Excel Worksheet Functions 1 October 10th 06 06:04 PM
EXCEL2000 trouble with SUM function recalculating on new data ent huskybytes Excel Worksheet Functions 2 March 10th 06 04:17 PM
Using the NOW() function without it recalculating Sean C Excel Worksheet Functions 3 January 16th 06 10:52 PM


All times are GMT +1. The time now is 10:36 PM.

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"