Formula, vba, or logic to remove certain occurrences

horees

Board Regular
Joined
Sep 25, 2012
Messages
63
All,
I'm looking for a Formula, vba, or logic to remove certain occurrences of a pattern.

To make life easier, please see the table below.

I want A formula or logic to calculate the column D as shown below. The idea is to get as minimum of records that show x, zz, y, ww without repition.

i.e. to be able to identify the records of (x,zz) and (y,ww).

too hard???

Thank you

ABCD
6/25XZZ1
6/25XWW0
6/25YZZ0
6/25YWW1

<TBODY>
</TBODY>
 
There are actually an "if" in there that does nothing... but I forgot to remove it... The If n > 0 doesnt do anything...
 
Upvote 0

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
There are actually an "if" in there that does nothing... but I forgot to remove it... The If n > 0 doesnt do anything...

I tried it on the provided example, it worked well, but after deleting the unneccesary rows, it gives the error "application-defined or object-defined error in this line
c = MyTable.Cells(a - 1, 1).Value

However, when I tried it on real data, it deleted some records that shouldn't be deleted...

can we do highlight entire row with color instead of deleting to be able to understand what is wrong?

Thank you

it looks like we are too close :)
 
Upvote 0
def so.. but unfortunatly i have to go to bed now... (more or less there)... i have stuff to do tomorow morning.. but ill go at it again when i get back home... unless you solve it yourself in the meanwhile :)


Best regards
 
Upvote 0
def so.. but unfortunatly i have to go to bed now... (more or less there)... i have stuff to do tomorow morning.. but ill go at it again when i get back home... unless you solve it yourself in the meanwhile :)


Best regards

Sure thing, thanks though...very helpful...waiting for your coming back!
 
Upvote 0
You may just try a helper column and some nested if statements. Sometimes the simple method is the best route.
 
Last edited:
Upvote 0
Try this formula:
Code:
=IF(AND(B2="XX",C2="XX"), "MATCH", IF(AND(B2="YY",C2="YY"), "MATCH", IF(AND(B2="XX",C2="YY"), "MATCH", IF(AND(B2="YY",C2="XX"), "MATCH", "NO MATCH"))))
 
Last edited:
Upvote 0
If you want to delete the items that don't match you could also use this vba:
Rich (BB code):
Sub DelRowsNoMatch()
'Deletes rows based on the value "NO MATCH" in column D.  The values are determined after the formula is auto-populated in column D.
Dim lr As Long, i As Long

lr = Range("A" & Rows.Count).End(xlUp).Row

Range("D2:D" & lr).Formula = "=IF(AND(B2=""XX"",C2=""XX""), ""MATCH"", IF(AND(B2=""YY"",C2=""YY""), ""MATCH"", IF(AND(B2=""XX"",C2=""YY""), ""MATCH"", IF(AND(B2=""YY"",C2=""XX""), ""MATCH"", ""NO MATCH""))))"

For i = lr To 1 Step -1
    If Range("D" & i).Value = "NO MATCH" Then Rows(i).Delete
Next i

End Sub
 
Upvote 0
If you want to delete the items that don't match you could also use this vba:
Rich (BB code):
Sub DelRowsNoMatch()
'Deletes rows based on the value "NO MATCH" in column D.  The values are determined after the formula is auto-populated in column D.
Dim lr As Long, i As Long

lr = Range("A" & Rows.Count).End(xlUp).Row

Range("D2:D" & lr).Formula = "=IF(AND(B2=""XX"",C2=""XX""), ""MATCH"", IF(AND(B2=""YY"",C2=""YY""), ""MATCH"", IF(AND(B2=""XX"",C2=""YY""), ""MATCH"", IF(AND(B2=""YY"",C2=""XX""), ""MATCH"", ""NO MATCH""))))"

For i = lr To 1 Step -1
    If Range("D" & i).Value = "NO MATCH" Then Rows(i).Delete
Next i

End Sub


The problem is that the values XX, YY, WW and ZZ aint static values.. they can be anything... and not even the same from day to day...

Best regards

Leandrial
 
Upvote 0
I tried it on the provided example, it worked well, but after deleting the unneccesary rows, it gives the error "application-defined or object-defined error in this line
c = MyTable.Cells(a - 1, 1).Value

However, when I tried it on real data, it deleted some records that shouldn't be deleted...

can we do highlight entire row with color instead of deleting to be able to understand what is wrong?

Thank you

it looks like we are too close :)

Hi again

Ya ill modify it so that it changes colour... Could you post the copy of the real data (ofc only if its not confidential)... would help for deducting the rules needed for the code to work correctly...


Best regards

leandrial
 
Upvote 0
Here the code with the color changes...

It no longer deletes the rows we dont wanna keep.. only color them red

Code:
Sub RemoveRows()


Dim MyTable As Range
Dim ws As Worksheet
Dim RowCount As Integer
Dim b As String
Dim c As String
Dim x1 As String
Dim x2 As String
Dim x3 As String
Dim x4 As String
Dim x5 As String
Dim x6 As String
Dim x7 As String
Dim x8 As String
Dim n As Integer




Set ws = ActiveWorkbook.Worksheets("Sheet1")  'change Sheet1 to whatever the name of the worksheet
Set MyTable = ws.Range("Sample") 'change Sampe to whatever the name of the range




RowCount = MyTable.Rows.Count


For a = RowCount To 1 Step -1


    b = MyTable.Cells(a, 1).Value
    c = MyTable.Cells(a - 1, 1).Value
    
    If b = c Then
    
        n = n + 1
       
    Else
    
        If n = 3 Then
            
                x1 = MyTable.Cells(a, 2).Value
                x2 = MyTable.Cells(a, 3).Value
                x3 = MyTable.Cells(a + 1, 2).Value
                x4 = MyTable.Cells(a + 1, 3).Value
                x5 = MyTable.Cells(a + 2, 2).Value
                x6 = MyTable.Cells(a + 2, 3).Value
                x7 = MyTable.Cells(a + 3, 2).Value
                x8 = MyTable.Cells(a + 3, 3).Value
                
                If x1 = x2 And x3 = x4 Then
                
                    With MyTable.Rows(a + 3).Interior
                        .Pattern = xlSolid
                        .PatternColorIndex = xlAutomatic
                        .Color = 255
                        .TintAndShade = 0
                        .PatternTintAndShade = 0
                    End With
                    
                    With MyTable.Rows(a + 2).Interior
                        .Pattern = xlSolid
                        .PatternColorIndex = xlAutomatic
                        .Color = 255
                        .TintAndShade = 0
                        .PatternTintAndShade = 0
                    End With
                                    
                    n = 0
                
                Else
                
                    With MyTable.Rows(a + 2).Interior
                        .Pattern = xlSolid
                        .PatternColorIndex = xlAutomatic
                        .Color = 255
                        .TintAndShade = 0
                        .PatternTintAndShade = 0
                    End With
                    
                    With MyTable.Rows(a + 1).Interior
                        .Pattern = xlSolid
                        .PatternColorIndex = xlAutomatic
                        .Color = 255
                        .TintAndShade = 0
                        .PatternTintAndShade = 0
                    End With
                
                    n = 0
                    
                End If
            
        End If
    End If


Next


End Sub


Best regrads

Leandrial
 
Upvote 0

Forum statistics

Threads
1,215,102
Messages
6,123,097
Members
449,096
Latest member
provoking

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