An Quala
Board Regular
- Joined
- Mar 21, 2022
- Messages
- 146
- Office Version
- 2021
- Platform
- Windows
Hello Guys, this code is supposed to lookup the certain words in a different sheet and then delete the matching rows. For now it does not match "apple" in a "Pineapple", but I want to do so. If I put "apple", it also matches "Pineapple" in the row.
Your help will be highly appreciated.
Thank you!
Your help will be highly appreciated.
Thank you!
VBA Code:
Sub Delete_Rows(CP_KeyWordCol As String, ShName As String, ColToCheck As String)
Dim RX As Object
Dim a As Variant, b As Variant
Dim nc As Long, i As Long, k As Long
Set RX = CreateObject("VBScript.RegExp")
RX.IgnoreCase = True
With Sheets("Control Panel")
If .Range(CP_KeyWordCol & Rows.Count).End(xlUp).Row >= 59 Then
a = Application.Transpose(.Range(CP_KeyWordCol & "59", .Range(CP_KeyWordCol & Rows.Count).End(xlUp)).Value)
If VarType(a) = vbVariant + vbArray Then
RX.Pattern = "\b(" & Replace(Join(Filter(Split("#" & Join(a, "#|#"), "|"), "##", False), "|"), "#", "") & ")\b"
Else
RX.Pattern = "\b" & a & "\b"
End If
End If
End With
If Len(RX.Pattern) > 0 Then
With Sheets(ShName)
nc = .Cells.Find(What:="*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
a = .Range(ColToCheck & "2", .Range(ColToCheck & Rows.Count).End(xlUp)).Value
ReDim b(1 To UBound(a), 1 To 1)
For i = 1 To UBound(a)
If RX.Test(a(i, 1)) Then
b(i, 1) = 1
k = k + 1
End If
Next i
If k > 0 Then
Application.ScreenUpdating = False
With .Range("A2").Resize(UBound(a), nc)
.Columns(nc).Value = b
.Sort Key1:=.Columns(nc), Order1:=xlAscending, Header:=xlNo
.Resize(k).EntireRow.Delete
End With
Application.ScreenUpdating = True
End If
End With
End If
End Sub
VBA Code:
Delete_Rows "O", "Sponsored Products Campaigns", "P"
VBA Code:
Delete_Rows "Q", "Sponsored Brands Campaigns", "O"
VBA Code:
Delete_Rows "S", "Sponsored Display Campaigns", "O"
Last edited by a moderator: