Fill non-contiguous blank cells with the value from the cell above the first blank

。_饼干妹妹 提交于 2019-11-26 11:34:15

问题


I have a column like the following:

1 red
2 blue
3 red
4 
5 blue
6
7
8 white

The blanks refer to the record above it. So #4 would be associated with red and 6 and 7 would be blue.

Is there an easy way to fill in the blanks for entire column?


回答1:


  • Select A1:A8.
  • Press F5 to show the Goto dialog.
  • Click Special ..... Select Blanks and click OK.

That will select a noncontiguous range of blank cells.

  • Then, without selecting anything else, type =A3 and press control+enter.
  • That will enter an array formula in all the blank cells referring to the cell above it.
  • Reselect A1:A8, and Edit - Copy.
  • Then Edit - Paste Special - Values. And you're all set.

Note that the =A3 refers to the cell above the first blank cell.

If you want to do it with a macro, you could loop through the cells and fill in the empty ones.

Public Sub FillBlanks()

    Dim rColumn As Range
    Dim rCell As Range

    If TypeName(Selection) = "Range" Then
        For Each rColumn In Selection.Columns
            For Each rCell In rColumn.Cells
                If rCell.Row > rColumn.Cells(1).Row Then
                    If IsEmpty(rCell.Value) Then
                        rCell.Value = rCell.Offset(-1).Value
                    End If
                End If
            Next rCell
        Next rColumn
    End If
End Sub


来源:https://stackoverflow.com/questions/3762340/fill-non-contiguous-blank-cells-with-the-value-from-the-cell-above-the-first-bla

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!