Fill Down until Last Empty Row or Next Filled Cell

15.4k views Asked by At

I know how to code in order to fill down a column, but I have a few conditions that I can't find out how to implement.

I want to fill down until the last row (that contains any value at all) or the next cell within the column that contains information.

The data looks like this

a        1  2  3  4  5  6  7  8  9  10
         1  2  3  4  5  6  7  8  9  10
         1  2  3  4  5  6  7  8  9  10
         1  2  3  4  5  6  7  8  9  10
         1  2  3  4  5  6  7  8  9  10
b        1  2  3  4  5  6  7  8  9  10
         1  2  3  4  5  6  7  8  9  10
c        1  2  3  4  5  6  7  8  9  10
d        1  2  3  4  5  6  7  8  9  10
         1  2  3  4  5  6  7  8  9  10
         1  2  3  4  5  6  7  8  9  10
e        1  2  3  4  5  6  7  8  9  10
         1  2  3  4  5  6  7  8  9  10
         1  2  3  4  5  6  7  8  9  10
         1  2  3  4  5  6  7  8  9  10
         1  2  3  4  5  6  7  8  9  10

So as you can see, the code needs to recognize how to stop at b (and not copy over it) when copying down the column. Also the code needs to stop at the last row with values in it when dragging down e.

I've been trying to figure it out to no avail, please help!!!

Previous code:

Yes I do have some code, but it is slow and I would like to figure out something more efficient.
'Sub CopyDown()
    Sheets("RAW").Range("A1").Select
    For i = 1 To 100
        ActiveCell.Copy
        ActiveCell.Offset(1, 0).Select
        If ActiveCell.Value = vbNullString Then
            ActiveCell.Paste
        End If
    Next i
End Sub'
2

There are 2 answers

5
tbur On BEST ANSWER

This one is simple, if your example dataset is used (filling down the existing values to the blanks in Column A.)

    Sub MacroFillAreas()

    For Each area In Columns("A:A").SpecialCells(xlCellTypeBlanks)
        If area.Cells.Row <= ActiveSheet.UsedRange.Rows.Count Then
            area.Cells = Range(area.Address).Offset(-1, 0).Value
        End If
    Next area

(Code Modified)

   End Sub
3
Holmes IV On

Without Code its hard to say, but assuming you are doing a loop, then all you need to do is check the cell

Sub filldown()

Dim X, Y As Long
Dim MaxX, MaxY As Long

MaxX = ActiveSheet.UsedRange.Rows.Count
MaxY = ActiveSheet.UsedRange.Columns.Count

For X = 1 To MaxX
    For Y = 1 To MaxY
        If IsEmpty(ActiveSheet.Cells(X, Y)) = True Then
            ''Do something
        End If
    Next
Next

End Sub