使用VBA从Excel文件中删除关键字

时间:2019-04-18 17:39:34

标签: excel vba

我有一个要在电子表格中查找并删除整行的关键字列表。其目标是找到这些关键字,将其替换为“#N / A”,然后删除关联的那些行。

现在,我在倒数第二行代码上出现错误,指出“运行时错误'1004':未找到任何单元格。”这是有道理的,因为我意识到我需要找到字符串“#N / A”而不是错误。

我正在努力想出一种删除与“#N / A”字符串关联的行的方法。

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

3 个答案:

答案 0 :(得分:1)

您需要从包含数据的最后一行开始,然后在要删除行时逐步进行。在下面,您将找到一段代码来检查数组中的任何值是否在源字符串中,该字符串可能是单元格的内容。

'********************************************************************************************************************
' 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

答案 1 :(得分:1)

如果该列完全包含您所显示的值,则根本不需要循环。只需根据您的值进行过滤,然后删除可见范围

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

答案 2 :(得分:0)

这应该是一个更简单的方法:

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