Forum Statistics
- Forum Members:
- Total Threads:
- Total Posts: 18
There are 1 users currently browsing forums.
|
 |
|

07-02-2009, 01:00 PM
|
|
Registered User
|
|
Join Date: 09 Jun 2009
Location: Veldhoven, Netherlands
MS Office Version:Excel 2007
Posts: 27
|
|
|
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
|

07-02-2009, 01:10 PM
|
 |
Forums Administrator
|
|
Join Date: 18 Nov 2003
Location: Derbyshire,UK
MS Office Version:Xp; 2007
Posts: 13,723
|
|
|
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.
|

07-03-2009, 03:31 AM
|
|
Registered User
|
|
Join Date: 09 Jun 2009
Location: Veldhoven, Netherlands
MS Office Version:Excel 2007
Posts: 27
|
|
|
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
|

07-04-2009, 08:22 AM
|
|
Registered User
|
|
Join Date: 09 Jun 2009
Location: Veldhoven, Netherlands
MS Office Version:Excel 2007
Posts: 27
|
|
|
Re: Duplicate rows of column(value in cell)
Has anyone got an idea how to clear the error?
Thanks!
|

07-04-2009, 02:28 PM
|
 |
Forum Guru
|
|
Join Date: 20 Jun 2007
Location: The Great State of Texas
MS Office Version:2003, 2007
Posts: 18,387
|
|
|
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.
|

07-04-2009, 07:21 PM
|
|
Registered User
|
|
Join Date: 09 Jun 2009
Location: Veldhoven, Netherlands
MS Office Version:Excel 2007
Posts: 27
|
|
|
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
|

07-04-2009, 07:32 PM
|
 |
Forum Guru
|
|
Join Date: 20 Jun 2007
Location: The Great State of Texas
MS Office Version:2003, 2007
Posts: 18,387
|
|
|
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.
|

07-05-2009, 04:24 AM
|
|
Registered User
|
|
Join Date: 09 Jun 2009
Location: Veldhoven, Netherlands
MS Office Version:Excel 2007
Posts: 27
|
|
|
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
|

07-05-2009, 12:21 PM
|
 |
Forum Guru
|
|
Join Date: 20 Jun 2007
Location: The Great State of Texas
MS Office Version:2003, 2007
Posts: 18,387
|
|
|
re: Delete duplicates
What result is returned by the formula?
__________________
Entia non sunt multiplicanda sine necessitate.
|

07-05-2009, 02:57 PM
|
|
Registered User
|
|
Join Date: 09 Jun 2009
Location: Veldhoven, Netherlands
MS Office Version:Excel 2007
Posts: 27
|
|
|
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".
|

07-05-2009, 04:23 PM
|
 |
Forum Guru
|
|
Join Date: 20 Jun 2007
Location: The Great State of Texas
MS Office Version:2003, 2007
Posts: 18,387
|
|
|
re: Delete duplicates
Post a workbook.
__________________
Entia non sunt multiplicanda sine necessitate.
|

07-05-2009, 04:35 PM
|
|
Registered User
|
|
Join Date: 09 Jun 2009
Location: Veldhoven, Netherlands
MS Office Version:Excel 2007
Posts: 27
|
|
|
re: Delete duplicates
I've attached the zipfile, it included the xlsm. I'll send the password by PM.
|

07-05-2009, 06:32 PM
|
 |
Forum Guru
|
|
Join Date: 20 Jun 2007
Location: The Great State of Texas
MS Office Version:2003, 2007
Posts: 18,387
|
|
|
re: Delete duplicates
VLB!B17 is empty.
__________________
Entia non sunt multiplicanda sine necessitate.
|

07-05-2009, 09:22 PM
|
|
Registered User
|
|
Join Date: 09 Jun 2009
Location: Veldhoven, Netherlands
MS Office Version:Excel 2007
Posts: 27
|
|
|
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
|

07-06-2009, 12:30 AM
|
 |
Forum Guru
|
|
Join Date: 20 Jun 2007
Location: The Great State of Texas
MS Office Version:2003, 2007
Posts: 18,387
|
|
|
re: Delete duplicates
How about posting a correct example, mon frere?
__________________
Entia non sunt multiplicanda sine necessitate.
|
 |
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|