If-And (?) Loop construction (?) to delete multiple rows based on multiple conditions?

Lil2606

Board Regular
Joined
Jan 29, 2019
Messages
79
Hi all,

I am struggling with this for a while... I have the following excel sheet as an example data set

Column A Column B Column C Column D
1 First names Last names Fruits Basket sizes
2 John Smith Raspberry 03 - Basket
3 John Smith Raspberry 05 - Basket
4 John Smith Raspberry 03 - Basket
5 John Smith Cherry Small Basket
6 John Smith Raspberry 03 - Basket
7 Jane Doe Raspberry 03 - Basket
8 Jane Doe Raspberry Small Basket
9 John Page Apple 03 - Basket
10 John Page Apple 03 - Basket
11 John Page Apple Small Basket
12 John Page Apple 05 - Basket
13 Grace Smith Raspberry 03 - Basket
14 Grace Smith Apple 05 - Basket
15 Grace Smith Cherry Small Basket

This is a table, so a listobject, and first row is header row (pretty obviously)
What I would like to do is write a VBA code to get rid of lines that are the same first and last name, and the same fruit, and only keep 1 line (remove duplicates/triplicates)
Either ignoring what is in Column D, or possibly, if there is an 05-Basket, that should be the line that is kept, if there is no "05*" it doesn't matter which line is kept. Also if it can't be done this way and lets say its always the first or last row of the group that is kept that is also good I will just sort it first. (Additional info, there will never be more than one 05 - Basket for the same name and fruit)
So the optimal outcome of this would be:

Column A Column B Column C Column D
1 First names Last names Fruits Basket sizes
2 John Smith Raspberry 05 - Basket
3 John Smith Cherry Small Basket
4 Jane Doe Raspberry 03 - Basket
5 John Page Apple 05 - Basket
6 Grace Smith Raspberry 03 - Basket
7 Grace Smith Apple 05 - Basket
8 Grace Smith Cherry Small Basket

I'm open to any creative solutions.. maybe a column could be inserted as "sorting column" in which the first names, last names and fruits are combined? And based on that column delete duplicates/triplicates?

If empty rows are left, that's also fine I will get rid of them after.

I really just need to figure out how to get rid of the unnecessary rows.. its weeks that I am trying to do this but I am quite new to VBA so its a bit above me yet.. hence if you have a solution, could you also give an explanation on how it does what it does please? I would like to learn.
Please help!
Thank you!
 
Super! Thank you very much!
Still don't understand how your code does what it does, I mean I get the sort part of it, but not the part how it deleted the duplicates..
Can you please explain it a little?

Cl is Column? Cell?
What is scripting.dictionary, and compare mode 1?
ValU= join (join what?)

Code:
 [COLOR=#333333]Set Rng = Cl.Resize(, 4) Else Set Rng = Union(Rng, Cl.Resize(, 4))[/COLOR]

in that (, 4) is column index? 4th column? If I have 20 is that supposed to change to 20?
 
Upvote 0

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Code:
10       With CreateObject("scripting.dictionary")
11          .CompareMode = 1
12          For Each Cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
13             ValU = join(Application.Index(Cl.Resize(, 3).Value, 1, 0), "|")
14             If Not .Exists(ValU) Then
15                .Add ValU, Nothing
16             Else
17                If Rng Is Nothing Then Set Rng = Cl.Resize(, 4) Else Set Rng = Union(Rng, Cl.Resize(, 4))
18             End If
19          Next Cl
20       End With
21       If Not Rng Is Nothing Then Rng.Delete
10) creates a dictionary see here for more info https://excelmacromastery.com/vba-dictionary/
11) sets comparemode to 1 (not case sensitive)
12) Starts to loop through each cell in column A
13) Joins (concatenates) columns A:C for each row with a | between values & assigns that value to the variable ValU
14) Checks if the value already exists in the dictionary
15) if it doesn't then add it as the Key with the Item being empty
17 ) if the value does exist set the variable Rng to be the width of your table (in this case 4 columns so resize (,4)) If Rng already has a value then set rng to be the existing range & the new range
21) If there are rows to be deleted, delete them

HTH
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
Sorry for the delay. I had out-patient surgery since my last post.

The macro approach is is fine. If you want to try a formula approach...

F3: {=INDEX(data,MATCH(0,COUNTIFS($F$2:$F2,first,$G$2:$G2,last,$H$2:$H2,fruits),0),COLUMN(A1))}

CSE, copy right and down

Note changed "Small" in data to "00"
 
Last edited:
Upvote 0

Book1
FGHIJKLMN
1WITH FORMULASEXPECTED
2FirstLastFruitsSizeFirstLastFruitsSize
3JohnSmithRasberry05JohnSmithRasberry05
4JohnPageApple05JohnSmithCherrySmall
5GraceSmithApple05JaneDoeRasberry03
6JaneDoeRasberry03JohnPageApple05
7GraceSmithRasberry03GraceSmithRasberry03
8JohnSmithCherry00GraceSmithApple05
9GraceSmithCherry00GraceSmithCherrySmall
10
11F3: {=INDEX(data,MATCH(0,COUNTIFS($F$2:$F2,first,$G$2:$G2,last,$H$2:$H2,fruits),0),COLUMN(A1))}
12
13CSE, copy right and down
14
15Note changed "Small" in data to "00"
Sheet1
Cell Formulas
RangeFormula
F11="F3: "&FORMULATEXT(F3)
F3{=INDEX(data,MATCH(0,COUNTIFS($F$2:$F2,first,$G$2:$G2,last,$H$2:$H2,fruits),0),COLUMN(A1))}
F4{=INDEX(data,MATCH(0,COUNTIFS($F$2:$F3,first,$G$2:$G3,last,$H$2:$H3,fruits),0),COLUMN(A2))}
F5{=INDEX(data,MATCH(0,COUNTIFS($F$2:$F4,first,$G$2:$G4,last,$H$2:$H4,fruits),0),COLUMN(A3))}
F6{=INDEX(data,MATCH(0,COUNTIFS($F$2:$F5,first,$G$2:$G5,last,$H$2:$H5,fruits),0),COLUMN(A4))}
F7{=INDEX(data,MATCH(0,COUNTIFS($F$2:$F6,first,$G$2:$G6,last,$H$2:$H6,fruits),0),COLUMN(A5))}
F8{=INDEX(data,MATCH(0,COUNTIFS($F$2:$F7,first,$G$2:$G7,last,$H$2:$H7,fruits),0),COLUMN(A6))}
F9{=INDEX(data,MATCH(0,COUNTIFS($F$2:$F8,first,$G$2:$G8,last,$H$2:$H8,fruits),0),COLUMN(A7))}
G3{=INDEX(data,MATCH(0,COUNTIFS($F$2:$F2,first,$G$2:$G2,last,$H$2:$H2,fruits),0),COLUMN(B1))}
G4{=INDEX(data,MATCH(0,COUNTIFS($F$2:$F3,first,$G$2:$G3,last,$H$2:$H3,fruits),0),COLUMN(B2))}
G5{=INDEX(data,MATCH(0,COUNTIFS($F$2:$F4,first,$G$2:$G4,last,$H$2:$H4,fruits),0),COLUMN(B3))}
G6{=INDEX(data,MATCH(0,COUNTIFS($F$2:$F5,first,$G$2:$G5,last,$H$2:$H5,fruits),0),COLUMN(B4))}
G7{=INDEX(data,MATCH(0,COUNTIFS($F$2:$F6,first,$G$2:$G6,last,$H$2:$H6,fruits),0),COLUMN(B5))}
G8{=INDEX(data,MATCH(0,COUNTIFS($F$2:$F7,first,$G$2:$G7,last,$H$2:$H7,fruits),0),COLUMN(B6))}
G9{=INDEX(data,MATCH(0,COUNTIFS($F$2:$F8,first,$G$2:$G8,last,$H$2:$H8,fruits),0),COLUMN(B7))}
H3{=INDEX(data,MATCH(0,COUNTIFS($F$2:$F2,first,$G$2:$G2,last,$H$2:$H2,fruits),0),COLUMN(C1))}
H4{=INDEX(data,MATCH(0,COUNTIFS($F$2:$F3,first,$G$2:$G3,last,$H$2:$H3,fruits),0),COLUMN(C2))}
H5{=INDEX(data,MATCH(0,COUNTIFS($F$2:$F4,first,$G$2:$G4,last,$H$2:$H4,fruits),0),COLUMN(C3))}
H6{=INDEX(data,MATCH(0,COUNTIFS($F$2:$F5,first,$G$2:$G5,last,$H$2:$H5,fruits),0),COLUMN(C4))}
H7{=INDEX(data,MATCH(0,COUNTIFS($F$2:$F6,first,$G$2:$G6,last,$H$2:$H6,fruits),0),COLUMN(C5))}
H8{=INDEX(data,MATCH(0,COUNTIFS($F$2:$F7,first,$G$2:$G7,last,$H$2:$H7,fruits),0),COLUMN(C6))}
H9{=INDEX(data,MATCH(0,COUNTIFS($F$2:$F8,first,$G$2:$G8,last,$H$2:$H8,fruits),0),COLUMN(C7))}
I3{=INDEX(data,MATCH(0,COUNTIFS($F$2:$F2,first,$G$2:$G2,last,$H$2:$H2,fruits),0),COLUMN(D1))}
I4{=INDEX(data,MATCH(0,COUNTIFS($F$2:$F3,first,$G$2:$G3,last,$H$2:$H3,fruits),0),COLUMN(D2))}
I5{=INDEX(data,MATCH(0,COUNTIFS($F$2:$F4,first,$G$2:$G4,last,$H$2:$H4,fruits),0),COLUMN(D3))}
I6{=INDEX(data,MATCH(0,COUNTIFS($F$2:$F5,first,$G$2:$G5,last,$H$2:$H5,fruits),0),COLUMN(D4))}
I7{=INDEX(data,MATCH(0,COUNTIFS($F$2:$F6,first,$G$2:$G6,last,$H$2:$H6,fruits),0),COLUMN(D5))}
I8{=INDEX(data,MATCH(0,COUNTIFS($F$2:$F7,first,$G$2:$G7,last,$H$2:$H7,fruits),0),COLUMN(D6))}
I9{=INDEX(data,MATCH(0,COUNTIFS($F$2:$F8,first,$G$2:$G8,last,$H$2:$H8,fruits),0),COLUMN(D7))}
Press CTRL+SHIFT+ENTER to enter array formulas.
Named Ranges
NameRefers ToCells
data=Sheet1!$A$3:$D$16
first=Sheet1!$A$3:$A$16
fruits=Sheet1!$C$3:$C$16
last=Sheet1!$B$3:$B$16
 
Upvote 0
Oh wow. Thank you for this, but I'm trying to avoid user inputs, thats why I'm doing it with VBA. This converter sheet I'm working on is for my colleagues who use excel to edit text... so I wouldn't go as far as ctrl + shift +enter with them :D
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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