Results 1 to 8 of 8

Insert blank row after duplicate rows?

This is a discussion on Insert blank row after duplicate rows? within the Excel Questions forums, part of the Question Forums category; I have a spreadsheet like this: 123 smith 123 smith 123 smith 124 jones 124 jones 124 jones 134 bill ...

  1. #1
    New Member
    Join Date
    Nov 2004
    Location
    chicago
    Posts
    12

    Default Insert blank row after duplicate rows?

    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

  2. #2
    Board Regular
    Join Date
    Oct 2004
    Location
    Luxembourg
    Posts
    195

    Default

    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

  3. #3
    New Member
    Join Date
    Nov 2004
    Location
    chicago
    Posts
    12

    Default

    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

  4. #4
    Board Regular
    Join Date
    Oct 2004
    Location
    Luxembourg
    Posts
    195

    Default

    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

  5. #5
    Board Regular
    Join Date
    Oct 2004
    Location
    Luxembourg
    Posts
    195

    Default

    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

  6. #6
    New Member
    Join Date
    Nov 2004
    Location
    chicago
    Posts
    12

    Default

    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

  7. #7
    MrExcel MVP
    Join Date
    Apr 2002
    Location
    Vancouver BC , Canada
    Posts
    6,259

    Default Re: Insert blank row after duplicate rows?

    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

    <MARQUEE>...........Never be afraid to try something new. Remember, amateurs built the ark, professionals built the Titanic...............The easiest thing to find is fault, don't be easy !.. --Anonymous--...</marquee>

  8. #8
    New Member
    Join Date
    Nov 2004
    Location
    chicago
    Posts
    12

    Default

    Worked like a charm!!

    THANKS
    Joe

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


DMCA.com