GoTo Cell and loop down until blank cell found, insert new row as above

LittlegmoonBH

New Member
Joined
May 2, 2019
Messages
15
Hi All,

I'm trying to add a macro to a template that' proving tricky.

Basically, the macro needs to go to cell "C4" then loop down a variable amount of times until it hits a blank cell, it then needs to add a blank row using the formatting from the above populated row without any data.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Welcome to the Board!

See if this does what you want:
Code:
Sub MyMacro()

    Dim r As Long
    
'   Find first blank row below C4
    If Range("C5") = "" Then
        r = 5
    Else
        r = Range("C4").End(xlDown).Row + 1
    End If
    
'   Insert blank row
    Rows(r).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    
End Sub
 
Upvote 0
Legend! It gives me what I need, the only thing I need to figure out is how bring down the formatting from the cells above. The above adds a row, but doesn't bring down the borders :)
 
Upvote 0
Hmmm...

Mine seemed to extend it down automatically. Let try it explicitly like this:
Code:
Sub MyMacro()

    Dim r As Long
    
'   Find first blank row below C4
    If Range("C5") = "" Then
        r = 5
    Else
        r = Range("C4").End(xlDown).Row + 1
    End If
    
'   Insert blank row
    Rows(r).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    
'   Copy formatting from row above
    Rows(r - 1).Copy
    Range("A" & r).PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
    Application.CutCopyMode = False
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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