+ Reply to Thread
Results 1 to 7 of 7

Function with "ActiveCell.Offset(x, y).value" not calculating cells automatically?

  1. #1
    Registered User
    Join Date
    03-26-2009
    Location
    New Jersey
    MS-Off Ver
    Excel 2007
    Posts
    26

    Function with "ActiveCell.Offset(x, y).value" not calculating cells automatically?

    I am attempting to write VBA functions like this one, which uses ActiveCell.Offset:
    Please Login or Register  to view this content.
    However cell values for cells that use this formula don't appear to calculate automatically. In order to get correct values, I have to go down the list of cells in order, entering each cell and hitting the Enter key.

    Why is this, and how can I calculate these cells automatically? Note that if there is something else that performs the same task as ActiveCell.Offset() here, I am fine with using that instead.

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Function with "ActiveCell.Offset(x, y).value" not calculating cells automatically

    You should be using Application.Caller rather than ActiveCell.

    If val1, val2 & valOffset are variables assigned to cells you should pass them to the UDF as Ranges rather than Integers.

    edit: below is not accurate... in my defence it's late
    If declared as Ranges they will become precedents of the UDF and as a result when altered they will cause the UDF to recalculate negating need for UDF to be made volatile.

    Please Login or Register  to view this content.
    edit: below incorrect - covered by romperstompers post below re: indirect reference by virtue of Offset (which if changed would not cause recalc)
    If the variables are constants and can not be tied to Ranges you will need to make the UDF volatile so as to minimise risk of calculation error - this brings with it additional overhead.

    Of course whether or not you need the UDF in the first instance is open to debate.
    Last edited by DonkeyOte; 01-19-2011 at 07:25 PM.

  3. #3
    Registered User
    Join Date
    03-26-2009
    Location
    New Jersey
    MS-Off Ver
    Excel 2007
    Posts
    26

    Re: Function with "ActiveCell.Offset(x, y).value" not calculating cells automatically

    Thank you Using Ranges is fine as I don't need to pass constants.

    Do all UDFs that are volatile need to have each cell calculated individually by hitting Enter? Or was I having that issue for some other reason?

  4. #4
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Function with "ActiveCell.Offset(x, y).value" not calculating cells automatically

    No. Volatile UDFs recalculate whenever any calculation takes place, even if it does not affect them, which is why they should be avoided if possible. You need to make your UDF volatile for it to work properly because you effectively have an indirect reference to another cell.
    Remember what the dormouse said
    Feed your head

  5. #5
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Function with "ActiveCell.Offset(x, y).value" not calculating cells automatically

    Thanks R.

    @skywalker, a lot of what I said re: Volatility should be disregarded in the context of your UDF - I've amended my post to that effect (ie added comments where talking nonsense)

  6. #6
    Registered User
    Join Date
    03-26-2009
    Location
    New Jersey
    MS-Off Ver
    Excel 2007
    Posts
    26

    Re: Function with "ActiveCell.Offset(x, y).value" not calculating cells automatically

    Quote Originally Posted by romperstomper View Post
    No. Volatile UDFs recalculate whenever any calculation takes place, even if it does not affect them, which is why they should be avoided if possible. You need to make your UDF volatile for it to work properly because you effectively have an indirect reference to another cell.
    Thank you. Then do you know why I am having the issue where, for my original function, I have to enter the formula bar and hit Enter individually for each cell in order to update the cell values?

  7. #7
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Function with "ActiveCell.Offset(x, y).value" not calculating cells automatically

    Yes - as I said, you need to make your function volatile. You can do this by adding:
    Please Login or Register  to view this content.
    as the first line inside the function code.

+ 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