Insert blank row after a group of data using VBA

Snail123

New Member
Joined
Sep 24, 2015
Messages
6
Hello,
I was wondering if it was possible to insert 2 blank rows after a data change in a specific column (i.e. group the data), then to hide that column, and use the data entry as a title (in bold) for that group of data. For example;

Col A Col B
1234 Apple
5678 Apple
9876 Apple
1357 Pear
2468 Pear
9753 Pear

Would look like this:

Col A
Apple
1234
5678
9876

Pear
1357
2468
9753

I have found a code that inserts 2 rows after a data change, but not sure where to begin when trying to edit it to hide the data change column (D in this example) and to add the title to the 2nd new row:

Sub InsertRowAtChangeInValue()
Dim lRow As Long
For lRow = Cells(Cells.Rows.Count, "D").End(xlUp).Row To 2 Step -1
If Cells(lRow, "D") <> Cells(lRow - 1, "D") Then
Rows(lRow).EntireRow.Insert
Rows(lRow).EntireRow.Insert
End If
Next lRow
End Sub

Thanks for your help in anticipation!

PS. I'm using MS Excel 2013.
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Welcome to the Board!

You can use a Pivot Table:


Excel 2012
A
3Row Labels
4Apple
55678
69876
7Pear
81357
92468
109753
Sheet4


HTH,
 
Upvote 0
You can just modify your own, not very efficient, code a bit like this, with your data in cols C and D. Also delete colD at the end if you don't want it.
Code:
Sub InsertRowAtChangeInValue()

Dim lRow As Long

For lRow = Cells(Cells.Rows.Count, "D").End(xlUp).Row To 2 Step -1
If Cells(lRow, "D") <> Cells(lRow - 1, "D") Then
    Rows(lRow).Resize(2).EntireRow.Insert
    Cells(lRow + 2, "D").Copy Cells(lRow + 1, "C")
End If
Next lRow

'Range("D:D").ClearContents
End Sub
 
Upvote 0
Thank you Smitty for your suggestion and warm welcome! kalak your code works very well - thank you. My only question is how can I make the header row: Cells(lRow + 2, "D").Copy Cells(lRow + 1, "C") become bold?
 
Upvote 0
Thank you Smitty for your suggestion and warm welcome! kalak your code works very well - thank you. My only question is how can I make the header row: Cells(lRow + 2, "D").Copy Cells(lRow + 1, "C") become bold?
There's a formatting and etc. ribbon at the top of very post you make in MrExcel.
 
Upvote 0
Not quite what I meant, but I have solved it by adding in this bit of code: Font.Bold = True. Thanks for your help earlier.
 
Upvote 0

Forum statistics

Threads
1,213,489
Messages
6,113,952
Members
448,535
Latest member
alrossman

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