macro deleting 3 out of 4 in any combo

excelNewbie22

Well-known Member
Joined
Aug 4, 2021
Messages
510
Office Version
  1. 365
Platform
  1. Windows
hi

how can i delete all rows containing 3 out of 4 predefined numbers ?

for example, predefined numbers are:
1 2 3 4
and i want to delete any row containing any combo of 3 out of these 4 ?
like:
1235 or 4128 or 6341 and so on
(without repeats of one the numbers like 8332=keep)

1 2 3 7 delete
5 6 2 7 keep
5 2 1 3 delete
6 7 2 4 keep
1 3 5 2 delete
4 5 2 3 delete
2 2 1 5 keep
1 3 5 5 keep
8 3 3 2 keep


sheet1
ABCDE
11237delete
25627keep
35213delete
46724keep
51352delete
64523delete
72215keep
81355keep
98332keep
sheet1
 
Don't duplicate the numbers in the variable aar2:
VBA Code:
 arr2 = Array("5", "3", "4")
 
Upvote 0

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
like my user name, i tried to understand and tried this:
arr2 = Array("5", "3", "4")
For r = UBound(arr1) To 1 Step -1
Val = Join(Array(arr1(r, 5) & arr1(r, 5) & arr1(r, 3) & arr1(r, 4)), ",")
but same error occurred

but also, and please forgive me at advanced but...
i rechecked my data and if the predefined numbers doesn't contain duplicates like 1-2-3-4
so i need the macro wouldn't delete lines with combinations of 3 numbers with duplicates like 1-2-2-5 or 3-4-7-3
but
when the predefined numbers contain some duplicate, like 5-5-4-3
i need the macro to delete only combo's of 3's containing the duplicate number (5 for this example) like 5-5-3-8 or 5-1-3-5
but won't delete combo's like 4-4-3-7 or 3-3-5-1

THANK YOU for bearing with me!
 
Upvote 0
ok
so i understood how to change the macro
but still with the other problem i wrote in my last message?
 
Upvote 0
Give this a try:
VBA Code:
Sub deleteRows()
    Application.ScreenUpdating = False
    Dim LastRow As Long, arr1 As Variant, arr2 As Variant, r As Long, x As Long, i As Long, Val As String
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    arr1 = Range("A1", Range("A" & Rows.Count).End(xlUp)).Resize(, 4).Value
    arr2 = Array("1", "2", "3", "4")
    For r = UBound(arr1) To 1 Step -1
        Val = Join(Array(arr1(r, 1) & arr1(r, 2) & arr1(r, 3) & arr1(r, 4)), ",")
        For i = LBound(arr2) To UBound(arr2)
            x = x + WorksheetFunction.CountIf(Range("A" & r).Resize(, 4), arr2(i))
        Next i
        If x >= 3 Then
            Rows(r).Delete
        End If
        x = 0
        Val = ""
    Next r
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
thanks but it's still delete the wrong lines
like: 1-1-1-1
1-1-1-2
2-4-5-4
and a lot more
maybe tell me how can i, if i can, help? maybe i don't explaining too well?
 
Upvote 0
Let's try this one more time. Post updated data that contains examples of as many combinations as possible. It's hard to suggest a solution if we can't see what all the data looks like.
 
Upvote 0
thanks for keep trying,
so, coulmn e is for predefined numbers 1-2-3-4 and any combo's of 3's need to delete
and any combo of 3's in this case which include a duplicate number from the set (1234) like 1-1-2-5 can't be deleted

and coumn f is for predefined numbers 5-5-4-3 and any combo's of 3's need to delete
but the thing with 5543 is there a duplicate number which is '5', so any combo of 3's in this case will allways include a '5'
(554x 5x53 x435 when x could be any number... and so on)

and maybe still use an array for me to able to change after if needs to?

y=keep
x=delete

p.s
the original file only include numbers from a:d
and the macro should handle only one predefined numbers
and the above demonstrations are only for example ot two set's



abc.xlsx
ABCDEF
11111yy
21112yy
31125yy
41218yy
51221yy
61222yy
71317yy
81341xy
91352xy
102111yy
112112yy
122347xy
132348xy
142354xx
152435xx
162551yy
172553yx
182581yy
192582yy
202622yy
212623yy
222812yy
233132xy
243242yy
253335yy
263531yy
273532yy
284134xy
294151yy
304415yy
314518yy
324557yx
335121yy
345122yy
355332yy
365527yy
376132xy
386133yy
396216yy
406217yy
416462yy
426526yy
436527yy
447113yy
457234xy
467334yy
477441yy
487442yy
497655yy
507656yy
518111yy
528121yy
538122yy
548222yy
558223yy
568345xx
578543yx
588553yx
598554yx
608711yy
618834yy
e-1234+f-5543
 
Upvote 0
This should work for 1,2,3,4.
VBA Code:
Sub deleteRows()
    Application.ScreenUpdating = False
    Dim arr1 As Variant, arr2 As Variant, r As Long, c As Long, i As Long
    arr1 = Range("A1", Range("A" & Rows.Count).End(xlUp)).Resize(, 4).Value
    arr2 = Array("1", "2", "3", "4")
    With CreateObject("scripting.dictionary")
        For r = UBound(arr1) To 1 Step -1
            For c = 1 To UBound(arr1, 2)
                For i = LBound(arr2) To UBound(arr2)
                    If arr2(i) = CStr(arr1(r, c)) Then
                        If Not .exists(arr2(i)) Then
                            .Add arr2(i), Nothing
                        End If
                    End If
                Next i
            Next c
            If .Count = 3 Then
                Rows(r).Delete
            End If
            .RemoveAll
I couldn't get anything to work for 3,4,5,5.  What happens if you want to use 3,3,4,5?  I'll try again and if I get anything that works, I'll get back to you.
 
Upvote 0
Oops!
VBA Code:
Sub deleteRows()
    Application.ScreenUpdating = False
    Dim arr1 As Variant, arr2 As Variant, r As Long, c As Long, i As Long
    arr1 = Range("A1", Range("A" & Rows.Count).End(xlUp)).Resize(, 4).Value
    arr2 = Array("1", "2", "3", "4")
    With CreateObject("scripting.dictionary")
        For r = UBound(arr1) To 1 Step -1
            For c = 1 To UBound(arr1, 2)
                For i = LBound(arr2) To UBound(arr2)
                    If arr2(i) = CStr(arr1(r, c)) Then
                        If Not .exists(arr2(i)) Then
                            .Add arr2(i), Nothing
                        End If
                    End If
                Next i
            Next c
            If .Count = 3 Then
                Rows(r).Delete
            End If
            .RemoveAll
        Next r
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,175
Messages
6,129,312
Members
449,499
Latest member
HockeyBoi

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