Delete for 10000 rows if the item contains the same value for two adjacent cells

KalilMe

Active Member
Joined
Mar 5, 2021
Messages
343
Office Version
  1. 2016
Platform
  1. Windows
Hi
I want to delete entire row for any item in column B contains the same values for adjacent cells into columns C,D .
I have about 10000 rows in my file
color.xlsm
ABCD
1ITEMBRPURSALES
21VEG TOMATO AA122300
32VEG ONION AA16120
43FR BANANA TTQ130020
54FR BANANA MNB1200
65VEG TOMATO AA2339339
76VEG ONION AA250
87FR BANANA TTQ2200100
98FR BANANA MNB22010
109VEG TOMATO AA31010
SHM



should be

color.xlsm
ABCD
1ITEMBRPURSALES
21VEG TOMATO AA122300
32VEG ONION AA16120
43FR BANANA TTQ130020
54FR BANANA MNB1200
65VEG ONION AA250
76FR BANANA TTQ2200100
87FR BANANA MNB22010
OUTCOME


please just see rows 6,10 before and after
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
You may see some shorter codes but this one should be very fast for your 10,000 rows

VBA Code:
Sub Del_Matches()
  Dim a As Variant, b As Variant
  Dim nc As Long, i As Long, k As Long
 
  nc = Cells.Find(What:="*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
  a = Range("C2:D" & Range("A" & Rows.Count).End(xlUp).Row).Value
  ReDim b(1 To UBound(a), 1 To 1)
  For i = 1 To UBound(a)
    If a(i, 1) = a(i, 2) Then
      b(i, 1) = 1
      k = k + 1
    End If
  Next i
  If k > 0 Then
    Application.ScreenUpdating = False
    With Range("A2").Resize(UBound(a), nc)
      .Columns(nc).Value = b
      .Sort Key1:=.Columns(nc), Order1:=xlAscending, Header:=xlNo
      .Resize(k).EntireRow.Delete
    End With
    Application.ScreenUpdating = True
  End If
End Sub
 
Upvote 0
I have tested with 10,000 rows and came the results immerdiately
VBA Code:
Option Explicit
Sub del()
Dim lr&
lr = Cells(Rows.Count, "B").End(xlUp).Row
Range("A2:A" & lr).Value = Evaluate(1 & "/(" & Range("C2:C" & lr).Address & "-" & Range("D2:D" & lr).Address & ")")
On Error Resume Next
Range("A2:A" & lr).SpecialCells(xlCellTypeConstants, xlErrors).EntireRow.Delete
lr = Cells(Rows.Count, "B").End(xlUp).Row
Range("A2:A" & lr).Value = Evaluate("=row(1:" & lr - 1 & ")")
End Sub
 
Upvote 0
I have tested with 10,000 rows and came the results immerdiately
It is all relative I guess and depend on machines, how many rows to delete and in how many disjoint areas. Also if the data involves formulas that have to recalculate.
For my testing on identical data, though both were 'fairly' fast yours took just over 25 times as long as mine (1.055 secs v 0.039 secs)

I did note that your code re-numbers column A as shown in post #1 but since that wasn't specifically mentioned in the request I didn't do it, but of course that could easily be added.
 
Upvote 0
Another possibility to try (have not timed it).
Change references, like Sheet names and Ranges, as required.
Code:
Sub Maybe_So()
Dim oldArr, NewArr, i As Long, j As Long, k As Long
oldArr = Sheets("Sheet2").Range("A2:D" & Sheets("Sheet2").Cells(Rows.Count, 2).End(xlUp).Row).Value
ReDim NewArr(1 To UBound(oldArr), 1 To 4)
k = 0
    For i = LBound(oldArr) To UBound(oldArr)
        If oldArr(i, 3) <> oldArr(i, 4) Then
        k = k + 1
        NewArr(k, 1) = k
            For j = 2 To 4
                NewArr(k, j) = oldArr(i, j)
            Next j
        End If
    Next i
Sheets("Sheet2").Cells(2, 1).Resize(UBound(NewArr), 4) = NewArr
End Sub

And it re-numbers Column A as shown in Post #1, 2nd Picture (tongue firmly in cheek)
 
Last edited:
Upvote 0
@jolivanes, I thought it was interesting that your code did not reset the UsedRange to the size of the new data set.
This might be an issue if there are a large number of rows being deleted or if subsequent code relies on the UsedRange,

It seems that the formatting not being cleared from the empty rows is the culprit.

Adding the below 2 lines to the bottom of your code seems to resolve the issue.

VBA Code:
Sheets("Sheet2").Cells(2 + k, 1).Resize(UBound(NewArr) - k, 4).ClearFormats
Sheets("Sheet2").UsedRange
 
Upvote 0
first sorry about delaying for you , that's because of test for each code individually and see the discussion among you .
second thanks guys for all of codes
@Peter_SSs fantastic ! I tested your code gives running speed 0.16 sec
I did note that your code re-numbers column A as shown in post #1 but since that wasn't specifically mentioned in the request I didn't do it, but of course that could easily be added.
sorry if I don't mentioned that. I thought the picture can be clear. yes shoud re-numbers column A as shown in post #1
I want showing data in second sheet with keep the orginal data in first sheet . I try modifying in this line
VBA Code:
    With sheets("outcome").Range("A2").Resize(UBound(a), nc)
but doesn't work , also somtimes gives error block variable in this line
VBA Code:
  nc = Cells.Find(What:="*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
seem the first sheet should be active , sometimes I select sheet2 accidentally . I hope fixing this problem
 
Upvote 0
@bebo021999 great without loop !
you have very slight advantage in speed with comparison Peter's code . it gives 0.15
so I want showing data in second sheet with keep the orginal data in first sheet .
 
Upvote 0

Forum statistics

Threads
1,216,171
Messages
6,129,279
Members
449,498
Latest member
Lee_ray

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