|
Home > Data > Data Entry > Fill Blanks Fill Blank Cells in Excel - Macros
|

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.

Video: Fill Blank Cells From AboveIn this video, I show how to fill blank cells without macros. The macros on this page do something similar, but much faster!
|
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.
|
-- Macro 1 - Dave Peterson -- Macro 2 - Rick Rothstein -- Macro 3 - Dave Peterson / Ron de Bruin -- Macro 4 - Alex Blakenburg |
In this fill blank cells macro, from Dave Peterson:
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
This fill blank cells macro, from Rick Rothstein
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
This example combines Dave Peterson's code (Example 1), with the special cells test from Ron de Bruin.
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
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:
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
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!
Increase Numbers With Paste Special
Last updated: February 10, 2026 4:07 PM