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

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
so I want showing data in second sheet with keep the orginal data in first sheet .
Try this then. I have assumed that sheet 'OUTCOME' as shown in post 1 does not already exist in the workbook.
This code will retain any formatting from the retained cells but would replace any formulas with their results.

VBA Code:
Sub FilterToNewSheet()
  Dim rData As Range, rCrit As Range
  
  With Sheets("SHM")
    Set rCrit = .Range("Z1:Z2")
    Set rData = .Range("A1:D" & .Range("A" & Rows.Count).End(xlUp).Row)
  End With
  rCrit.Cells(2).Formula = "=C2<>D2"
  Application.ScreenUpdating = False
  Sheets.Add(After:=Sheets("SHM")).Name = "OUTCOME"
  With Sheets("OUTCOME")
    rData.AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=rCrit, CopyToRange:=.Range("A1"), Unique:=False
    rCrit.ClearContents
    With .Range("A2", .Range("A" & Rows.Count).End(xlUp))
      .Value = Evaluate("row(" & .Address & ")-1")
    End With
    .UsedRange.Columns.AutoFit
  End With
  Applicatio
 
Upvote 0
@Peter_SSs thanks

truly first code is much better than last . the last gives me 1.08 despite of the last code using advanced filter , strange!!

this is not big problem , but I need fixing error if I run the macro continuously will give error about shee name outcome is already existed . could be updated data in sheet outcome based on update sheet SHM without shows error after create sheet OUTPUT
 
Upvote 0
truly first code is much better than last .
:confused: Not sure how the first code can be better when it doesn't move the results to a new sheet like you want?

if I run the macro continuously will give error about shee name outcome is already existed . could be updated data in sheet outcome based on update sheet SHM without shows error after create sheet OUTPUT
What do you want to happen if the code is run repeatedly?
- Delete the OUTPUT sheet if it exists and put the new data on a new OUTPUT sheet?
- Put the new data on a different new sheet each time but keep the old ones too?
- Append the new data at the bottom of the existing OUTPUT sheet? If you do this, presumably some of the data will get repeated on the OUTPUT sheet.
- Something else? Please give clear details.
 
Upvote 0
:confused: Not sure how the first code can be better when it doesn't move the results to a new sheet like you want?
the same question ask myself. I'm surprised.
- Put the new data on a different new sheet each time but keep the old ones too?
I don't need creating new sheet every time when run the macro . should be in the same sheet has been created . this means to replace data when run the macro should clear data from the first time from sheet OUPUT before bring data from first sheet until doesn't repeat copying data .
 
Upvote 0
Try this

VBA Code:
Sub FilterToOUTPUT()
  Dim rData As Range, rCrit As Range
  
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  On Error Resume Next
  Sheets("OUTPUT").Delete
  On Error GoTo 0
  Application.DisplayAlerts = True
  Sheets.Add(After:=Sheets("SHM")).Name = "OUTPUT"
  With Sheets("SHM")
    Set rCrit = .Range("Z1:Z2")
    Set rData = .Range("A1:D" & .Range("A" & Rows.Count).End(xlUp).Row)
  End With
  rCrit.Cells(2).Formula = "=C2<>D2"
  With Sheets("OUTPUT")
    rData.AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=rCrit, CopyToRange:=.Range("A1"), Unique:=False
    rCrit.ClearContents
    With .Range("A2", .Range("A" & Rows.Count).End(xlUp))
      .Value = Evaluate("row(" & .Address & ")-1")
    End With
    .UsedRange.Columns.AutoFit
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
excellent ! this is what I want .
thank you & of course the other guys for your help ;)
 
Upvote 0

Forum statistics

Threads
1,216,179
Messages
6,129,334
Members
449,503
Latest member
glennfandango

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