Adding a blank line above a row with a non-blank cell

laniw75

New Member
Joined
Dec 18, 2010
Messages
15
I need some more vba expertise.

I need a blank row inserted above any non-blank cell in column A.

Can anyone please advise?

Thanks
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
This macro should do it. But note that if you do entire column A it will take some time

Code:
Sub InsertRow()
Dim i As Long

For i = 1 To 5 'insert all the row you want.
    If Len(Cells(i, 1).Value) = 0 Then
        Cells(i, 1).EntireRow.Insert shift:=xlUp 'Inserts row above
        i = i + 1 'skip to next row
    End If
Next i

End Sub
 
Upvote 0
You may wanna add

Code:
application.screenupdating=false
application.screenupdating=true

aroudn the code to speed things up
 
Upvote 0
Hi again,

It appears to be adding a line above the blank cells, I need it to add a line above the non-blank cell.

Thanks
 
Upvote 0
Try this:

Code:
application.screenupdating=false

Sub InsertRow()
Dim i As Long

For i = 1 To 5 'insert all the row you want.
    If Len(Cells(i, 1).Value) = 0 Then
        Cells(i, 1).EntireRow.Insert shift:=xlDown 'Inserts row above
        i = i + 1 'skip to next row
    End If
Next i

application.screenupdating=true
End Sub
 
Upvote 0
If you are looping through the rows to add, it's best to work from bottom to top. Try ..

VBA Code:
Sub Insert_Rows()
    Dim LR As Long, r As Long
    
    Application.ScreenUpdating = False
    LR = Range("A" & Rows.Count).End(xlUp).Row
    For r = LR To 1 Step -1
        If Len(Range("A" & r).Value) > 0 Then
            Rows(r).Insert
        End If
    Next r
    Application.ScreenUpdating = True
End Sub
 
Last edited by a moderator:
Upvote 0
Your VBA worked like a dream, thanks Peter!

Any chance we can enter code to turn the row yellow?
 
Upvote 0

Forum statistics

Threads
1,214,813
Messages
6,121,706
Members
449,049
Latest member
THMarana

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