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.
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
Try this, your range definition looks wrong
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 01: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 Tips & Solutions, free examples and tutorials why not check out my downloads
New members please read & follow the Forum Rules
Remember to mark your questions Solved and rate the answer(s)
Developer gave me a few errors:I believe I fixed the errors, but now it gives me a 1004:
- the 'End With' was missing
- there was one closing bracket too many
application-defined or object-defined error. The line, with an asterisk in front of it, is the line which gives the error.
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
Has anyone got an idea how to clear the error?
Thanks!
Try this (untested):
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
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,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
Last edited by Lithium; 07-04-2009 at 07:21 PM. Reason: removed qiuote for readability
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):
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 09:13 PM.
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
Actually, B17 contains this formula:"XML" is a sheet name and A17 is the string to match.=SUBSTITUTE(ADDRESS(1;MATCH(A17;XML!1:1;0);4);1;"")
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
What result is returned by the formula?
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
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".
Post a workbook.
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
I've attached the zipfile, it included the xlsm. I'll send the password by PM.
VLB!B17 is empty.
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
Okay, that's not right. It should be on the XML sheet, B17. The sheets got mixed up (purple). The semi-correct code:Now it gives me an invalid qualifier (red)... I guess I'd buy the book VBA for Dummies or somethingSub 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![]()
Last edited by Lithium; 07-05-2009 at 09:25 PM. Reason: readability
How about posting a correct example, mon frere?
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks