VBA to insert blank row after repeating cell values in a column

KP_SoCal

Board Regular
Joined
Nov 17, 2009
Messages
116
In Column A, I have several repeating cell values. I would l like to insert a blank row at point the value changes into a new set of repeating values. The illustration below should better illustrate what I’m trying to get at.

Before code execution…

A1: Title
A2: Apple
A3: Apple
A4: Apple
A5: Pear
A6: Pear
A7: Orange
A8: Orange
A9: Orange



AFTER code execution…
A1: Title
A2: Apple
A3: Apple
A4: Apple
A5:
A6: Pear
A7: Pear
A8:
A9: Orange
A10: Orange
A11: Orange


Here’s a nice link that points me in the general direction of what I need, but I can’t figure out how to tailor it to my specific needs. Thanks for any suggestions! :)
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Try...

Code:
Sub InsertBlankRows()

Dim LastRow As Long
Dim i As Long

LastRow = Cells(Rows.Count, "A").End(xlUp).Row

For i = LastRow To 2 Step -1
    If i = 2 Then
        'Do nothing
    ElseIf Cells(i, "A") <> Cells(i - 1, "A") Then
        Cells(i, "A").Insert
    End If
Next i

End Sub
 
Upvote 0
Here's a method that doesn't involve looping through the rows individually. I think it should do what you want.

VBA Code:
Sub InsertRows()
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  With Range("A1", Range("A" & Rows.Count).End(xlUp))
    .Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(1), _
    Replace:=True, PageBreaks:=False, SummaryBelowData:=True
    .Offset(, -1).AutoFilter Field:=1, Criteria1:="=*total*"
    .Offset(2).SpecialCells(xlCellTypeVisible).ClearContents
    .AutoFilter
    .Offset(, -1).EntireColumn.Delete
    .EntireColumn.RemoveSubtotal
  End With
  Application.DisplayAlerts = True
  Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Hats off to both of you guys! I really appreciate it. Both of these examples worked!

Cheers!!!

:biggrin:

KP
 
Upvote 0
Just realised, that mine doesn't actually need to do the AutoFilter. So this version should be even faster, though speed probably won't be an issue for you anyway unless your dataset is large.

VBA Code:
Sub InsertRows()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
With Range("A1", Range("A" & Rows.Count).End(xlUp))
.Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(1), _
Replace:=True, PageBreaks:=False, SummaryBelowData:=True
.Offset(2, -1).SpecialCells(xlCellTypeConstants).Offset(, 1).ClearContents
.Offset(, -1).EntireColumn.Delete
.EntireColumn.RemoveSubtotal
End With
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
With regards to the solution I offered, to insert an entire blank row, not just a blank cell, replace...

Code:
Cells(i, "A").Insert
with

Code:
Cells(i, "A").EntireRow.Insert
 
Upvote 0
With regards to the solution I offered, to insert an entire blank row, not just a blank cell, replace...

Code:
Cells(i, "A").Insert
with

Code:
Cells(i, "A").EntireRow.Insert
Or just
Code:
Rows(i).Insert
 
Upvote 0
Try...

Code:
Sub InsertBlankRows()
 
Dim LastRow As Long
Dim i As Long
 
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
 
For i = LastRow To 2 Step -1
    If i = 2 Then
        'Do nothing
    ElseIf Cells(i, "A") <> Cells(i - 1, "A") Then
        Cells(i, "A").Insert
    End If
Next i
 
End Sub



how can i do the same code for the Two columns?
i.e.
ElseIf Cells(i, "A") <> Cells(i - 1, "A") Then
Cells(i, "A").EntireRow.Insert

Also do the same for Column "C"
ElseIf Cells(i, "C") <> Cells(i - 1, "C") Then
Cells(i, "C").EntireRow.Insert

BUT I don't want more than one blank row after applying Both Logic
hence
May be,IF there are Two consequtive Blank Row delete all extra & keep one only???
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,198
Members
449,072
Latest member
DW Draft

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