VBA: Search for empty cells in column and replace fill will the previous cell in same column

MarcWiseman

New Member
Joined
Jun 26, 2008
Messages
33
Hey all,

This seems like it should be ridiculously easy. But how would you write a macro that searches for empty cells in a column, in my case column B, and pastes the previous cell in the column in there.

For example, in my column A row 1 has 103.1, followed by 4 blank cells. Row 6 has 117.5 followed by 10 blank cells, etc. I want the blank cells in row 2-5 to have 103.1 and rows 7-16 to read 117.5.

I hope this is clear.

thanks in advance.

Marc
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
I would do the following:


  • Start recording a macro
    Create an if statement in column B. If column A has a value then show it, else show cell before.
    Copy formula down, and paste column values in to column A
    Clear column B
    Stop recording

D
 
Upvote 0
You can use a for loop for each cell in the column, and it might help to use relative references. This will give the active cell the value of the one above it:
ActiveCell.FormulaR1C1 = "=R[-1]C"
 
Upvote 0
This macro should do what you want...

Code:
Sub FillInTheBlanks()
  Dim LastRow As Long
  Const DataStartRow As Long = 2
  Const ColumnToFill As String = "A"
  LastRow = Cells.Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlFormulas).Row
  Cells(DataStartRow, ColumnToFill).Resize(LastRow - DataStartRow + 1).SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
  Columns(ColumnToFill).Value = Columns(ColumnToFill).Value
End Sub
Note: There are two constants (the Const statements) that you need to make sure are set for your own setup. I guessed the data starts on Row 2 (Row 1 being a header) and the blanks to be filled in are in Column A.
 
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,261
Members
452,901
Latest member
LisaGo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top