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
 
What's the data set your running the code on that gives the error?
 
Upvote 0

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Here it is. Thank you much. I do have idenitcal column U insheet1 too.

Book1
OPQRSTU
1Theater:delim1.
2Movie:delim2.
3TITLE1delim3.
4ErosEros:4Eros: Dunndelim4.
5ErosStrand shows Abba.delim5.
6ErosMandir shows Sholaydelim6.
7MetroRoxi shows Cars.delim7.
8MetroOpera House shows Shordelim8.
9MetroMetro:4Metro: Batmandelim9.
10MinervaMinerva:4Minerva: Starwarsdelim10.
11RegalRegal:4Regal: Superman
125Eros shows Superman nextweek.
135Metro shows Dunn next month.
14MetroPlaceTITLE2
159Eros shows Superman nextweek. Delim4.
169Metro shows Dunn next month. Delim3. Closed today.
179Delim2. Minerva is showing Abba.
189Before Metro Roxi shows Cars.
199After Eros Strand shows Abba.
209After Eros Mandir shows Sholay
219Before Metro Opera House shows Shor
Sheet2
 
Upvote 0
I can't replicate the error. It runs fine on my end. What is on Sheet1?
 
Upvote 0
Book1
U
1delim1.
2delim2.
3delim3.
4delim4.
5delim5.
6delim6.
7delim7.
8delim8.
9delim9.
10delim10.
Sheet1
 
Upvote 0
I don't see anything that would cause the Subscript error. Try running it on a new blank workbook.
 
Upvote 0
It did much better in the new book. However, it did not sort based on the order in the column U.
 
Upvote 0
Here is what I mean. I got the following results.

Book1
OPQRSTU
1Theater:delim1.
2Movie:delim2.
3TITLE1delim3.
4ErosEros:4Eros: Dunndelim4.
5ErosStrand shows Abba.delim5.
6ErosMandir shows Sholaydelim6.
7MetroRoxi shows Cars.delim7.
8MetroOpera House shows Shordelim8.
9MetroMetro:4Metro: Batmandelim9.
10MinervaMinerva:4Minerva: Starwarsdelim10.
11RegalRegal:4Regal: Superman
125Eros shows Superman nextweek.
135Metro shows Dunn next month.
14MetroPlaceTITLE2
159Eros shows Superman nextweek.
169Metro shows Dunn next month. Closed today.
179Minerva is showing Abba.
Sheet2


Based on the the delim value the last three rows had, It should have been the following.

Book1
OPQRSTU
1Theater:delim1.
2Movie:delim2.
3TITLE1delim3.
4ErosEros:4Eros: Dunndelim4.
5ErosStrand shows Abba.delim5.
6ErosMandir shows Sholaydelim6.
7MetroRoxi shows Cars.delim7.
8MetroOpera House shows Shordelim8.
9MetroMetro:4Metro: Batmandelim9.
10MinervaMinerva:4Minerva: Starwarsdelim10.
11RegalRegal:4Regal: Superman
125Eros shows Superman nextweek.
135Metro shows Dunn next month.
14MetroPlaceTITLE2
159Minerva is showing Abba.
169Metro shows Dunn next month. Closed today.
179Eros shows Superman nextweek.
Sheet2
 
Upvote 0
The original data was as follows.

Book1
YZ
19Eros shows Superman nextweek. Delim4.
29Metro shows Dunn next month. Delim3. Closed today.
39Delim2. Minerva is showing Abba.
Sheet2
 
Upvote 0
Try.
VBA Code:
Sub CheckAndClear()
    Dim ws As Worksheet, ws2 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") 'Main sheet
    Set ws2 = ThisWorkbook.Sheets("Sheet1") 'Delim sheet

    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 = ws2.Range("U1:U" & ws2.Cells(ws2.Rows.Count, "U").End(xlUp).Row).Value

    foundRow = 1
    foundIndex = 1
    ReDim foundWords(1 To UBound(dataR, 1), 1 To 1)

    ' Iterate over dataU array first
    For j = 1 To UBound(dataU, 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
                If InStr(1, dataR(i, 1), dataU(j, 1), vbTextCompare) > 0 Then
                    foundWords(foundIndex, 1) = dataR(i, 1)
                    Debug.Print dataR(i, 1)
                    Debug.Print foundWords(foundIndex, 1)
                    For k = LBound(dataU) To UBound(dataU)
                        foundWords(foundIndex, 1) = Trim(Replace(foundWords(foundIndex, 1), dataU(k, 1), "", , , vbTextCompare))
                    Next k
                    foundIndex = foundIndex + 1
                End If
            End If
        Next i
    Next j

    If foundRow > 1 Then
        ws.Range("R" & foundRow).Resize(UBound(foundWords, 1), 1).Value = foundWords
    End If

    For i = lastRow To 1 Step -1
        If ws.Cells(i, "R").Value = "" Then
            ws.Rows(i).Delete
        End If
    Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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