Insert blank row after duplicate rows?

joekoug

New Member
Joined
Nov 2, 2004
Messages
12
I have a spreadsheet like this:


123 smith
123 smith
123 smith
124 jones
124 jones
124 jones
134 bill
134 bill

I'd like to know if I can automatically insert a blank row after each set of duplicates in column 1?

Thanks
Joe
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Hi,

the code that follows would do it:
Sub insertBlank()
'
'
Application.ScreenUpdating = False

Dim startPoint As Integer
Dim noToCheck As Integer
Dim i As Integer

startPoint = ActiveCell.Row
Selection.End(xlDown).Select
noToCheck = ActiveCell.Row - startPoint
Selection.End(xlUp).Select

For i = 1 To noToCheck
If ActiveCell.Value = ActiveCell.Offset(-1, 0).Value Then
ActiveCell.Offset(1, 0).Select
Selection.EntireRow.Insert
End If

ActiveCell.Offset(1, 0).Select
Next i
Application.ScreenUpdating = True

End Sub

If you select the cell at the top left and then run it it would do it. (This probably isn't the most eloquent way to do it but if you're in a hurry it'll do it.)

If you have not used macros before let me know and I'll try to help further.

Regards,

Will
 
Upvote 0
When running the macro i get a vbasic error:

Runtime Error 1004:
Application-defined or object-defined error

I debug and it brings up this line of code:

"If ActiveCell.Value = ActiveCell.Offset(-1, 0).Value Then"

Thanks
Joe
 
Upvote 0
Joe,

sorry I didn't consider that the data may start in row 1 of your sheet. If you insert:

ActiveCell.Offset(1,0).Select

above the For i = 1 To noToCheck line it should solve your problem.

Regards,

Will
 
Upvote 0
Joe,

I've had another thought. By 'set of duplicates' do you mean group of identical consecutive entries or do you mean duplicate (i.e. only two identical lines). In other words do you want the data to be:

123 smith
123 smith

123 smith
124 jones
124 jones

124 jones
134 bill
134 bill

or

123 smith
123 smith
123 smith

124 jones
124 jones
124 jones

134 bill
134 bill

If it is the latter you will also need to alter the line:
If ActiveCell.Value = ActiveCell.Offset(-1, 0).Value Then

to:

If ActiveCell.Value = ActiveCell.Offset(-1, 0).Value And _
ActiveCell.Value <> ActiveCell.Offset(1,0).Value Then

Regards,

Will
 
Upvote 0
Will- Worked like a charm! Saved me hours of work! One more question for you though. I wasn't very clear in my original question, I would like a blank row inserted after each set of unique data or if there is only one row of data i would still like a break in between those lines. This is a huge spreadsheet of client transactions, some client records are missing so i have some rows with only one entry for a client, and other rows where there are 4 or 5 entries for the same client, I'd like to be able to easily glance through the sheet and tell where one guy ends and another begins...

Thanks for all the help
Joe
 
Upvote 0
Public Sub InsertRows()
For Rw = Cells(65536, 1).End(xlUp).Row To 1 Step -1
If Not Cells(Rw, 1).Value = Cells(Rw + 1, 1).Value Then Cells(Rw + 1, 1).EntireRow.Insert
Next Rw
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,535
Messages
6,114,198
Members
448,554
Latest member
Gleisner2

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