Insert an empty row above a highlighted cell.

bessieunl

New Member
Joined
May 19, 2015
Messages
11
I want to insert a blank row above each colored cell when in a column

Under column A I have 5268 records
I want to insert a blank row above a cell that is highlighted yellow. I want to do it till the last value under that column.
Can someone tell me please how to write a vba for?
Thank you in advance for your assistance.

My original code isn't doing what I am asking because instead of inserting just one row, its inserting 5268 rows above the first found highlighted cell.
Code:
Sub Top10()
'lastrow = ActiveSheet.UsedRange.Rows(ActiveSheet.UsedRange.Rows.count).Row'
Dim c As range

    For Each c In range("A1:A100000")
        If c.Interior.Color = 65535 Then
            c.Offset(0, 0).EntireRow.Insert
        End If
    Next c

End Sub

also, another thing is that I don't want use range actually, because it requires the user to know how many rows of data it has, I want to use a for loop sth like for I = 2 to lastrow?
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
When inserting or deleting rows, it is best to loop through your range backwards. Otherwise:
- with "insert", you keep hitting the same row (because your rows are moving down at the same time you loop is)
- with "delete", you will miss rows if you have two in a row that should be deleted (because the rows are moving up as you loop is moving down)

So, you want to use something like:
Code:
For i = LastRow to 1 Step -1
    If Cells(i,"A").Interior.Color = 65535 Then
        Rows(i).Insert
    End If
Next i
 
Upvote 0
When working with rows, always loop backwards:

Code:
Sub Top10()


Dim i As Long

For i = Cells(Rows.Count, 1).End(xlUp).Row To 1 Step -1
    If Cells(i, 1).Interior.Color = 65535 Then Cells(i, 1).EntireRow.Insert Shift:=xlDown
Next i

End Sub
 
Upvote 0
Try this:

Code:
Sub test()
'
' test Macro
'
    LastRow = Range("A" & Rows.Count).End(xlUp).Row 'This will limit it to only the last row with data, rather than all rows
    Dim i As Integer
    For i = LastRow To 1 Step -1 'This begins reviewing at the bottom and works upwards
        If Range("A" & i).Interior.Color = 65535 Then
            Rows(i).Select
            Selection.Insert Shift:=xlDown ' This inserts your row
            With Rows(i).Interior ' This removes any highlighting from the inserted row
                .Pattern = xlNone
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With
        End If
    Next i 'goes to next row up

End Sub
 
Upvote 0
When working with rows, always loop backwards:

Code:
Sub Top10()


Dim i As Long

For i = Cells(Rows.Count, 1).End(xlUp).Row To 1 Step -1
    If Cells(i, 1).Interior.Color = 65535 Then Cells(i, 1).EntireRow.Insert Shift:=xlDown
Next i

End Sub

Thanks! This worked perfect!
 
Upvote 0

Forum statistics

Threads
1,214,818
Messages
6,121,725
Members
449,049
Latest member
MiguekHeka

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