+ Reply to Thread
Results 1 to 12 of 12

Moving average excluding blank cells

  1. #1
    Registered User
    Join Date
    08-15-2007
    Posts
    8

    Moving average excluding blank cells

    HI, i am new to the site and was hoping for some help.

    I have a column of data that contains various blank cells where no data was measured. In the adjacent column I want to take the moving average of the last 4 data points including the most recent entry. My problem is i do not know how to handle blank cells where there was no data. I need it to average the last four in the column where data acutally exists. I am ok with using helper cells if needed and I am not worried about the first four results at this time.

    Below is what i would like to see


    Example data

    A..................B
    15
    50
    25
    20................55
    Blank............55
    30................31.25
    35................27.5
    blank............27.5
    blank............27.5
    15................25
    10................22.5
    15................18.75
    40................20
    blank.............20

    Thanks for any help.

  2. #2
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967
    Try

    Please Login or Register  to view this content.
    in B4 assuming your data is in column A
    Martin

  3. #3
    Registered User
    Join Date
    08-15-2007
    Posts
    8
    Thanks for the response. That simple solution would work on the first four values because there are no blanks, but as you go down the the column there me be a blank in the range. The problem is i always need the average to include the last four legitimate data points. So if there is two blanks it will need to go up six places to get four real values, five spots if one blank etc..

  4. #4
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967
    Try this user defined function

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    08-15-2007
    Posts
    8
    Quote Originally Posted by mrice
    Try this user defined function

    Please Login or Register  to view this content.


    Thanks for the help. This almost works except it counts down the sheet instead of taking the four "previous values". I tried modifying where I start the reference cell but didn't work. I am not very good with VB code, is there a way to modify this to make the range take the four previous values instead of starting with the reference and counting down the column?

    Thanks again for any help.

  6. #6
    Registered User
    Join Date
    08-15-2007
    Posts
    8
    I tried this modificationand it seems to work. Thanks so much for the help! i am sure i will have many more questions to post on this site.


    Function MoveAve(StartCell As Range)
    Counter = 0
    Do While ValidCounter < 4
    MySum = MySum + StartCell.Offset(-1 * Counter, 0)
    If StartCell.Offset(-1 * Counter, 0) <> "" Then ValidCounter = ValidCounter + 1
    Counter = Counter + 1
    Loop
    MoveAve = MySum / 4
    End Function

  7. #7
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967
    Slight variation needed.

    Please Login or Register  to view this content.

  8. #8
    Forum Contributor
    Join Date
    01-05-2004
    Location
    Helsinki, Finland
    Posts
    100
    Hi,

    Assuming, that you data starts from A2, put this array-formula in B2:

    =SUM(INDIRECT("A"&LARGE(($A$2:A2<>"")*ROW($A$2:A2),4)&":A"&ROW()))/4

    and copy it down for as long as needed. Because it is an array formula, you should enter it by pressing Ctrl+Shift+Enter. For the first three cell it gives an error, but after fourth value it should work just fine.

    - Asser

  9. #9
    Registered User
    Join Date
    08-15-2007
    Posts
    8
    Both of the above worked. Thanks Mrice and jazzer

    Jazzer, for learning purposes can you explain that formula out for me?

    Thanks

  10. #10
    Forum Contributor
    Join Date
    01-05-2004
    Location
    Helsinki, Finland
    Posts
    100
    Hi,

    first of all, if array formula is a new thing for you, you should read this article: http://www.cpearson.com/excel/ArrayFormulas.aspx. It explains what array formulas are and how they work.

    Secondly, you should use Excels "Evaluate Formula"- function (from Formula Auditing toolbar) to study, how this array formula (or any formula) works.

    Let's take a look at the cell B11 (besides 11 with value 25)

    =SUM(INDIRECT("A"&LARGE(($A$2:A11<>"")*ROW($A$2:A11),4)&":A"&ROW()))/4

    ($A$2:A11<>"") part returns an array about, if the cells in the area A2:A11 are not empty (<>"") or if they are: {TRUE, TRUE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE, FALSE, TRUE}

    ROW($A$2:A11) part returns an array of row numbers in are A2:A11: {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}

    You should know, that boolean values TRUE and FALSE represent integer values 1 and 0 in Excel. So when you multiply for example values TRUE*2 = 1*2 = 2 and FALSE*6 = 0*6 = 0. So After you multiply those two arrays, you'll get: {2, 3, 4, 5, 0, 7, 8, 0, 0, 11}

    So now we know which rows (in A2:A11) has a value and which doesn't. Next we need to find out the fourth largest row that has an value. LARGE({2, 3, 4, 5, 0, 7, 8, 0, 0, 11},4) will give that and it is row 5.

    Then we make a cell reference that includes the last four values (including blanks if there are any): INDIRECT("A"&5&":A"&ROW()) -> INDIRECT("A5:A11")

    And then we SUM this: SUM(A5:A11) = SUM({20, 0, 30, 35, 0, 0, 15}) = 100. And finally devide it by 4 and get 100/4 = 25.

    I hope this helps you understand the formula better.

    - Asser
    Last edited by Jazzer; 08-17-2007 at 01:34 AM.

  11. #11
    Registered User
    Join Date
    08-15-2007
    Posts
    8
    Thanks so much for the explanation and help. It is greatly appreciated.

  12. #12
    Registered User
    Join Date
    08-28-2011
    Location
    Silver City, NM
    MS-Off Ver
    Excel 2007
    Posts
    1

    Question Re: Moving average ecluding blank cells

    This was of great help. Thanks to all.

    In addition to a non-blank moving average, I would like to compute an average of the last 10 of my first-of-the-day, i.e.fasting, blood sugar readings. Currently I signify this by drawing a bottom border under the last reading of the previous day. Something like this:


    8/23/2011 5:15 AM 69
    5:00 PM 139
    8/24/2011 5:30 AM 86
    5:00 PM 199
    8/25/2011 6:00 AM 70
    5:00 PM 224
    8/26/2011 5:15 AM 102
    4:45 PM 179
    8/27/2011 5:30 AM 66

    Well, I couldn't replicate the underlining, but I hope you get the idea.

    Is there any way to incorporate this kind of formatting into the previous Visual Basic formula? Or is there some creative way I could use the time (second column) as a parameter to control which cells to count?

    Thanks in advance for any help.
    4:45 PM 144

+ 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