Welcome to the Excel Forum

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed.

Please Register to Remove these Ads

Please Register to Remove these Ads



Reply
  #1  
Old 07-02-2009, 01:00 PM
Lithium Lithium is offline
Registered User
 
Join Date: 09 Jun 2009
Location: Veldhoven, Netherlands
MS Office Version:Excel 2007
Posts: 27
Lithium is becoming part of the community
Delete duplicates

Please Register to Remove these Ads

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 03:16 PM. Reason: unclear title / setting as solved
Reply With Quote
  #2  
Old 07-02-2009, 01:10 PM
royUK's Avatar
royUK royUK is offline
Forums Administrator
 
Join Date: 18 Nov 2003
Location: Derbyshire,UK
MS Office Version:Xp; 2007
Posts: 13,723
royUK Has a higher level of understanding royUK Has a higher level of understanding royUK Has a higher level of understanding royUK Has a higher level of understanding royUK Has a higher level of understanding royUK Has a higher level of understanding royUK Has a higher level of understanding
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
__________________
Hope that helps.

RoyUK
--------
If you are pleased with a member's answer then use the Scales icon to rate it

For Excel consulting, free examples and tutorials visit my site
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

Last edited by royUK; 07-02-2009 at 01:14 PM.
Reply With Quote
  #3  
Old 07-03-2009, 03:31 AM
Lithium Lithium is offline
Registered User
 
Join Date: 09 Jun 2009
Location: Veldhoven, Netherlands
MS Office Version:Excel 2007
Posts: 27
Lithium is becoming part of the community
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
Reply With Quote
  #4  
Old 07-04-2009, 08:22 AM
Lithium Lithium is offline
Registered User
 
Join Date: 09 Jun 2009
Location: Veldhoven, Netherlands
MS Office Version:Excel 2007
Posts: 27
Lithium is becoming part of the community
Re: Duplicate rows of column(value in cell)

Has anyone got an idea how to clear the error?

Thanks!
Reply With Quote
  #5  
Old 07-04-2009, 02:28 PM
shg's Avatar
shg shg is online now
Forum Guru
 
Join Date: 20 Jun 2007
Location: The Great State of Texas
MS Office Version:2003, 2007
Posts: 18,387
shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay
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
__________________
Entia non sunt multiplicanda sine necessitate.
Reply With Quote
  #6  
Old 07-04-2009, 07:21 PM
Lithium Lithium is offline
Registered User
 
Join Date: 09 Jun 2009
Location: Veldhoven, Netherlands
MS Office Version:Excel 2007
Posts: 27
Lithium is becoming part of the community
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 07:21 PM. Reason: removed qiuote for readability
Reply With Quote
  #7  
Old 07-04-2009, 07:32 PM
shg's Avatar
shg shg is online now
Forum Guru
 
Join Date: 20 Jun 2007
Location: The Great State of Texas
MS Office Version:2003, 2007
Posts: 18,387
shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay
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
__________________
Entia non sunt multiplicanda sine necessitate.

Last edited by shg; 07-04-2009 at 09:13 PM.
Reply With Quote
  #8  
Old 07-05-2009, 04:24 AM
Lithium Lithium is offline
Registered User
 
Join Date: 09 Jun 2009
Location: Veldhoven, Netherlands
MS Office Version:Excel 2007
Posts: 27
Lithium is becoming part of the community
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 04:28 AM. Reason: added extra info
Reply With Quote
  #9  
Old 07-05-2009, 12:21 PM
shg's Avatar
shg shg is online now
Forum Guru
 
Join Date: 20 Jun 2007
Location: The Great State of Texas
MS Office Version:2003, 2007
Posts: 18,387
shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay
re: Delete duplicates

What result is returned by the formula?
__________________
Entia non sunt multiplicanda sine necessitate.
Reply With Quote
  #10  
Old 07-05-2009, 02:57 PM
Lithium Lithium is offline
Registered User
 
Join Date: 09 Jun 2009
Location: Veldhoven, Netherlands
MS Office Version:Excel 2007
Posts: 27
Lithium is becoming part of the community
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".
Reply With Quote
  #11  
Old 07-05-2009, 04:23 PM
shg's Avatar
shg shg is online now
Forum Guru
 
Join Date: 20 Jun 2007
Location: The Great State of Texas
MS Office Version:2003, 2007
Posts: 18,387
shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay
re: Delete duplicates

Post a workbook.
__________________
Entia non sunt multiplicanda sine necessitate.
Reply With Quote
  #12  
Old 07-05-2009, 04:35 PM
Lithium Lithium is offline
Registered User
 
Join Date: 09 Jun 2009
Location: Veldhoven, Netherlands
MS Office Version:Excel 2007
Posts: 27
Lithium is becoming part of the community
re: Delete duplicates

I've attached the zipfile, it included the xlsm. I'll send the password by PM.
Attached Files
File Type: zip Skin.zip (69.7 KB, 4 views)
Reply With Quote
  #13  
Old 07-05-2009, 06:32 PM
shg's Avatar
shg shg is online now
Forum Guru
 
Join Date: 20 Jun 2007
Location: The Great State of Texas
MS Office Version:2003, 2007
Posts: 18,387
shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay
re: Delete duplicates

VLB!B17 is empty.
__________________
Entia non sunt multiplicanda sine necessitate.
Reply With Quote
  #14  
Old 07-05-2009, 09:22 PM
Lithium Lithium is offline
Registered User
 
Join Date: 09 Jun 2009
Location: Veldhoven, Netherlands
MS Office Version:Excel 2007
Posts: 27
Lithium is becoming part of the community
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 09:25 PM. Reason: readability
Reply With Quote
  #15  
Old 07-06-2009, 12:30 AM
shg's Avatar
shg shg is online now
Forum Guru
 
Join Date: 20 Jun 2007
Location: The Great State of Texas
MS Office Version:2003, 2007
Posts: 18,387
shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay shg makes giving solutions look like childsplay
re: Delete duplicates

How about posting a correct example, mon frere?
__________________
Entia non sunt multiplicanda sine necessitate.
Reply With Quote


Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Forum Jump