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

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
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,224,817
Messages
6,181,149
Members
453,021
Latest member
Justyna P

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