Match and delete

topi1

Active Member
Joined
Aug 6, 2014
Messages
252
Office Version
  1. 2010
Need help with VBA. TY.

In the following example, I want the vba to loop through all column R cells where the column Q value is 9. Match the strings in those cells and see if they contain any of the strings in the column U. case insensitive match is fine. If the cell in the column R with column Q=9 does not contain any of the column U strings, delete it. Thank you. If the code works, last two cells should be deleted.

Book1
QRSTU
1Theater:delim1.
2Movie:delim2.
3delim3.
4delim4.
5TITLE1delim5.
6delim6.
74Eros: Dunndelim7.
84Strand shows Abba.delim8.
94Regal: Supermandelim9.
104Roxi shows Cars.delim10.
114Metro: Batman
124Minerva: Starwars
13
14
15
165Eros shows Superman nextweek.
175Metro shows Dunn next month.
185
19TITLE2
209Eros shows Superman nextweek. Delim4.
219Metro shows Dunn next month. Delim3. Closed today.
229Delim2. Minerva is showing Abba.
239Before Metro Roxi shows Cars.
249After Eros Strand shows Abba.
Sheet2
 
@Cubist The above code did not work. One of the earlier versions as follows worked. I am not capable of deciphering the difference between the recent ones and the following. TY.


VBA Code:
Sub CheckAndClear()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim i As Long, j As Long, k As Long
    Dim dataQ As Variant, dataR As Variant, dataU As Variant
    Dim foundRow As Long
    Dim foundWords() As String

    Set ws = ThisWorkbook.Sheets("Sheet2") ' Change Sheet1 to your sheet name

    lastRow = ws.Cells(ws.Rows.Count, "Q").End(xlUp).Row

    dataQ = ws.Range("Q1:Q" & lastRow).value
    dataR = ws.Range("R1:R" & lastRow).value
    dataU = ws.Range("U1:U" & ws.Cells(ws.Rows.Count, "U").End(xlUp).Row).value
    foundRow = 1
    ReDim foundWords(1 To UBound(dataR, 1), 1 To 1)

    For i = 1 To UBound(dataQ, 1)
        If dataQ(i, 1) = 9 Then
            If foundRow = 1 Then foundRow = i ' Store the first row 9
            For j = 1 To UBound(dataU, 1)
                If InStr(1, dataR(i, 1), dataU(j, 1), vbTextCompare) > 0 Then
                    foundWords(j, 1) = dataR(i, 1)
            For k = LBound(dataU) To UBound(dataU)
                foundWords(j, 1) = Trim(Replace(foundWords(j, 1), dataU(k, 1), "", , , vbTextCompare))
            Next k
                    Exit For
                End If
            Next j
            Cells(i, "R").ClearContents
        End If
    Next i

    ' Output modified strings to column R starting from foundRow
    ws.Range("R" & foundRow).Resize(UBound(foundWords), 1).value = foundWords

    ' Delete rows where no modifications were made
    For i = lastRow To 1 Step -1
        If Cells(i, "R").value = "" Then
            Rows(i).Delete
        End If
    Next i
End Sub
 
Upvote 0

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
This code doesn't have U on Sheet1 and it doesn't sort based on the Delim list. The prior code addresses both and works on my tests. I'm not sure what else.
 
Upvote 0
Thank you. I’ll use the one which is working more consistently in my work book. Thank you.
 
Upvote 0
@Cubist So far so good. The following code seems to be working consistently. Thank you. Wanted to give you follow up since you helped me a lot.

VBA Code:
Sub CheckAndClear()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim i As Long, j As Long, k As Long
    Dim dataQ As Variant, dataR As Variant, dataU As Variant
    Dim foundRow As Long
    Dim foundWords() As String

    Set ws = ThisWorkbook.Sheets("Sheet2") ' Change Sheet1 to your sheet name

    lastRow = ws.Cells(ws.Rows.Count, "Q").End(xlUp).Row

    dataQ = ws.Range("Q1:Q" & lastRow).value
    dataR = ws.Range("R1:R" & lastRow).value
    dataU = ws.Range("U1:U" & ws.Cells(ws.Rows.Count, "U").End(xlUp).Row).value
    foundRow = 1
    ReDim foundWords(1 To UBound(dataR, 1), 1 To 1)

    For i = 1 To UBound(dataQ, 1)
        If dataQ(i, 1) = 9 Then
            If foundRow = 1 Then foundRow = i ' Store the first row 9
            For j = 1 To UBound(dataU, 1)
                If InStr(1, dataR(i, 1), dataU(j, 1), vbTextCompare) > 0 Then
                    foundWords(j, 1) = dataR(i, 1)
            For k = LBound(dataU) To UBound(dataU)
                foundWords(j, 1) = Trim(Replace(foundWords(j, 1), dataU(k, 1), "", , , vbTextCompare))
            Next k
                    Exit For
                End If
            Next j
            Cells(i, "R").ClearContents
        End If
    Next i

    ' Output modified strings to column R starting from foundRow
    ws.Range("R" & foundRow).Resize(UBound(foundWords), 1).value = foundWords

    ' Delete rows where no modifications were made
    For i = lastRow To 1 Step -1
        If Cells(i, "R").value = "" Then
            Rows(i).Delete
        End If
    Next i
End Sub
 
Upvote 0
@Cubist I am sorry to keep bothering. I am at a total loss why the code is inconsistent. It stopped working. Even in the new workbook. Ends up deleting rows it should not.
 
Upvote 0

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

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