Home » excel » Delete keywords from Excel File using VBA

Delete keywords from Excel File using VBA

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have a list of keywords that I want to find and delete the entire row in a spreadsheet. The goal is for it to find these key words, replace them with “#N/A”, and then delete those rows associated.

Right now I get an error on the second to last line of code that states, “Run-time error ‘1004’: No cells were found.” This makes sense as I realized I need to find the string “#N/A” not an error.

I’m struggling to come up with a way to delete the rows associated with the “#N/A” string.

Option Explicit

Sub Delete_EEE()

Dim Wrds As Variant, Gwrd As String
Dim i As Long

Gwrd = "Jans"
Wrds = Array("ohm", "resistor", "MCKT", "micro", "inductor")

Range("G:G").Replace Gwrd, "#N/A", xlPart, , False

Application.ScreenUpdating = False

For i = LBound(Wrds) To UBound(Wrds)
    Range("E:E").Replace Wrds(i), "#N/A", xlPart, , False
    Range("I:I").Replace Wrds(i), "#N/A", xlPart, , False
Next i

Range("E:I").SpecialCells(xlCellTypeConstants, xlErrors).EntireRow.Delete

Application.ScreenUpdating = True

End Sub
How to&Answers:

You need to start with the last row with data and work you way up when you want to delete rows. And below you will find a piece of code to check if any value in your array is in the source string, which string could be the content of a cell.

'********************************************************************************************************************
' To check if any word from an array is within the source string
' Duplicate blanks are removed and strings put in lower case for the validation
' Returns False if there was an error or no item was found
'********************************************************************************************************************
Public Function isFromArrInSentence(ByVal strSource As Variant, ByVal wordsArray As Variant) As Boolean
Dim tmpBool As Boolean, x As Long

tmpBool = False
If VarType(strSource) = vbString And IsArray(wordsArray) Then
    strSource = LCase(Application.WorksheetFunction.Trim(strSource))
    If Len(strSource) > 0 Then
        If Not isEmpty(wordsArray) Then
            For x = LBound(wordsArray) To UBound(wordsArray)
                If InStr(1, strSource, LCase(Application.WorksheetFunction.Trim(wordsArray(x)))) > 0 Then
                    tmpBool = True
                    Exit For
                End If
            Next x
        End If
    End If
End If
isFromArrInSentence = tmpBool

End Function

Answer:

If the column contains your value exactly as it appears, you do not need to loop at all. Just filter by your values, and delete the visible range

Option Explicit

Sub iDelete()

Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")

Dim Arr, LR As Long, DeleteMe As Range
Arr = Array("ohm", "resistor", "MCKT", "micro", "inductor")

LR = ws.Range("G" & ws.Rows.Count).End(xlUp).Row
ws.Range("G:G").AutoFilter 1, Arr, xlFilterValues

Set DeleteMe = ws.Range("G2:G" & LR).SpecialCells(xlCellTypeVisible)

If Not DeleteMe Is Nothing Then
    DeleteMe.EntireRow.Delete
End If

End Sub

Answer:

This should be a simpler method:

LastRow = Cells(Rows.Count, 7).End(xlUp).Row

For CurRow = LastRow to 1 Step -1
    GVal = Cells(CurRow, 7).Value 'column G
    Select Case True
        Case GVal Like "*ohm*", GVal Like "*resistor*", GVal Like "*MCKT*", GVal Like "*micro*", GVal Like "*inductor*"
            Cells(CurRow, 7).EntireRow.Delete xlShiftUp
    End Select
    EVal = Cells(CurRow, 5).Value 'column E
    Select Case True
        Case EVal Like "*ohm*", EVal Like "*resistor*", EVal Like "*MCKT*", EVal Like "*micro*", EVal Like "*inductor*"
            Cells(CurRow, 7).EntireRow.Delete xlShiftUp
    End Select
    IVal = Cells(CurRow, 9).Value 'column I
    Select Case True
        Case IVal Like "*ohm*", IVal Like "*resistor*", IVal Like "*MCKT*", IVal Like "*micro*", IVal Like "*inductor*"
            Cells(CurRow, 7).EntireRow.Delete xlShiftUp
    End Select
Next CurRow