【问题标题】:How to search a substring using values from another column and then delete row, Excel VBA如何使用另一列中的值搜索子字符串然后删除行,Excel VBA
【发布时间】:2017-08-15 14:51:01
【问题描述】:

使用 VBA,我想知道如何使用另一个工作表中的单词列表在 A 列中搜索子字符串,如果找到匹配项,我想删除该单元格。

目前它只会删除完全匹配的单元格。我想不区分大小写并查找部分字符串。

Private Sub RemoveBusinessesButton_Click_OLD2()
    Dim Firstrow As Long
    Dim lastrow As Long
    Dim Lrow As Long
    Dim CalcMode As Long
    Dim ViewMode As Long

    With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
    End With

    'We use the ActiveSheet but you can replace this with
    'Sheets("MySheet")if you want
    With ActiveSheet

        'We select the sheet so we can change the window view
        .Select

        'If you are in Page Break Preview Or Page Layout view go
        'back to normal view, we do this for speed
        ViewMode = ActiveWindow.View
        ActiveWindow.View = xlNormalView

        'Turn off Page Breaks, we do this for speed
        .DisplayPageBreaks = False

        'Set the first and last row to loop through
        Firstrow = .UsedRange.Cells(1).Row
        lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row

        'We loop from Lastrow to Firstrow (bottom to top)
        For Lrow = lastrow To Firstrow Step -1

            'We check the values in the A column in this example
            With .Cells(Lrow, "A")

                If Not IsError(.Value) Then

                    If Not IsError(Application.Match(.Value, _
                    Sheets("BUSINESS_KEYWORDS").Range("A1:A683"), 0)) Then .EntireRow.Delete
                    'This will delete each row with the Value "ron"
                    'in Column A, case sensitive.

                End If

            End With

        Next Lrow

    End With

    ActiveWindow.View = ViewMode
    With Application
        .ScreenUpdating = True
        .Calculation = CalcMode
    End With

End Sub

提前致谢

【问题讨论】:

    标签: vba excel substring


    【解决方案1】:

    如果您想检查一个字符串是否包含另一个字符串而不是与另一个字符串完全相同,请使用此函数:

    If InStr(1, StringToBeSearched, StringtoFind, vbTextCompare) > 0 Then
        Do xyz
    End If
    

    Instr 返回一个整数,表示StringToFind 出现在StringToBeSearched 中的字符,如果找不到则返回0

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-25
      • 2017-06-22
      • 2016-11-04
      • 1970-01-01
      • 2020-07-07
      • 2014-12-23
      • 1970-01-01
      相关资源
      最近更新 更多