Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
714 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  • 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 +.
  • 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...