+ Reply to Thread
Results 1 to 12 of 12

Custom Function calculation problem

Hybrid View

  1. #1
    Registered User
    Join Date
    02-15-2009
    Location
    Aylesbury,England
    MS-Off Ver
    Excel 2010
    Posts
    10

    Custom Function calculation problem

    I have developed a UDF for use in one workbook.

    The problem is that if another open workbook is calculated when I return to the first workbook all cells using the UDF display #VALUE! and the workbook has to be recalculated before it can be used.

    Is there a way of avoiding this irritation problem other than not opening more than one workbook.

  2. #2
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Custom Function calculation problem

    Maybe something about the UDF, post the code.

  3. #3
    Registered User
    Join Date
    02-15-2009
    Location
    Aylesbury,England
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Custom Function calculation problem

    It is used to calculate prices based on several relatively complex parameters and contains plenty of references to worksheet ranges.

    I added Application.Volatile at the start but this seems to have no effect.

    I would rather not post the code as it is business confidential.

  4. #4
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Custom Function calculation problem

    Quote Originally Posted by tonyph17 View Post
    I would rather not post the code as it is business confidential.
    Come on, get real. It is highly unlikely to contain something we haven't seen (many times) before.

  5. #5
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,396

    Re: Custom Function calculation problem

    It really is hard to provide a specific reason for your problem without the code.

    Can I assume that by UDF you mean a Function procedure that is being called from a spreadsheet cell?

    contains plenty of references to worksheet ranges.
    One suggestion: Are these references passed to the function through the argument list, or are they hard coded into the function? If the function needs information from the spreadsheet, it is usually best to pass that information through the argument list to avoid problems when the calling sheet/workbook is not the active workbook. Passing information through the argument list also eliminates (most of the time anyway) the need for application.volatile as Excel can readily figure out how the UDF fits into the calculation dependency tree.

  6. #6
    Registered User
    Join Date
    02-15-2009
    Location
    Aylesbury,England
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Custom Function calculation problem

    Most of the arguments are for various discount percentages and each argument is set to the value of the worksheet cell with the same name range.

    Is the use of values from the worksheet what is causing the problem when another open worksheet is calculated? After all these years I am surprised if there is not an effective solution.

  7. #7
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Custom Function calculation problem

    The first suggestion that I would have is to make sure that the UDF has fully qualified references. A missing dot (changing the reference to the ActiveSheet) can really mess things up.

    The second suggestion is the observation that a robust UDF does not have hard coded range references in it. Any ranges should be passed as an argument. A hypothetical LookUpInfoFor could be written with one search term argument, and have the data base range hard coded. A more robust version would have two arguments, the search term and the data base range. The DB range could be made an optional argument that defaults to your favorite location.

    Both of these suggestions are aimed at protecting the UDF from being run "at the wrong time" by eliminating "the wrong time".
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  8. #8
    Registered User
    Join Date
    02-15-2009
    Location
    Aylesbury,England
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Custom Function calculation problem

    I am afraid I do not follow the point you are making - my ability with VBA is pretty limited.

    Here is a snippet of code I am using -this is repeated a number of times to set the values of all the ranges I need to use:

    Dim d_2009B As Single

    d_2009B = Range("d_2009B").Value

    The UDF works fine so long as I keep the workbook as the active workbook but displays #VALUE! in each cell when another open workbook is calculated. I then have to recalculate the workbook with the UDF when it is active.

  9. #9
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Custom Function calculation problem

    That's exactly what I'm talking about.
    Have Range("d_2098B") passed as a argument and there won't be any problem with which sheet is open.

    I agree with everyone else that, unless you post the code of the UDF, we're pretty much useless.

  10. #10
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Custom Function calculation problem

    Actualy there is one thing you could do.
    Change that reference to
    Workbooks("someWorkbooks.xlsm").Names("d_2098B").RefersToRange

  11. #11
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,396

    Re: Custom Function calculation problem

    mikerickson is correct in that you could make sure your references are all to specific workbook/worksheet combinations. However, I'd still say that best programming practice is to pass this as an argument:

    function myfunction(d_2009b as single, d_2010b as single, d_2011b as single,...)
    (statements as needed)
    end function
    called from a spreadsheet cell as =myfunction(d_2009b,d_2010b,d_2011b,...). This allows you to control what values are passed to the function, and allows Excel to see how the cell dependencies work so it can optimize the calculation event.

  12. #12
    Registered User
    Join Date
    02-15-2009
    Location
    Aylesbury,England
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Custom Function calculation problem

    I have solved my problem by setting the values stored in the ranges as UDF constants rather than asking my UDF to read these values.

    The various Case statements I use later in the UDF then pick up the appropriate Const value and work perfectly even if another workbook is open.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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