Delete duplicate rows while keeping the largest value and smallest value corresponding to the same name

sofas

Well-known Member
Joined
Sep 11, 2022
Messages
559
Office Version
  1. 2021
  2. 2019
Platform
  1. Windows
Hello. I have a list of duplicate names in column B looking for a way that enables me to delete duplicate rows provided only the largest value and the smallest corresponding value of the same name are kept in column E in case the number of iterations of the name exceeds more than 2 times

Capture d'écran 2024-03-17 204236.png
Capture d'écran 2024-03-17 204236.png


 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Have you tried:
Cell I1
Excel Formula:
=UNIQUE(B1:B21)
Make sure you encompass the entire range
Then in Cell L2
Excel Formula:
=MAXIFS(E:E,B:B,I2)
Fill down

That worked when I tested it.
 
Upvote 0
Have you tried:
Cell I1
Excel Formula:
=UNIQUE(B1:B21)
Make sure you encompass the entire range
Then in Cell L2
Excel Formula:
=MAXIFS(E:E,B:B,I2)
Fill down

That worked when I tested it.
Thank you, but I don't want to extract the results elsewhere. What is required is to use the VBA code to delete the rows that contain duplicate values, while keeping the smallest and largest value for each duplicate name on column b.
 
Upvote 0
Start at the bottom and work your way to the top and check to see if the value in E equals the maxifs of Range B. If not, delete, else continue.
This is some very simplistic code that will work. You can adjust it to become more versatile to suit your needs.
VBA Code:
Sub sofas()
Dim I As Integer
Dim A As Integer
Dim nme As String

For I = 21 To 2 Step -1
    nme = Range("B" & I).Value
    A = Application.WorksheetFunction.MaxIfs(Range("E:E"), Range("B:B"), nme)
    If Range("E" & I).Value < A Then
        Range("B" & I & ":E" & I).Delete
        Else
    End If
Next I
    
End Sub
 
Upvote 0
Solution
Start at the bottom and work your way to the top and check to see if the value in E equals the maxifs of Range B. If not, delete, else continue.
This is some very simplistic code that will work. You can adjust it to become more versatile to suit your needs.
VBA Code:
Sub sofas()
Dim I As Integer
Dim A As Integer
Dim nme As String

For I = 21 To 2 Step -1
    nme = Range("B" & I).Value
    A = Application.WorksheetFunction.MaxIfs(Range("E:E"), Range("B:B"), nme)
    If Range("E" & I).Value < A Then
        Range("B" & I & ":E" & I).Delete
        Else
    End If
Next I
   
End Sub
Thank you. The code has been modified to suit my request. I appreciate your effort 👍👍👍
 
Upvote 1

Forum statistics

Threads
1,223,836
Messages
6,174,922
Members
452,592
Latest member
Welshy1491

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top