+ Reply to Thread
Results 1 to 3 of 3

Recursive/Running Function

  1. #1
    Registered User
    Join Date
    05-07-2009
    Location
    Boston
    MS-Off Ver
    Excel 2003 and 2007
    Posts
    1

    Recursive/Running Function

    Hi,

    short question: Is there a way to pass the current value of a cell into a user function, run some calculations, and then change that same cell (without using activecell)?

    Longer explanation:
    I've got a macro which I'm running to recalculate every few minutes or so as data changes. The macro is calculating the moving average, so it takes the previous moving average, and then takes the new value, and uses those two items plus the time interval to calculate a new moving avg.

    Anyway, instead of recalculating the spreadsheet through a macro, I thought a user function would be better.

    I started with this:
    Function dmvavg(interval As Single, num As Single) As Double
    Dim spec As Single
    spec = dmvavg
    spec = (num * (1 / interval)) + (spec * (1 - 1 / interval))
    dmvavg = (num * (1 / interval)) + (spec * (1 - 1 / interval))
    End Function


    Thanks for any help.

  2. #2
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Recursive/Running Function

    hi,

    Quote Originally Posted by goldm View Post
    Hi,
    short question: Is there a way to pass the current value of a cell into a user function, run some calculations, and then change that same cell (without using activecell)?
    ...
    If the function is a UDF for use within the spreadsheet, asfaik it is unlikely to allow changes to a cell (other than the one that is calling the function). However, you could wrap your existing function within a Sub which allows for changes to be made to other cells, see the below link for an example which you may be able to adapt:
    http://www.mcgimpsey.com/excel/accumulator.html
    found via:
    http://www.google.co.uk/search?hl=en...+cell%22&meta=

    On the odd chance that you can make your Function change other cell values (untested)...
    The below code allows for the use of a range (ie the cell in question) to be incorporated into your function (untested). I'm not sure but may be better if this was done "byval" (see Google for discussions abput this)...

    Please Login or Register  to view this content.
    hth
    Rob
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

  3. #3
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983

    Re: Recursive/Running Function

    goldm

    Welcome to Exceltip forum

    Please take a couple of minutes and read ALL theForum Rules then wrap your VBA code (Rule 3)
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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