Fill Blank Cells with Formula From Row Above

millerprm

New Member
Joined
Jan 14, 2015
Messages
6
Where I work, Excel 2007 is still being used. When doing autofill on workbooks with thousands of rows, you get an error stating "Selection is too large" because of the 8192 different areas in the special cells range issue. I found code online (by Dave Peterson by way of Ron De Bruin) that will work around this using a Do While loop on 8000 rows at a time, but it fills the rows with values. I need something that fills each blank with the FORMULA from the cell above. Any examples would be greatly appreciated. Code is below. Much Thanks

Code:
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
        MsgBox "Over the Special Cells Limit" 'this line can be deleted
        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
 
Last edited by a moderator:

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
might be missing something, but can't you simply remove these lines
Code:
   'replace formulas with values
   With .Cells(1, col).EntireColumn
       .Value = .Value
   End With
 
Upvote 0
might be missing something, but can't you simply remove these lines
Code:
   'replace formulas with values
   With .Cells(1, col).EntireColumn
       .Value = .Value
   End With

thanks for the quick reply. i did comment out the code, but when i ran the macro again, the column was still filled with values
 
Upvote 0
Are there formulas in the line above the blanks ??
The code works fine for me !!
But you simply appear to be copying the formula down the page...if that's so, why not use this instead
It worked for 20000 rows
Rich (BB code):
Sub MM1()
Dim lr As Long
col = ActiveCell.Column
lr = Cells(Rows.Count, col).End(xlUp).Row
Range(Cells(2, col), Cells(lr, col)).Formula = "your starting line formula goes here"
End Sub
 
Upvote 0
Are there formulas in the line above the blanks ??
The code works fine for me !!
But you simply appear to be copying the formula down the page...if that's so, why not use this instead
It worked for 20000 rows
Rich (BB code):
Sub MM1()
Dim lr As Long
col = ActiveCell.Column
lr = Cells(Rows.Count, col).End(xlUp).Row
Range(Cells(2, col), Cells(lr, col)).Formula = "your starting line formula goes here"
End Sub

thanks again for the quick reply. yes, row 2 has a formula in every column, and there are several. but i'll give your idea a try on one of the columns. thanks!
 
Upvote 0

Forum statistics

Threads
1,214,515
Messages
6,119,973
Members
448,933
Latest member
Bluedbw

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