nplano1
New Member
- Joined
- Sep 6, 2014
- Messages
- 22
HELP FINDING DUPLICATES AND DELETEING THE WHOLE ROW ITS IN
I have a database that contains somewhere between 7 and 10 thousand records all in column A and every month I download a data set that contains between 70 and 100 thousand Records, this goes from column A through column L.
I want to check column A in my database against column A in the dataset that I downloaded and if there are any duplicates I wanted to delete the whole row in the dataset that I just downloaded not the database and at the end of it I would like to know how many duplicates were removed.
This is what I tryed
Sub RemovePhoneNumbers()
Dim WSL As Worksheet
Dim WSD As Worksheet
Dim WBT As Workbook
Dim WBD As Workbook
Dim iRange As Range
Set WBT = ThisWorkbook
Set WBD = ActiveWorkbook
If WBT.Name = WBD.Name Then
MsgBox "Switch to the data worksheet first. Then run the macro."
Exit Sub
End If
Set WSD = ActiveSheet
Set up the criteria range
Set WSL = Worksheets("NUMBERS TO TAKE OUT")
FinalRow = WSL.Cells(Rows.Count, 1).End(xlUp).Row
WSL.Cells(1, 1).Resize(FinalRow, 1).Name = "OutList"
Set up the input range
FinalRow = WSD.Cells(Rows.Count, 1).End(xlUp).Row
FinalCol = WSD.Cells(1, Columns.Count).End(xlToLeft).Column
Set iRange = WSD.Cells(1, 1).Resize(FinalRow, FinalCol)
Make sure the criteria range is correct
For i = 1 To 1
HdFound = False
ThisHeading = WSL.Cells(1, i).Value
For j = 1 To FinalCol
If WSD.Cells(1, j).Value = ThisHeading Then
HdFound = True
Exit For
End If
Next j
If Not HdFound Then
Msg = "Nick - the Numbers to Remove worksheet has a heading in column " & i & " of " & ThisHeading & ". This needs to match a heading in row 1 of " & WSD.Name & "."
MsgBox Msg
Exit Sub
End If
Next i
Find the items to Remove
iRange.AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
Range("OutList"), Unique:=False
FoundCount = iRange.Columns(1).SpecialCells(xlCellTypeVisible).Count - 1
If FoundCount = 0 Then
WSD.ShowAllData
MsgBox "Nothing found to delete."
Exit Sub
End If
WSD.Cells(2, 1).Resize(FinalRow - 1, 1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
WSD.ShowAllData
WSD.Cells(1, 1).Select
MsgBox "Removed " & FoundCount & " rows."
End Sub
I have a database that contains somewhere between 7 and 10 thousand records all in column A and every month I download a data set that contains between 70 and 100 thousand Records, this goes from column A through column L.
I want to check column A in my database against column A in the dataset that I downloaded and if there are any duplicates I wanted to delete the whole row in the dataset that I just downloaded not the database and at the end of it I would like to know how many duplicates were removed.
This is what I tryed
Sub RemovePhoneNumbers()
Dim WSL As Worksheet
Dim WSD As Worksheet
Dim WBT As Workbook
Dim WBD As Workbook
Dim iRange As Range
Set WBT = ThisWorkbook
Set WBD = ActiveWorkbook
If WBT.Name = WBD.Name Then
MsgBox "Switch to the data worksheet first. Then run the macro."
Exit Sub
End If
Set WSD = ActiveSheet
Set up the criteria range
Set WSL = Worksheets("NUMBERS TO TAKE OUT")
FinalRow = WSL.Cells(Rows.Count, 1).End(xlUp).Row
WSL.Cells(1, 1).Resize(FinalRow, 1).Name = "OutList"
Set up the input range
FinalRow = WSD.Cells(Rows.Count, 1).End(xlUp).Row
FinalCol = WSD.Cells(1, Columns.Count).End(xlToLeft).Column
Set iRange = WSD.Cells(1, 1).Resize(FinalRow, FinalCol)
Make sure the criteria range is correct
For i = 1 To 1
HdFound = False
ThisHeading = WSL.Cells(1, i).Value
For j = 1 To FinalCol
If WSD.Cells(1, j).Value = ThisHeading Then
HdFound = True
Exit For
End If
Next j
If Not HdFound Then
Msg = "Nick - the Numbers to Remove worksheet has a heading in column " & i & " of " & ThisHeading & ". This needs to match a heading in row 1 of " & WSD.Name & "."
MsgBox Msg
Exit Sub
End If
Next i
Find the items to Remove
iRange.AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
Range("OutList"), Unique:=False
FoundCount = iRange.Columns(1).SpecialCells(xlCellTypeVisible).Count - 1
If FoundCount = 0 Then
WSD.ShowAllData
MsgBox "Nothing found to delete."
Exit Sub
End If
WSD.Cells(2, 1).Resize(FinalRow - 1, 1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
WSD.ShowAllData
WSD.Cells(1, 1).Select
MsgBox "Removed " & FoundCount & " rows."
End Sub