how to remove duplicate row based on a column value
I have below sheet where column B has server names repeated two times, some time many times or not repeated at all. Corresponding column C has size which could be same or less or different number or all have same number. I want to delete all dups row leaving behind one row which has bigger number in Column C or same ( If all has same number). I tried to loop thru with below code
Option Explicit
Sub removeDups()
Dim NumberOfValues, counter As Integer
Dim name, foundname As String
Dim value1 As Long
Dim i As Long
NumberOfValues = ThisWorkbook.Sheets("Sheet1").Range("B1").End(xlDown).Row
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
For i = 1 To NumberOfValues
name = Range("B" & i).Value
value1 = Range("C" & i).Value
foundname = True
counter = 1
If counter > 1 Then
'don't know how to loop'
End If
Next
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
I don't think you need VBA for this. You can just use the standard Remove Duplicates functionality.
Sort your data first to ensure the Value 1 column is sorted descending.
Now remove the duplicates being careful to only compare the first two columns ...
The end result should be what you need ...
You can accomplish this in different ways:
Excel 365:
You may benefit fron UNIQUE and MAXIFS:
Formula in E3 is
=UNIQUE(A1:B26)
and formula in G3 is
=MAXIFS($C$1:$C$26;$B$1:$B$26;F3;$A$1:$A$26;E3)
Then copy/paste as values and delete original data
Any Excel Version:
You can use Pivot Tables to get the range you want and copy/paste. Just create Pivot Table, Columns A and B into rows section and Column C into Values section and Choose MAX instead of SUM
VBA
Sub test()
Dim LR As Long
Dim IR As Long
Dim i As Long
Dim MyDict As Object
Dim MyKey As Variant
Set MyDict = CreateObject("Scripting.Dictionary")
LR = Range("B" & Rows.Count).End(xlUp).Row
IR = 2 'initial row of data
For i = IR To LR Step 1
If Not MyDict.Exists(Range("B" & i).Value) Then
MyDict.Add Range("B" & i).Value, Evaluate("SUMPRODUCT(MAX((B" & IR & ":B" & LR & "=""" & Range("B" & i).Value & """)*C" & IR & ":C" & LR & "))")
End If
Next i
'destiny of new data
'as example, we start pasting data in row 2 column E
i = IR
For Each MyKey In MyDict
Range("E" & i).Value = MyKey
Range("F" & i).Value = MyDict(MyKey)
i = i + 1
Next MyKey
MyDict.RemoveAll
Set MyDict = Nothing
'rest of code to delete data or whatever
End Sub