multiple Down-Fill formulas, not values...

omairhe

Well-known Member
Joined
Mar 26, 2009
Messages
2,040
Office Version
  1. 2019
Platform
  1. Windows
Hey everyone,

I recently was in need of a procedure where multiple headings required filling down. Kind of like

From this
Bean
""
""
Axel
""
Frog
""
""
""

To this
Bean
Bean
Bean
Axel
Axel
Frog
Frog
Frog
Frog

an easy way of doing it was selecting the column and using F5 key; selecting blanks and Ctrl + Enter. This works great with values, however, with formulas its a different story. What might be a solution here if I needed to copy/fill down formulas multiple times for each formula based heading.

Thanks and will appreciate.
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
VBA Code:
Sub COPY_HEADER()

Application.ScreenUpdating = False
Application.EnableEvents = False

    For my_rows = 2 To Range("C" & Rows.Count).End(xlUp).Row 'C2 is the starting cell reference

        If IsEmpty(Range("C" & my_rows)) Then
            
            Range("C" & my_rows - 1).Copy
            Range("C" & my_rows).PasteSpecial (xlPasteFormulas)
        
        End If

    Next my_rows

Application.ScreenUpdating = True
Application.EnableEvents = True

End Sub

The above code copy pastes the formula in column C but it takes a while to do this, as I am working with more than 50,000 rows. Can it be made more efficient please, or is this the best that vba has to offer?
 
Upvote 0
Not a very elegant way of doing this but this will do the job for now..

VBA Code:
Sub Copy_Formula_Headers()
'please select last cell from below of your desired column before running this macro
On Error GoTo errhandler
Application.ScreenUpdating = False
Dim I
For I = 1 To 1000000 'max number of possible filldowns
    If IsEmpty(ActiveCell.Value) Then
    ActiveCell.Offset().Range("A1").Select
    Range(Selection, Selection.End(xlUp)).Select
    Selection.FillDown
    Else
    ActiveCell.Offset(1, 0).Range("A1").Select
    Selection.End(xlUp).Select
    ActiveCell.Offset(-1, 0).Range("A1").Select
    End If
Next I
Application.ScreenUpdating = True
errhandler: Exit Sub
End Sub
 
Upvote 0
For the record, if someone is looking for something similar and working with 1000s of rows that need formulas to be copied down, I reckon disabling the auto calculations temporarily. It will do the job in mere seconds. this version of the code will disable calculations temporarily until the procedure ends .

VBA Code:
Sub Copy_Formula_Headers()
'please select last cell from below of your desired column before running this macro
On Error GoTo errhandler
Application.Calculation = xlManual
Application.ScreenUpdating = False
Application.EnableEvents = False
Dim I
For I = 1 To 10000000 'max number of possible filldowns (please do not change)
    If IsEmpty(ActiveCell.Value) Then
    ActiveCell.Offset().Range("A1").Select
    Range(Selection, Selection.End(xlUp)).Select
    Selection.FillDown
    Else
    ActiveCell.Offset(1, 0).Range("A1").Select
    Selection.End(xlUp).Select
    ActiveCell.Offset(-1, 0).Range("A1").Select
    End If
Next I
Application.EnableEvents = True
Application.ScreenUpdating = True
errhandler:
Application.Calculation = xlAutomatic
Exit Sub
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,198
Members
449,072
Latest member
DW Draft

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