+ Reply to Thread
Results 1 to 7 of 7

UDFs - Improve to automatically update when input changes

  1. #1
    Forum Contributor
    Join Date
    04-13-2010
    Location
    England
    MS-Off Ver
    Office 2016 365
    Posts
    211

    UDFs - Improve to automatically update when input changes

    HI,

    I'm dipping my toe into the world of UDFs and developed two really simply ones to create an index at the start of my workbook.

    One function returns a sheet name based on the index number of the sheet, and the 2nd function returns the value of a cell in a given sheet.

    It works when i pass the inputs to the function, but if i change the inputs i have to recalculate the function manually. I have read a couple of work arounds to get the workbook to recalculate, but as i'm just learning i'd rather build the functions correctly in the first place with best practice rather than get into fudges & bad habits!

    Can someone point out what i am doing wrong and what i should be doing? Thanks for any help!

    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: UDFs - Improve to automatically update when input changes

    You can put this at the top after sub function

    Please Login or Register  to view this content.

  3. #3
    Forum Contributor
    Join Date
    04-13-2010
    Location
    England
    MS-Off Ver
    Office 2016 365
    Posts
    211

    Re: UDFs - Improve to automatically update when input changes

    That's worked thanks!

    Is it standard practice that if you want a UDF to automatically update whenever any of the inputs change that you need to set that property at the start of the code?

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

    Re: UDFs - Improve to automatically update when input changes

    I am not 100% sure. I think I have seen functions work with out volatile, but can not remember how and why.

  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,985

    Re: UDFs - Improve to automatically update when input changes

    No - you only need to make the function volatile if you use indirect references to objects. For example - changing the number you pass to the UDF you have should cause it to recalculate, but changing a sheet name will not because that is not an argument passed to the function. You should also avoid referencing activeworkbook in your code because your workbook might not be the active one when it calculates - especially if you use volatile functions. I suggest you replace ActiveWorkbook with ThisWorkbook if the code is located in the workbook, or Application.Caller.Parent.Parent if the code is in a different workbook (or an add-in).
    Everyone who confuses correlation and causation ends up dead.

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

    Re: UDFs - Improve to automatically update when input changes

    Rory!
    Thank you for clarification!

  7. #7
    Forum Contributor
    Join Date
    04-13-2010
    Location
    England
    MS-Off Ver
    Office 2016 365
    Posts
    211

    Re: UDFs - Improve to automatically update when input changes

    thank you both for the input, i'll take those points under advisement!

    Thanks!

+ 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. Slow update (how to improve)
    By Stingone in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-15-2012, 01:47 PM
  2. Replies: 0
    Last Post: 12-05-2012, 09:59 PM
  3. Automatically update existing data using a Update command button
    By 9999335 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-23-2012, 09:46 PM
  4. Using input box to improve my method
    By cereldine in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-25-2006, 07:25 AM
  5. [SOLVED] Why don't fields update automatically when new numbers are input?.
    By Frustrated in NJ in forum Excel General
    Replies: 2
    Last Post: 02-25-2005, 12:06 PM

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