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
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
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)
            If UBound(Split(Val, arr2(i))) = 1 Then
                x = x + 1
            End If
        Next i
        If x >= 3 Then
            Rows(r).Delete
        End If
        x = 0
        Val = ""
    Next r
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Another approach.

VBA Code:
Sub DeleteRows()
 Dim r As Long
  Application.ScreenUpdating = False
  For r = Cells(Rows.Count, 1).End(3).Row To 1 Step -1
   If Evaluate("COUNTIF(" & Cells(r, 1).Resize(, 4).Address & ","">4"")") = 1 And _
    Evaluate("SUM(1/COUNTIF(" & Cells(r, 1).Resize(, 4).Address & "," & Cells(r, 1).Resize(, 4).Address & "))") = 4 Then Rows(r).Delete
  Next r
End Sub
 
Upvote 0
thank you both
but none is working
both of them leaving me with
combo's of 1-1-2-3 or 1-1-3-4 or 1-1-3-2
and that's just from the start of a 4k lines/rows

the meanwhile i thought if i need to change the predefined numbers to something like 1-3-4-4 or 5-2-2-3?

any combo will include duplicate numbers like 3-4-4-5 or 1-4-4-6 or 5-2-2-7
so i know i wrote down
(without repeats of one the numbers like 8332=keep)
i take it back, cause i will need it to include even duplicate/repeated numbers like the the example above
and the ability to change the numbers ofcourse

sorry for the inconvenience and appreciate your help ?
 
Upvote 0
Please post an updated data sample.
 
Upvote 0
the red color is just to emphasis the combo's of 3's repeated/duplicated numbers from the predefined numbers
and yellow for emphasis the examples for the one's to keep

Copy.xlsx
ABCDEF
13445deletefor 1-2-3-4
21446deletefor 1-2-3-4
35227keepfor 1-2-3-4
41123deletefor 1-2-3-4
51135deletefor 1-2-3-4
68311deletefor 1-2-3-4
78313deletefor 1-2-3-4
81136deletefor 1-2-3-4
94312keepfor 2-3-5-5
105315deletefor 2-3-5-5
112258deletefor 2-3-5-5
copy
 
Upvote 0
I've split the predefined numbers from column F to G:J.

Pasta1
ABCDEFGHIJK
13445deletefor 1-2-3-41234
21446deletefor 1-2-3-41234
35227keepfor 1-2-3-41234
41123deletefor 1-2-3-41234
51135deletefor 1-2-3-41234
68311deletefor 1-2-3-41234
78313deletefor 1-2-3-41234
81136deletefor 1-2-3-41234
94312keepfor 2-3-5-52355
105315deletefor 2-3-5-52355
112258deletefor 2-3-5-52355
12
Plan1


VBA Code:
Sub DeleteRowsV2()
 Dim r As Long
  Application.ScreenUpdating = False
  For r = Cells(Rows.Count, 1).End(3).Row To 1 Step -1
   If Evaluate("=SUMPRODUCT(COUNTIF(" & Cells(r, 1).Resize(, 4).Address & "," & Cells(r, 7).Resize(, 4).Address & "))") > 2 Then Rows(r).Delete
  Next r
End Sub
 
Upvote 0
i don't understand
it's works for the sample i gave above
but when i run it in the big file=4k lines, it delete nothing!

also, i gave 2 predefined numbers (1234 and 2355) only to demonstrate what i need
(like combo's of 235 and 355 or 552 and so on)
the macro need to delete only combinations of one predefined numbers
which i need be able to change every time
and in this macro i don't understand how

not to even suggest what to do, but maybe with an array? (learned this term lately)
for me to manage and change

sorry if i'm troubling you guys with this nugget
 
Upvote 0
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 + UBound(Split(Val, 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
work like a charm!
but when i tried change it, for example to : 5-5-3-4
in those 2 lines:

Code:
 arr2 = Array("5", "5", "3", "4")

Code:
        Val = Join(Array(arr1(r, 5) & arr1(r, 5) & arr1(r, 3) & arr1(r, 4)), ",")


it returned an error saying: run time error 9 subscript out of range

can you please maybe fix it so i can change it to any 4 numbers without a hitch ?

edit:
debug coloring this line:
Val = Join(Array(arr1(r, 5) & arr1(r, 5) & arr1(r, 3) & arr1(r, 4)), ",")
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,930
Members
449,094
Latest member
teemeren

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