Home > Data > Data Entry > Fill Blanks

Fill Blank Cells in Excel - Macros

Debra Dalgleish - Contextures

Use these macros to quickly fill blank cells in Excel, with the value from above. After the cells are filled in, you can sort and filter the data, without problems. Get the Excel file with the macros, and see the written steps

fill the blank cells in a column with value from above

Fill Blank Cells From Above

In the report shown below, some cells were left blank, so the report headings and subheadings are easier to read.

However, those missing headings cause problems if you try to filter or sort the data.

fill the blank cells in a column with value from above

Video: Fill Blank Cells From Above

In this video, I show how to fill blank cells without macros. The macros on this page do something similar, but much faster!

Fill Blank Cells - Macros

If you have to fill blank cells in lots of weekly reports, you can use a macro, to make the job much easier, and faster.

There are four Fill Blank Cells macros shown below.

  • Each macro fills blank cells in the active column.
  • Each macro uses a different method to find the last row, and to fill blank cells programmatically

-- Macro 1 - Dave Peterson

-- Macro 2 - Rick Rothstein

-- Macro 3 - Dave Peterson / Ron de Bruin

-- Macro 4 - Alex Blakenburg

Fill Blank Cells - Macro 1

In this fill blank cells macro, from Dave Peterson:

  • uses a formula to fill the cells
  • pastes the results as values
  • uses the .SpecialCells(xlCellTypeLastCell) method to find the last row
  • fills blanks from row 2 to the last row

You can copy the code below, and paste it into a regular code module, in your own Excel file. Or, download the sample Excel file, and copy the code from there.

Sub FillColBlanks()
'by Dave Peterson  2004-01-06
'fill blank cells in column with value above
'https://www.contextures.com/excelfillblanksmacros.html
Dim wks As Worksheet
Dim rng As Range
Dim LastRow As Long
Dim col As Long

Set wks = ActiveSheet
With wks
   col = activecell.column
   'or
   'col = .range("b1").column

   Set rng = .UsedRange  'try to reset the lastcell
   LastRow = .Cells.SpecialCells(xlCellTypeLastCell).Row
   Set rng = Nothing
   On Error Resume Next
   Set rng = .Range(.Cells(2, col), .Cells(LastRow, col)) _
                  .Cells.SpecialCells(xlCellTypeBlanks)
   On Error GoTo 0

   If rng Is Nothing Then
       MsgBox "No blanks found"
       Exit Sub
   Else
       rng.FormulaR1C1 = "=R[-1]C"
   End If

   'replace formulas with values
   With .Cells(1, col).EntireColumn
       .Value = .Value
   End With

End With

End Sub       

Fill Blank Cells - Macro 2

This fill blank cells macro, from Rick Rothstein

  • uses the .Find method to calculate the last row
  • Each cell gets its value from the cell above the first cell of the Area that it's in, using the Offset property

You can copy the code below, and paste it into your own Excel file. Or, download the sample Excel file, and copy the code from there.

Sub FillColBlanks_Offset()
'by Rick Rothstein  2009-10-24
'fill blank cells in column with value above
'https://www.contextures.com/excelfillblanksmacros.html

  Dim Area As Range, LastRow As Long
  On Error Resume Next
  LastRow = Cells.Find(What:="*", SearchOrder:=xlRows, _
               SearchDirection:=xlPrevious, _
               LookIn:=xlFormulas).Row
  For Each Area In ActiveCell.EntireColumn(1).Resize(LastRow). _
               SpecialCells(xlCellTypeBlanks).Areas
    Area.Value = Area(1).Offset(-1).Value
  Next
End Sub

Fill Blank Cells - Macro 3

This example combines Dave Peterson's code (Example 1), with the special cells test from Ron de Bruin.

  • In Excel 2007, and earlier versions, there is a problem with special cells if there are more than 8192 different areas in the special cells range.
    • NOTE: This problem was fixed in Excel 2010.
  • This code tries to count the areas, and if over the limit, it loops through the range in groups of 8000 rows

You can copy the code below, and paste it into your own Excel file. Or, download the sample Excel file, and copy the code from there.

Sub FillColBlanksSpecial()
'https://www.contextures.com/excelfillblanksmacros.html
'by Dave Peterson  2004-01-06
'fill blank cells in column with value above

'2010-10-12 incorporated Ron de Bruin's test 
'  for special cells limit
'https://jkp-ads.com/rdb/win/s4/win003.htm

Dim wks As Worksheet
Dim rng As Range
Dim rng2 As Range
Dim LastRow As Long
Dim col As Long
Dim lRows As Long
Dim lLimit As Long

Dim lCount As Long
On Error Resume Next
 
lRows = 2 'starting row
lLimit = 8000

Set wks = ActiveSheet
With wks
   col = ActiveCell.Column

   Set rng = .UsedRange  'try to reset the lastcell
   LastRow = .Cells.SpecialCells(xlCellTypeLastCell).Row
   Set rng = Nothing
   
    lCount = .Columns(col).SpecialCells(xlCellTypeBlanks) _
					.Areas(1).Cells.Count
    
    If lCount = 0 Then
        MsgBox "No blanks found in selected column"
        Exit Sub
    ElseIf lCount = .Columns(col).Cells.Count Then
        'next line can be deleted
        MsgBox "Over the Special Cells Limit" 
        Do While lRows < LastRow
            Set rng = .Range(.Cells(lRows, col), _
                 .Cells(lRows + lLimit, col)) _
                 .Cells.SpecialCells(xlCellTypeBlanks)
            rng.FormulaR1C1 = "=R[-1]C"
            lRows = lRows + lLimit
        Loop
    Else
        Set rng = .Range(.Cells(2, col), _
                 .Cells(LastRow, col)) _
                 .Cells.SpecialCells(xlCellTypeBlanks)
        rng.FormulaR1C1 = "=R[-1]C"
    End If

   'replace formulas with values
   With .Cells(1, col).EntireColumn
       .Value = .Value
   End With

End With

End Sub

Fill Blank Cells - Macro 4

This macro example is from Alex Blakenburg, who had numbers entered as text in his data, and didn't want them converted to real numbers. Also, his data didn't always start in row 1 on the worksheet.

In this macro, Alex addressed those problems:

  • macro starts from the active cell
  • stops at the last row in the active column, after resetting the last cell
  • uses .FillDown, instead of .Value = .Value
    • because Value = .Value converts text numbers to real numbers

You can copy the code below, and paste it into your own Excel file. Or, download the sample Excel file, and copy the code from there.

Sub FillSelBlanks_Num()
'from Alex Blakenburg
'use FillDown to prevent text numbers
' from converting to real numbers
'starts from active cell
Dim RowEnd As Long
Dim RowStart As Long
Dim ColStart As Long
Dim RowNo As Long
Dim wks As Worksheet
Set wks = ActiveSheet

' Reset end of sheet and
'   capture end row and column nos
Call ResetLastCell
    
With wks.UsedRange
  RowEnd = .Rows(.Rows.Count).Row
End With

' Start from selected cell
'   check next row is blank, fill down
RowStart = ActiveCell.Row
ColStart = ActiveCell.Column

RowNo = RowStart

' to limit errors due to invoking
'   accidentally, check that next row
'   is blank. If not, exit routine
With wks
  If Not IsEmpty(.Cells(RowStart + 1, _
          ColStart)) _
        And .Cells(RowStart + 1, _
        ColStart).Value <> "" Then
      MsgBox "Expect next row to be blank" _
        & " - exiting routine"
      Exit Sub
  End If

  ' Cycle through rows,
    '  copy group value down

  Do Until RowNo >= RowEnd
    RowNo = RowNo + 1
    If IsEmpty(.Cells(RowNo, _
        ColStart).Value) _
      Or .Cells(RowNo, _
        ColStart).Value = "" Then
      ' Use previous row to try to transfer
      '   both the text attribute and value
      ' for Numeric text. Using the variable
      '   converted numeric text to a number
      .Cells(RowNo, ColStart).FillDown
    End If
  Loop
End With

End Sub

'=============================

Sub ResetLastCell()
Dim lLastRow As Long
Dim lLastColumn As Long
Dim lRealLastRow As Long
Dim lRealLastColumn As Long
Dim rng As Range
Dim wks As Worksheet
Set wks = ActiveSheet

' Find last row,column
'   special cells method

With wks.Range("A1") _
    .SpecialCells(xlCellTypeLastCell)
  lLastRow = .Row
  lLastColumn = .Column
End With

' Find backwards from A1
'   to last non-blank row
With wks.Cells
  lRealLastRow = .Find("*", Range("A1"), _
    xlFormulas, , xlByRows, xlPrevious).Row

  ' Find backwards from A1
    ' to last non-blank column
  lRealLastColumn = .Find("*", Range("A1"), _
    xlFormulas, , xlByColumns, _
      xlPrevious).Column
End With

With wks
  'Delete from row after real last row
  'to last row, per special cells method
  If lRealLastRow < lLastRow Then
    .Range(.Cells(lRealLastRow + 1, 1), _
      .Cells(lLastRow, 1)).EntireRow.Delete
  End If

  'Delete from column after real last column
  'to last column per special cells method
  If lRealLastColumn < lLastColumn Then
      .Range(.Cells(1, lRealLastColumn + 1), _
        .Cells(1, lLastColumn)) _
          .EntireColumn.Delete
  End If

Set rng = .UsedRange 'Resets last cell
End With
    
End Sub

Get the Excel Files ✅

  • Fill Blanks Macros: To see the Excel report with blank cells, and test the Fill Blanks macros, you can download the Fill Blanks Macros Excel file.
    • The zipped file is in xlsm format, and contains macros. When the file opens, be sure to enable macros, if you want to test them.

Get Monthly Excel Tips!

Don't miss my monthly Excel newsletter! You'll get quick tips, article links, and a bit of fun. Add your email, and click Sign Up.

Next, when you get my reply, click the Confirm button. I add this step to protect you from spam!

More Data Entry Tutorials

Fill Blank Cells From Above

Data Entry Tips

Convert Text to Numbers

Increase Numbers With Paste Special

Fill Blank Cells Excel 2003

 

 

Last updated: February 10, 2026 4:07 PM