+ Reply to Thread
Results 1 to 3 of 3

VBA: Calculate average based on data from previous column, variable length

  1. #1
    Registered User
    Join Date
    02-13-2012
    Location
    NYC
    MS-Off Ver
    Excel 2003
    Posts
    39

    VBA: Calculate average based on data from previous column, variable length

    Hello,

    In column C I try to calculate an average from data in column B and the number of cells in column B to be calculated is defined by a variable Time.
    To clarify, Time is a variable number that will indicate the first number of rows to be calculated. Column B will not start at row 2 but rather at a unspecified row (approx between say row 20 and 30).
    Example:
    Time=5
    column B: row 21 as first row with a number
    in column C: looking in row 25 for average of 5 first values of column B (row 21 to 25)

    This value is calculated only once (so no rolling value). I'm looking for a generic formula.
    Thanks for your help.

  2. #2
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Re: VBA: Calculate average based on data from previous column, variable length

    Does the result in C always maintain the current row as the bottom of the range in B to be averaged and extend up by n rows, where n = time.Value (5)?

    So if I was in Cell C30 and Time = 10, you want the average of B20:B30?
    Perhaps it was the Noid who should have avoided me...
    If you are satisfied with my solution click the small star icon on the left. Thanks
    1. Make a copy of your workbook and run the following code on your copy (just in case)
    2. With excel open, press ALT+F11 to open the Visual Basic Editor (VBE). From the "Insert" menu, select "Module".
    3. Paste the code from above into the empty white space. Close the VBE.
    4. From the developer tab, choose "Macros", select the Sub Name, and click "Run".

  3. #3
    Registered User
    Join Date
    02-13-2012
    Location
    NYC
    MS-Off Ver
    Excel 2003
    Posts
    39

    Re: VBA: Calculate average based on data from previous column, variable length

    The current row will not be used, because the cursor will be in another location. (Do I understand your question correctly?)
    I will execute VBA, that will perform a number of operations on various sheets. One of these operations is this calculation.
    This calculation is only done for this particular cell, the first cell of column C in this example. The location of this cell (row #) will be the value 'n' of Time (here 5) lower than where the previous column (B) starts.
    All cells above will be empty, except for row 1 that has the titles.
    Thanks for your help.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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