Search code examples
excelexcel-formulaflood-fillvba

Excel Fill blank rows with values from above


Looking to fill blank values in a row with the 'top' value, similar to the functionality provided by Editing>Fill>Top. The difference being that the Fill function requires you to go row-by-row rather than applying itself to a larger dataset. Example below:

Apple   1   foo
Banana  1   foo
        2   foo
            bar
Cherry  2   bar
        3   foo
        6   bar
Grape   1   foo

Would end up as the following:

Apple   1   foo
Banana  1   foo
Banana* 2   foo
Banana* 2*  bar
Cherry  2   bar
Cherry* 3   foo
Cherry* 6   bar
Grape   1   foo       //new values represented with *

Note that the third entry in the second column (2) was also brought down implying this could be applied to multiple columns.

The value in this would be converting a table that has the relationships Cherry>2, Cherry>3, Cherry>6 represented in the first table into a format that could be used as an associative table.


Solution

  • Try this short macro:

    Sub FillInTheBlanks()
        Dim i As Long, N As Long, j As Long
    
        N = Cells(Rows.Count, "C").End(xlUp).Row
    
        For i = 2 To N
            For j = 1 To 2
                If Cells(i, j).Value = "" Then Cells(i, j).Value = Cells(i, j).Offset(-1, 0).Value
            Next j
        Next i
    End Sub