+ Reply to Thread
Results 1 to 5 of 5

Auto Average Cells on Either Side of Blank in Column

  1. #1
    Registered User
    Join Date
    03-31-2018
    Location
    Bellingham, WA
    MS-Off Ver
    2016
    Posts
    3

    Auto Average Cells on Either Side of Blank in Column

    So this is the first ever post I have made to a forum. I am a total beginner to VBA. This is real simple I am sure. Not sure why I am getting an error, seems rather straightforward to me! I left my first attempt at average in there although at this point I am assuming that doesn't work for seem reason as current attempt. Also, is there a VBA function for average or we have to use the application average? Thanks

    Sub Macro2()
    ' Macro2 Macro
    Dim A As Long
    A=100
    For i = 1 To A
    If Not (IsNumeric(Cells(i, 3).Value)) Then
    Cells(i, 3).Value = ((Cells(i + 1, 3) + Cells(i - 1, 3))) / 2
    'Cells(i, 3).Value = Application.Average(Range(Cells(i+1,3),Cells(i-1,3))
    End if
    Next i
    End Sub

  2. #2
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,151

    Re: Auto Average Cells on Either Side of Blank in Column

    You're trying to do mathematical operations for numbers on non-numeric values:
    Please Login or Register  to view this content.
    This "Not" is unnecessary because it changes the entire meaning of the "If" statement.

    And if "i" = 1 then "i - 1" = 0, there is no row "0".
    Last edited by mjr veverka; 03-31-2018 at 07:21 PM.

  3. #3
    Registered User
    Join Date
    03-31-2018
    Location
    Bellingham, WA
    MS-Off Ver
    2016
    Posts
    3

    Re: Auto Average Cells on Either Side of Blank in Column

    Ok, thanks, actually the issue is a really obvious one, "i" can't equal 1 because i - 1 would be off the sheet, so it has to start above 1. Sorry, will delete this post if I can,
    Last edited by jmjohnston; 03-31-2018 at 07:24 PM.

  4. #4
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,151

    Re: Auto Average Cells on Either Side of Blank in Column

    1. "IsNumeric" returns "True" if the entire expression is recognized as a number, otherwise, it returns "False".

    If Not (IsNumeric(Cells(i, 3).Value)) Then = If Not True Then, where "IsNumeric(Cells(i, 3).Value)" is a Boolean.

    The condition is performed only if the values are non-numeric.
    "Not" in this construction is unnecessary.

    "For i = 1 to 100 ... Cells(i - 1, 3).Value / 2" => Cells(1 - 1, 3).Value / 2 => Cells(0, 3).Value / 2 => Error, because there is no "0" row.

  5. #5
    Registered User
    Join Date
    03-31-2018
    Location
    Bellingham, WA
    MS-Off Ver
    2016
    Posts
    3

    Re: Auto Average Cells on Either Side of Blank in Column

    Yes, I see that it is a True or False Boolean. Turned out the issue was the zero row problem I neglected to see. Sometimes the obvious slips the mind. Thanks for the help!

+ 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. Auto fill macro when adjacent column has blank cells
    By ElsiePOA in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-30-2014, 09:49 PM
  2. How to Get the Average of First 5 Non-Blank Cells in a Column?
    By southerk in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 03-24-2014, 10:01 PM
  3. Replies: 14
    Last Post: 03-24-2014, 08:09 PM
  4. Replies: 4
    Last Post: 11-20-2013, 10:16 PM
  5. Average the last 10 cells of a dynamic column but ignore blank cells & 0
    By mattadler22 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-13-2013, 11:37 PM
  6. [SOLVED] find blank cells in column and average the cells below
    By desibabuji in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-03-2013, 12:03 PM
  7. Average of next 6 non blank cells in a column.
    By hackboy in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 11-15-2012, 10:44 AM

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