+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Registered User
    Join Date
    06-09-2009
    Location
    Veldhoven, Netherlands
    MS-Off Ver
    Excel 2007
    Posts
    32

    Delete duplicates

    Hi, the code in the codebox under here, which I wrote, is supposed to delete all duplicate records in the column. Which column to uniquify is stated in cell Skin!B17. I searched the internet round, and there are different solutions everywhere. I have been unable though to find the right one - so I thought, why not ask here. If you have a solution, I'd be happy to hear it.

    Code:
    Sub Remove_Duplicates_In_A_Range()
        
        Dim x               As Long
        Dim LastRow         As Long
        
    Sheets("VLB").Select
    
      LastRow = Range("A65536").End(xlUp).Row
        For x = LastRow To 1 Step -1
            If Application.WorksheetFunction.CountIf(Sheets("Skin").Range(B17 & "1:" & B17 & x), Sheets("Skin").Range(B17 & x).Text) > 1 Then
                Sheets("Skin").Range(B17 & x).EntireRow.Delete
            End If
        Next x
         
    End Sub
    Last edited by Lithium; 07-06-2009 at 04:16 PM. Reason: unclear title / setting as solved

  2. #2
    Forums Administrator royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    24,427

    Re: Duplicate rows of column(value in cell)

    Try this, your range definition looks wrong
    Code:
        Dim x      As Long
        Dim Col    As Long
        Dim LastRow As Long
    
        With Sheets("VLB")
            Col = .Cells(17, 2).Value
            LastRow = .Cells(.rows.count).End(xlUp).Row
    End with
    
    With Sheets("Skin")
            For x = LastRow To 1 Step -1
                If Application.WorksheetFunction.CountIf(.Cells(1, _
                                 Col), .Cells(LastRow, Col)).Value) > 1 Then
                    .Cells(x,Col).EntireRow.Delete
                End If
            Next x
    Last edited by royUK; 07-02-2009 at 02:14 PM.
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel consulting, free examples and tutorials visit Excel Consulting-Excel VBA
    Check out the free Excel Toolbar

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)


    Code Tags: Make your code easier for us to read

  3. #3
    Registered User
    Join Date
    06-09-2009
    Location
    Veldhoven, Netherlands
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Duplicate rows of column(value in cell)

    Developer gave me a few errors:
    • the 'End With' was missing
    • there was one closing bracket too many
    I believe I fixed the errors, but now it gives me a 1004:
    application-defined or object-defined error. The line, with an asterisk in front of it, is the line which gives the error.

    Code:
    Sub Remove_Duplicates_In_A_Range()
        
        Dim x      As Long
        Dim Col    As Long
        Dim LastRow As Long
    
    'Define ranges
    With Sheets("VLB")
            Col = .Cells(17, 2).Value
            LastRow = .Cells(.Rows.Count).End(xlUp).Row
    End With
    
    'Execute removal
    With Sheets("Skin")
            For x = LastRow To 1 Step -1
    *            If Application.WorksheetFunction.CountIf(.Cells(1, Col), .Cells(LastRow, Col).Value) > 1 Then
                    .Cells(x, Col).EntireRow.Delete
                End If
            Next x
    End With
    End Sub

  4. #4
    Registered User
    Join Date
    06-09-2009
    Location
    Veldhoven, Netherlands
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Duplicate rows of column(value in cell)

    Has anyone got an idea how to clear the error?

    Thanks!

  5. #5
    Forum Moderator shg's Avatar
    Join Date
    06-21-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007
    Posts
    25,129

    Re: Duplicate rows of column(value in cell)

    Try this (untested):
    Code:
    Sub RemoveDups()
        Dim iRow    As Long
        Dim iCol    As Long
        Dim iRowEnd As Long
    
        With Sheets("VLB")
            iCol = .Range("B17").Value
            iRowEnd = .Cells(.Rows.Count).End(xlUp).Row
        End With
    
        With Sheets("Skin").Columns(iCol)
            For iRow = iRowEnd To 2 Step -1
                If WorksheetFunction.CountIf(Range(.Cells(1), .Cells(iRow - 1)), .Cells(iRow).Value) > 0 Then
                    .Cells(iRow).EntireRow.Delete
                End If
            Next iRow
        End With
    End Sub
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  6. #6
    Registered User
    Join Date
    06-09-2009
    Location
    Veldhoven, Netherlands
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Duplicate rows of column(value in cell)

    Code:
    Sub RemoveDups()
        Dim iRow    As Long
        Dim iCol    As Long
        Dim iRowEnd As Long
    
        With Sheets("VLB")
            iCol = .Range("B17").Value
            iRowEnd = .Cells(.Rows.Count).End(xlUp).Row
        End With
    
        With Sheets("Skin").Columns(iCol)
            For iRow = iRowEnd To 2 Step -1
                If WorksheetFunction.CountIf(Range(.Cells(1), .Cells(iRow - 1)), .Cells(iRow).Value) > 0 Then
                    .Cells(iRow).EntireRow.Delete
                End If
            Next iRow
        End With
    End Sub
    It still gives me the 1004 error (on the red line). In cell B17 is a formula, which calculates the column character. I hope that is not a problem,
    Last edited by Lithium; 07-04-2009 at 08:21 PM. Reason: removed qiuote for readability

  7. #7
    Forum Moderator shg's Avatar
    Join Date
    06-21-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007
    Posts
    25,129

    Re: Duplicate rows of column(value in cell)

    If B17 contains a string (e.g., "A"), then the column variable has to be either a string or a Variant (which will accommodate a number or a string):

    Code:
    Sub RemoveDups()
        Dim iRow    As Long
        Dim vCol    As Variant
        Dim iRowEnd As Long
    
        With Sheets("VLB")
            vCol = .Range("B17").Value
            iRowEnd = .Cells(.Rows.Count).End(xlUp).Row
        End With
    
        With Worksheets("Skin").Columns(vCol)
            For iRow = iRowEnd To 2 Step -1
                If WorksheetFunction.CountIf(Range(.Cells(1), .Cells(iRow - 1)), .Cells(iRow).Value) > 0 Then
                    .Cells(iRow).EntireRow.Delete
                End If
            Next iRow
        End With
    End Sub
    Last edited by shg; 07-04-2009 at 10:13 PM.
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  8. #8
    Registered User
    Join Date
    06-09-2009
    Location
    Veldhoven, Netherlands
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Duplicate rows of column(value in cell)

    Actually, B17 contains this formula:
    Code:
    =SUBSTITUTE(ADDRESS(1;MATCH(A17;XML!1:1;0);4);1;"")
    "XML" is a sheet name and A17 is the string to match.

    It still gives a 1004 on the same line as before
    Last edited by Lithium; 07-05-2009 at 05:28 AM. Reason: added extra info

  9. #9
    Forum Moderator shg's Avatar
    Join Date
    06-21-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007
    Posts
    25,129

    re: Delete duplicates

    What result is returned by the formula?
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  10. #10
    Registered User
    Join Date
    06-09-2009
    Location
    Veldhoven, Netherlands
    MS-Off Ver
    Excel 2007
    Posts
    32

    re: Delete duplicates

    A17 is a string and is a column name of sheet "XML". The value of B17 will be a letter of the alphabet. That letter is equal to the column which has the column name in A17. A17 and B17 are on a different sheet than "XML".

  11. #11
    Forum Moderator shg's Avatar
    Join Date
    06-21-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007
    Posts
    25,129

    re: Delete duplicates

    Post a workbook.
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  12. #12
    Registered User
    Join Date
    06-09-2009
    Location
    Veldhoven, Netherlands
    MS-Off Ver
    Excel 2007
    Posts
    32

    re: Delete duplicates

    I've attached the zipfile, it included the xlsm. I'll send the password by PM.
    Attached Files Attached Files

  13. #13
    Forum Moderator shg's Avatar
    Join Date
    06-21-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007
    Posts
    25,129

    re: Delete duplicates

    VLB!B17 is empty.
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  14. #14
    Registered User
    Join Date
    06-09-2009
    Location
    Veldhoven, Netherlands
    MS-Off Ver
    Excel 2007
    Posts
    32

    re: Delete duplicates

    Okay, that's not right. It should be on the XML sheet, B17. The sheets got mixed up (purple). The semi-correct code:
    Code:
    Sub Remove_Duplicates_In_A_Range()
        
        Dim x      As Long
        Dim Col    As Long
        Dim LastRow As Long
    
    'Define ranges
    With Sheets("Skin")
            Col = .Cells(17, 2).Value
            LastRow = .Cells(.Rows.Count).End(xlUp).Row
    End With
    
    'Execute removal
    With Sheets("VLB")
            For x = LastRow To 1 Step -1
                If Application.WorksheetFunction.CountIf(.Cells(1, Col), .Cells(LastRow, Col)).Value > 1 Then
                    .Cells(x, Col).EntireRow.Delete
                End If
            Next x
    End With
    End Sub
    Now it gives me an invalid qualifier (red)... I guess I'd buy the book VBA for Dummies or something
    Last edited by Lithium; 07-05-2009 at 10:25 PM. Reason: readability

  15. #15
    Forum Moderator shg's Avatar
    Join Date
    06-21-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007
    Posts
    25,129

    re: Delete duplicates

    How about posting a correct example, mon frere?
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0