Results 1 to 4 of 4

Thread: Fill Blanks With Averages

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    2
    Rep Power
    0

    Fill Blanks With Averages

    Hi

    I use Excel rarely and almost exclusively to generate data which is used to create Corel Draw images.
    Searching on another site I found the following macro (written by Rick Rothstein) which seems to be close to what I am after. I think I understand everything except the important bit within the loop, which is not enough to be able to alter it to my needs, and wonder if someone would give an explanation of the line within the loop.


    Code:
    Sub FillBlanksWithAverages()
      Dim LastRow As Long, Ar As Range
      LastRow = Cells(Rows.Count, "G").End(xlUp).Row
      For Each Ar In Range("G2:G" & LastRow).SpecialCells(xlBlanks).Areas
        Ar.Value = (Ar(1).Offset(-1) + Ar(Ar.Count).Offset(1)) / 2
      Next
    End Sub

    To try and explain what Im trying to achieve Im using an example of a grey scale image where each row generates a single integer between 0 and 255 representing a shade of grey but the intention is to triplicate the method to represent RGB.

    In a spreadsheet with 2 columns 'A' & 'B': In Column A, I want to enter a value of between 0 and 255 in at least the top and bottom rows but may also enter values in any number of rows between them – in this example imagine we have entered the following four values –

    10 in A1 (top row)
    150 in A100,
    75 in A900 and
    20 in A1000 (bottom row).

    In Column B I would like Excel to fill the intermediary values so that in this example they would gradually step up in value between B1 where it would show 10, to B100 where it would show 150 and then decreasing in value from B100 to B900 where it shows 75, and then decreasing from 75 to 20 between B900 to B1000.

    I think the logic would go something like;

    If this row has a value in Column A, use that value in Column B, otherwise

    To calculate the value for column B in this row;

    Use the A column value and row number of the first row ABOVE which has a value in column A and the A column value and row number of the first row BELOW which has a value in column A.

    Take the value of ABOVE from the value of BELOW and divide by the number of rows between them, multiply that result by the number of rows between the current row and the row ABOVE, and then add the value in ABOVE.

    I have already asked this question at Colour Gradients - Lookup next non-empty cell
    Last edited by nicksoph; 08-13-2014 at 08:47 AM.

  2. #2
    Senior Member
    Join Date
    Jun 2012
    Posts
    337
    Rep Power
    13
    Since this is an Excel Forum I'd prefer a sample Excel file.

  3. #3
    Member p45cal's Avatar
    Join Date
    Oct 2013
    Posts
    94
    Rep Power
    12
    See if this does anything for you:
    Code:
    Sub blah()
    lr = Cells(Rows.Count, "A").End(xlUp).Row
    Range("B1:B" & lr).Value = Range("A1:A" & lr).Value
    For Each are In Range("B1:B" & lr).SpecialCells(xlCellTypeBlanks).Areas
      With are.Offset(-1).Resize(are.Rows.Count + 2)
        .DataSeries Rowcol:=xlColumns, Type:=xlLinear, Trend:=True
      End With
      For Each cll In are.Cells
        cll.Value = Round(cll.Value)
      Next cll
    Next are
    End Sub

  4. #4
    Junior Member
    Join Date
    Aug 2014
    Posts
    2
    Rep Power
    0

    Perfect

    Hi P45cal

    That worked perfectly, I really appreciate your helping.

    It looks very elegant and introduces several new bits of Excel which I will attempt to understand.

    Many thanks,

    Nick

Similar Threads

  1. Replies: 9
    Last Post: 02-11-2014, 07:42 AM
  2. fill in cells according to selected optionbutton
    By paul_pearson in forum Excel Help
    Replies: 6
    Last Post: 09-13-2013, 10:26 AM
  3. Macro to clear data based on color fill
    By Howardc in forum Excel Help
    Replies: 7
    Last Post: 12-03-2012, 09:25 AM
  4. Unmerge Cells and Fill with Duplicate Values
    By princ_wns in forum Excel Help
    Replies: 3
    Last Post: 10-09-2012, 07:36 AM
  5. Replies: 4
    Last Post: 07-27-2012, 08:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •