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.
 

Some videos you may like

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

Smitty

Legend
Joined
May 15, 2003
Messages
29,536
Welcome to the Board!

You can use a Pivot Table:

<b>Excel 2012</b><table cellpadding="2.5px" rules="all" style=";background-color: #FFFFFF;border: 1px solid;border-collapse: collapse; border-color: #BBB"><colgroup><col width="25px" style="background-color: #DAE7F5" /><col /></colgroup><thead><tr style=" background-color: #DAE7F5;text-align: center;color: #161120"><th></th><th>A</th></tr></thead><tbody><tr ><td style="color: #161120;text-align: center;">3</td><td style=";">Row Labels</td></tr><tr ><td style="color: #161120;text-align: center;">4</td><td style=";">Apple</td></tr><tr ><td style="color: #161120;text-align: center;">5</td><td style=";">5678</td></tr><tr ><td style="color: #161120;text-align: center;">6</td><td style=";">9876</td></tr><tr ><td style="color: #161120;text-align: center;">7</td><td style=";">Pear</td></tr><tr ><td style="color: #161120;text-align: center;">8</td><td style=";">1357</td></tr><tr ><td style="color: #161120;text-align: center;">9</td><td style=";">2468</td></tr><tr ><td style="color: #161120;text-align: center;">10</td><td style=";">9753</td></tr></tbody></table><p style="width:3.6em;font-weight:bold;margin:0;padding:0.2em 0.6em 0.2em 0.5em;border: 1px solid #BBB;border-top:none;text-align: center;background-color: #DAE7F5;color: #161120">Sheet4</p><br /><br />

HTH,
 

kalak

Active Member
Joined
Jun 10, 2015
Messages
487
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
 

Snail123

New Member
Joined
Sep 24, 2015
Messages
6
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?
 

kalak

Active Member
Joined
Jun 10, 2015
Messages
487
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.
 

Snail123

New Member
Joined
Sep 24, 2015
Messages
6
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,123,135
Messages
5,599,917
Members
414,348
Latest member
KloppyM

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
Top