+ Reply to Thread
Results 1 to 5 of 5

UDF executes with a wrong parameter value

  1. #1
    Registered User
    Join Date
    03-03-2005
    Posts
    41

    UDF executes with a wrong parameter value

    Hi All
    I have a wee problem with an Exel UDF.
    The function is used in a cell in the spreadsheet typically just
    = payrate(C8,E8,$E$5 - $D8)
    and generally works fine

    But when I edit the cell with the function (in the simplest case, F2 and enter) the function code is then evaluated twice.
    For the first evaluation, one of the parameters (a calcuated value) is 0 despite having a valid value visible in the cell - the other two are correct but they are constants not formulae
    .
    I presume the second evaluation happens because, at some stage after the first evaluation, the calculated parameter is recalculated (no idea why) and this triggers a second calculation of the UDF

    But why does excel use 0 for the first calculation when there is a perfectly good value visible in the cell? it wouldn't matter greatly except that it triggers a bunch of msgbox errors


    And a minor irritation: Towards the end, the function is set with Payrate = Round(mres,2)
    But when the result is displayed on the worksheet it can be something like 8.909999847
    whereas I thought round should make it 8.91

    Thanks for any suggestions

    With apologies for the poor quality, the UDF follows:

    Please Login or Register  to view this content.

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: UDF executes with a wrong parameter value

    I think it is a good habit to define all variables. Since you mentioned recalculate, i have added a line to auto calculate.

    Please Login or Register  to view this content.

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,377

    Re: UDF executes with a wrong parameter value

    We can't review it without the file to test it on.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,814

    Re: UDF executes with a wrong parameter value

    This is a bit of an old reference, but it appears that it still applies
    Quote Originally Posted by decisionmodels
    UDFs may be evaluated more than once per workbook calculation

    Writing efficient and robust UDFs is not always simple.


    Various conditions may cause Excel to evaluate your UDF more than once during a recalculation. This means that your UDF should:

    Explicitly initialize all variables used in the UDF.
    Error-handle input from uncalculated cells (check for ISEMPTY, unexpected zeros, blanks, missing properties etc)
    Avoid doing intensive calculations until all input cells have been fully calculated
    Cache input values and output values to avoid time-intensive calculations when the input values have not changed.

    http://www.decisionmodels.com/calcsecretsj.htm
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  5. #5
    Registered User
    Join Date
    03-03-2005
    Posts
    41

    Re: UDF executes with a wrong parameter value

    Mr Shorty - Thank you.

    So it seems that its 'just something excel sometimes does'

    The subsequent advice of course is sound and I had done most of that in order to 'work round' what I perceived to be a problem. (Yes I know I should have done it anyway)
    I shall simply 'move on'.

    Thanks to those others that pitched in.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 2
    Last Post: 10-06-2014, 08:15 AM
  2. Replies: 0
    Last Post: 10-06-2014, 07:35 AM
  3. Parameter Query that takes parameter value from Excel Cell
    By Aquamore in forum Access Tables & Databases
    Replies: 1
    Last Post: 01-23-2014, 03:43 PM
  4. Replies: 0
    Last Post: 11-12-2013, 03:57 AM
  5. Parameter Query - Bad Parameter Type
    By Kyle123 in forum Excel General
    Replies: 0
    Last Post: 04-23-2013, 01:06 PM
  6. SQL Connection Executes.
    By Slyone2 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-16-2010, 03:34 PM
  7. Macro that executes upon Open
    By jerem in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-18-2009, 10:55 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1