+ Reply to Thread
Results 1 to 3 of 3

AVERAGE formula for dynamic range

  1. #1
    Forum Contributor
    Join Date
    05-26-2004
    Location
    Halifax, UK
    MS-Off Ver
    Office 2016
    Posts
    260

    AVERAGE formula for dynamic range

    Bit of an odd post this because my formulas actually seem to work, I just don't understand how one part can work and just wondering if anyone knows why.

    I was wanting to create a formula that locates that last used (non-blank) cell in column in order to create a dynamic AVERAGE formula. In order to find the last non-blank cell and return the row number I used this array formula:

    =SUMPRODUCT(MAX((A:A<>"")*ROW(A:A)))

    This returns the row number of the last non-blank in column A, and seems to work fine. After a bit of Googling I found the following for the dynamic AVERAGE formula:

    =AVERAGE(A1:INDEX(A:A,B1))

    Where B1 is the location of the SUMPRODUCT formula from before (row no. of last used row). Now this also seems to give the correct answer (correct average from the numbers I've tried) but I actually don't understand how this can work at all and not produce an error? My understanding of the AVERAGE function is that the syntax needs to be either a RANGE from which to take the numbers or number themselves. However in this example it starts with a range (A1) but the second part of the reference (INDEX(A:A,B1) just returns the contents of the last non-blank cell. So in my spreadsheet the last blank cell is A20 and has the number 500 in it, so as far as I know the formula evaluates as:

    =AVERAGE(A1:500)

    Which makes no sense and if I type that in I just get an error. Yet somehow in formula format it gives the correct average for the dynamic range. I'm curious to know how Excel comes back with the correct answer and also a bit reluctant to use the formula even though it seems to work without understanding why.

    Thanks in advance for any insights! :-)

  2. #2
    Forum Guru
    Join Date
    08-28-2014
    Location
    USA
    MS-Off Ver
    Excel 2019
    Posts
    17,569

    Re: AVERAGE formula for dynamic range

    If you select the cell containing the AVERAGE based formula and utilize the Evaluate Formula feature you'll see that INDEX returns the last non blank cell and not the value that is therein.
    Also the AVERAGE function documentation states that "If a range or cell reference argument contains text, logical values, or empty cells, those values are ignored; however, cells with the value zero are included".
    So, providing there are blank cells in column A, =AVERAGE(A:A) should return the same value as =AVERAGE(A1:INDEX(A:A,B1))
    Let us know if you have any questions.
    Consider taking the time to add to the reputation of everybody that has taken the time to respond to your query.

  3. #3
    Forum Expert XLent's Avatar
    Join Date
    10-13-2010
    Location
    Northumberland, UK
    MS-Off Ver
    various
    Posts
    2,704

    Re: AVERAGE formula for dynamic range

    INDEX is very versatile -- in above context range:INDEX(A:A,B1) will result in A500 being used as a range refernece.

    sort of related thread can be found here
    Last edited by XLent; 03-08-2019 at 10:49 AM. Reason: added underline to hyper

+ 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. [SOLVED] average formula for dynamic range
    By rossg in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-01-2014, 07:41 AM
  2. Dynamic Range and Average Formula
    By molfetta55 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-25-2014, 09:54 AM
  3. Dynamic Range average
    By rjhe22 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 01-12-2013, 03:42 PM
  4. Average over a dynamic range of values in a different worksheet
    By MummyB in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-30-2012, 10:41 AM
  5. [SOLVED] Average of last 3 cells in a range, dynamic
    By Armitage2k in forum Excel General
    Replies: 11
    Last Post: 03-25-2012, 02:48 PM
  6. Excel 2007 : Dynamic range/average issue?
    By gannon_w in forum Excel General
    Replies: 5
    Last Post: 02-26-2012, 10:42 PM
  7. Dynamic range in average formula
    By Cicada in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-09-2011, 02:08 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