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
 
Perfect. Changes sheet3 to Sheet2 and sheet2 to sheet1> And I am in business. Appreciate it.
@Cubist Sorry to bother you but why do I sometimes get the following error? Thank you.

VBA Code:
foundWords(foundIndex, 1) = dataR(i, 1)
 
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Tge Vba stops and upon debugging, the above line is highlighted yellow.
 
Upvote 0
When it stops, it gives you an error message/code. What is it?
 
Upvote 0
Upon debugging, the vba displays the entire code with the following line highlighted yellow.

foundWords(foundIndex, 1) = dataR(i, 1)
 
Upvote 0
Yes, I'm aware that it highlights yellow, but it gives you an error message box. What does it say on the error message box?
Screen Shot 2024-05-27 at 8.07.12 AM.png
 
Upvote 0
Here is how it looks. Could not find highlight. The error line in the following is displayed in bold red.

Sub CheckAndClear_OneA()
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") ' Change Sheet3 to your sheet name
Set ws2 = ThisWorkbook.Sheets("Sheet1") ' Set ws2 to refer to Sheet2

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)

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(foundIndex, 1) = dataR(i, 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
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 ws.Cells(i, "R").value = "" Then
ws.Rows(i).Delete
End If
Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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