Creating a macro that will copy down content from certain cells

UTdawg

New Member
Joined
Jul 30, 2019
Messages
6
Hi all,

I've created a macro in my excel sheet that will copy down all of the formulas when I insert a row. I'm using the following code to do this:

Sub InsertRowFormulas ()
Selection.EntireRow.Insert
For Each cell In Intersect (ActiveSheet.UsedRange, Selection.Offset (-1, 0). EntireRow)
If cell.Has Formula Then
cell.Copy cell.Offset (1,0)
End If
Next
End Sub

However, there are some columns with an "H" in them which denotes a holiday. I would like for my macro to copy down these H's as well when inserting a new row. Any advice or help on how to do this would be greatly appreciated.

Thanks!
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Try:
Code:
Sub M1()

    Dim x   As Long
    Dim c   As Long
        
    Application.ScreenUpdating = False
    
    With Selection
        .EntireRow.Insert
        .SpecialCells(xlCellTypeFormulas).Copy
        .Offset(1).PasteSpecial xlPasteFormulas
        Application.CutCopyMode = False
        x = 1
        Do
            If .Cells(1, x).Value = "H" Then .Cells(2, x).Value = "H"
            x = x + 1
        Loop Until Len(.Cells(1, x).Value) = 0
    End With
                
    Application.ScreenUpdating = True
    
End Sub
 
Last edited:
Upvote 0
Unfortunately that didn't work. I got an error message saying Run-time error '1004'

Application-defined or object-defined error

Any advice on what to do now?
 
Upvote 0
Tested and this works for me, try:
Code:
Sub M1()

    Dim r   As Range
    
    Application.ScreenUpdating = False
        
    Selection.EntireRow.Insert
    
    For Each r In Intersect(ActiveSheet.UsedRange, Selection.Offset(-1).EntireRow)
        With r
            If .HasFormula Then .Copy .Offset(1)
            If .Value = "H" Then .Offset(1).Value = "H"
        End With
    Next r
    
     With Application
        .CutCopyMode = False
        .ScreenUpdating = True
    End With
            
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,390
Members
448,957
Latest member
Hat4Life

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