+ Reply to Thread
Results 1 to 9 of 9

How to average every n elements in an array

  1. #1
    Registered User
    Join Date
    01-12-2015
    Location
    PL
    MS-Off Ver
    2016
    Posts
    29

    How to average every n elements in an array

    Hi,
    I have an array consisting of values from a named range. What I would like to do is to calculate an average value every n elements and save it into a new array.
    Can you please advise on how to do that?
    The original array is created by the code given below.
    Please Login or Register  to view this content.
    Thank you in advance
    Regards

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

    Re: How to average every n elements in an array

    Do you want an average of every nth row, e.g. AVERAGE(B3, B6, B9, B12, ...

    Or you you want to break the range into blocks, each of which is N rows, and average each of those blocks AVERAGE(B3:B6), AVERAGE(B7:B10), ....
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  3. #3
    Registered User
    Join Date
    01-12-2015
    Location
    PL
    MS-Off Ver
    2016
    Posts
    29

    Re: How to average every n elements in an array

    Hi,
    The second approach. I want to take n rows in an array, average them, then take another n rows, average them and so on.

    Regards

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

    Re: How to average every n elements in an array

    Here is a formula based approach that employs three helper columns which may be moved and/or hidden for aesthetic purposes:
    1. The first helper column (F) is populated using: =IF(MOD(ROWS(A$1:A1),C$1)=1,1,"")
    2. The second helper column (G) is populated using: =COUNTIFS(F$2:F2,1)
    3. The third helper column (H) is populated using: =AVERAGEIFS(A$2:A$13,G$2:G$13,G2)
    4. Cell C1 is manually populated with n
    5. D2:D13 are populated using: =IF(ROWS(A$1:A1)>COUNT(A$2:A$13)/C$1,"",ROWS(A$1:A1))
    6. E2:E13 are populated using: =IF(D2="","",INDEX(H$2:H$13,MATCH(D2,G$2:G$13,0)))
    Note that in the attached file columns F:H are hidden by choosing font color white
    Let us know if you have any questions.
    Attached Files Attached Files
    Consider taking the time to add to the reputation of everybody that has taken the time to respond to your query.

  5. #5
    Registered User
    Join Date
    01-12-2015
    Location
    PL
    MS-Off Ver
    2016
    Posts
    29

    Re: How to average every n elements in an array

    @ JeteMC,
    Thank you, but the "worksheet formula" approach is not what I would like to use. At present, in order to calculate the average values, and thus reduce the data points, I use the following code:
    Please Login or Register  to view this content.
    It is based on the nested formula using Average and Offset functions, in accordance to this web page. However this approach makes everything work slowly, that is why I want to use an array and reduce the data points in flight, without writing formulas directly into the worksheet cells.
    Can you advise on how to do that?

    Regards

  6. #6
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: How to average every n elements in an array

    Can you post an example file so we have something to work with.

    On how to, follow the instructions in the yellow banner at the top of the page
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  7. #7
    Registered User
    Join Date
    01-12-2015
    Location
    PL
    MS-Off Ver
    2016
    Posts
    29

    Re: How to average every n elements in an array

    Hi, sorry the late reply.
    I've slightly altered my approach. Instead of trying to average every n rows in an array, I use Application.Average function with the "FOR" loop and write the results to an array, and subsequently i print that array in the predefined range - see the code below. This way the whole tool, I was working on is about 2-3 times faster, which for me is more than satisfying. Thank you everyone for your help.
    Please Login or Register  to view this content.

  8. #8
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: How to average every n elements in an array

    This is what I kind of already had prepared but was waiting for an example file to do final tests.

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

  9. #9
    Registered User
    Join Date
    01-12-2015
    Location
    PL
    MS-Off Ver
    2016
    Posts
    29

    Re: How to average every n elements in an array

    @bakerman2, It was not my intention to make you feel bad
    I will hold off with marking this thread as solved until next week. Tomorrow I will present the tool the people who ordered it, so chances are I will have to rework some of my ideas.

    Cheers

+ 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. find array length got using split feature and valdate array elements
    By malathi1972 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-21-2018, 04:51 AM
  2. [SOLVED] Excel IsNumber Function treats array elements differently from range elements
    By Alan Beban in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 01-31-2018, 02:22 PM
  3. Array Storage Problem: Array Elements Deleted on 'End' Command
    By AidenS in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-15-2014, 10:38 AM
  4. [SOLVED] Removing specific elements from an Array based on matching elements in a different Array
    By jamesdeuce in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-24-2014, 10:35 PM
  5. help assigning elements of dictionary to array and then pasting array into worksheet
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 12-05-2013, 10:37 AM
  6. [SOLVED] Populate Listbox with all rows of a dynamic array where elements of a single array match.
    By Tayque_J_Holmes in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 08-07-2012, 04:54 AM
  7. Average of n elements
    By Lattaio in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 04-13-2010, 09:12 AM

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