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
 
sorry to say, after all your trying...
still no good
from the little bit i check,
it kept
4-1-2-3
4-2-3-1
4-2-1-3
and maybe more
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
can anyone please try and help me out? for summarize i'll copy in here the main problem:

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

range is always a:d for unknown numbers of rows
in here i'm writing two examples,
but the macro should handle only one example=predefined numbers:

example 1:
column e is for predefined numbers 1-2-3-4 and any combo's of 3 unique numbers need to be deleted
like 1-2-3-5 or 4-2-3-8 but not when one of the three is a duplicate one like 1-2-3-3

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

and maybe use an array for me to be able to change when needs to?

y=keep
x=delete
example.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
304132xy
314415yy
324518yy
334557yx
345121yy
355122yy
365332yy
375527yy
386132xy
396133yy
406216yy
416217yy
426462yy
436526yy
446527yy
457113yy
467234xy
477334yy
487441yy
497442yy
507655yy
517656yy
528111yy
538121yy
548122yy
558222yy
568223yy
578345xx
588543yx
598553yx
608554yx
618711yy
628834yy
e-1234+f-5543
 
Upvote 0
Try this version for any array of 4 numbers:
VBA Code:
Sub deleteRows()
    Application.ScreenUpdating = False
    Dim arr1 As Variant, arr2 As Variant, r As Long, c As Long, i As Long, Val As String, Val2 As String
    arr1 = Range("A1", Range("A" & Rows.Count).End(xlUp)).Resize(, 4).Value
    arr2 = Array("3", "4", "5", "5")
    For r = UBound(arr1) To 1 Step -1
        Val = Join(Array(arr1(r, 1) & arr1(r, 2) & arr1(r, 3) & arr1(r, 4)), ",")
        Val2 = Join(Array(arr2(0) & arr2(1) & arr2(2) & arr2(3)), ",")
        If InStr(1, Val, arr2(0)) > 0 Then
            x = x + 1
        End If
        If InStr(1, Val, arr2(1)) > 0 Then
            x = x + 1
        End If
        If InStr(1, Val, arr2(2)) > 0 Then
            x = x + 1
        End If
        If InStr(1, Val, arr2(3)) > 0 Then
            x = x + 1
        End If
        If x >= 3 Then
            Rows(r).Delete
        End If
        x = 0
    Next r
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
G = 1st question, like column E
Map1
ABCDEFG
11111yyokay
21112yyokay
31125yyokay
41218yyokay
51221yyokay
Blad1
Cell Formulas
RangeFormula
G1:G5G1=IF((MAX(COUNTIF(A1:D1,{1;2;3;4}))>1)+(SUM(--(COUNTIF(A1:D1,{1;2;3;4})<>0))<3),"okay","delete")
Named Ranges
NameRefers ToCells
_FilterDatabase=Blad1!$A$1:$M$62G1
 
Upvote 0
i'm not sure if i understood the 2nd rule well.
Map1
ABCDEFGH
11111yyokayokay
21112yyokayokay
31125yyokayokay
41218yyokayokay
51221yyokayokay
61222yyokayokay
71317yyokayokay
81341xyokayokay
91352xydeleteokay
102111yyokayokay
112112yyokayokay
122347xydeleteokay
132348xydeleteokay
142354xxdeletedelete
152435xxdeletedelete
162551yyokayokay
172553yxokayokay
182581yyokayokay
192582yyokayokay
202622yyokayokay
212623yyokayokay
222812yyokayokay
233132xyokayokay
243242yyokayokay
253335yyokayokay
263531yyokayokay
273532yyokayokay
284134xyokayokay
294151yyokayokay
304132xydeleteokay
314415yyokayokay
324518yyokayokay
334557yxokayokay
345121yyokayokay
355122yyokayokay
365332yyokayokay
375527yyokayokay
386132xydeleteokay
396133yyokayokay
406216yyokayokay
416217yyokayokay
426462yyokayokay
436526yyokayokay
446527yyokayokay
457113yyokayokay
467234xydeleteokay
477334yyokayokay
487441yyokayokay
497442yyokayokay
507655yyokayokay
517656yyokayokay
528111yyokayokay
538121yyokayokay
548122yyokayokay
558222yyokayokay
568223yyokayokay
578345xxokaydelete
588543yxokaydelete
598553yxokayokay
608554yxokayokay
618711yyokayokay
628834yyokayokay
Blad1
Cell Formulas
RangeFormula
G1:G62G1=IF((MAX(COUNTIF(A1:D1,{1;2;3;4}))>1)+(SUM(--(COUNTIF(A1:D1,{1;2;3;4})<>0))<3),"okay","delete")
H1:H62H1=IF((COUNTIF(A1:D1,5)>1)+(SUM(--(COUNTIF(A1:D1,{3;4;5})<>0))<3),"okay","delete")
Named Ranges
NameRefers ToCells
_FilterDatabase=Blad1!$A$1:$H$62G1:H1
 
Upvote 0
I am pretty sure this macro will do what you want (just assign the numbers to check as a dash delimited string to the CheckMe variable)...
VBA Code:
Sub DeleteIfThreeOutOfFour()
  Dim R As Long, X As Long
  Dim Arr As Variant, ArrChk As Variant
  Dim CheckMe As String, Combo As String
  
  CheckMe = "1-2-3-4"
  
  ArrChk = Split(CheckMe, "-")
  Arr = Range("A1", Cells(Rows.Count, "D").End(xlUp))
  For R = UBound(Arr) To 1 Step -1
    Combo = Join(Application.Index(Intersect(Rows(R), Columns("A:D")).Value, 1, 0), "")
    For X = 0 To 3
      Combo = WorksheetFunction.Substitute(Combo, ArrChk(X), "", 1)
    Next
    If Len(Combo) = 1 Then Rows(R).Delete
  Next
End Sub
 
Upvote 0
mumps:
it was perfect for 1-2-3-4
but for 5-5-4-3
it deleted 1-4-4-5 because it had two '4' when it shouldn't, cause 4-4-5 isn't combo of 3's
only '5' in this predefined numbers need to be searched for duplicity and deleted

BSALV:
thank you, but i need a macro, and also it wasn't right as you can see for the example i gave

Rick Rothstein:
the macro kept the line 1-2-3-4 !!
 
Upvote 0
Rick Rothstein:
the macro kept the line 1-2-3-4 !!
Then I guess I don't understand your requirement. I thought you wanted only rows where 3 of the 4 numbers existed without repeats being counted twice. Is your actual requirement to delete rows where 3 or 4 numbers exist without repeats being counted twice? If so, then give this version a try...
VBA Code:
Sub DeleteIfThreeOutOfFour()
  Dim R As Long, X As Long
  Dim Arr As Variant, ArrChk As Variant
  Dim CheckMe As String, Combo As String
 
  CheckMe = "1-2-3-4"
 
  ArrChk = Split(CheckMe, "-")
  Arr = Range("A1", Cells(Rows.Count, "D").End(xlUp))
  For R = UBound(Arr) To 1 Step -1
    Combo = Join(Application.Index(Intersect(Rows(R), Columns("A:D")).Value, 1, 0), "")
    For X = 0 To 3
      Combo = WorksheetFunction.Substitute(Combo, ArrChk(X), "", 1)
    Next
    If Len(Combo) <= 1 Then Rows(R).Delete
  Next
End Sub
 
Upvote 0
Solution
an adapted Rick-macro, but i see there were reactions, so ...
VBA Code:
Sub DeleteIfThreeOutOfFour()
     Dim R As Long
     Dim Arr As Variant, ArrChk
     Dim CheckMe As String
     Dim iSmFr, iSm3, c, a, Most_Frequent, b

     CheckMe = "1-2-3-4"
     ArrChk = Split(CheckMe, "-")
    
     Set c = Range("A1", Cells(Rows.Count, "D").End(xlUp))
     Arr = c.Value

     For R = UBound(Arr) To 1 Step -1
          iSmFr = 9E+99                                         'very high value                               '
          a = Application.Index(Arr, R, 0)                      'take 1 row
          Most_Frequent = Application.Mode_Mult(a)              'values that are most frequent, as there are only 4, that's 1 or 2 duplicates
          If Not IsError(Most_Frequent) Then iSmFr = Application.Min(Most_Frequent)     'the smallest in case of 2
          iSm3 = Application.Small(a, 3)                        'the 3rd smallest value of the 4
          b = (iSmFr <= 4) Or (iSm3 > 4)                        'a duplicate<=4 or 3rd smallest >4 then it's okay
          If Not b Then c.Cells(R, 1).EntireRow.Interior.ColorIndex = 3        'tempory : mark row red instead of deleting
     Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,013
Messages
6,122,694
Members
449,092
Latest member
snoom82

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