经过过滤后搜索关键字,然后在另一列中返回值

时间:2019-05-24 08:08:45

标签: excel vba

有一个过滤器应用于我的数据范围,我想在过滤后在“ I”列中搜索关键字“ abc”,并在我的数据列“ W”的最后返回值“ Check”

我尚不知道此函数的任何示例,但是我确实有一个代码来搜索值,如果数量为0,则删除该行。

'Delete rows for zero value

 Dim LR As Long, i As Long

      With Sheets("tempp")

           LR = .Cells.Find(What:="0", SearchDirection:=xlPrevious, 
           SearchOrder:=xlByRows).Row

           For i = LR To 1 Step -1
                If .Range("C" & i).Value = 0 Then .Rows(i).Delete
           Next i

      End With

2 个答案:

答案 0 :(得分:0)

以下代码将搜索您的I列。请根据需要调整工作表名称。如果找到“ abc”,它将返回检查状态。如果找不到,则可以在规定的位置运行所需的检查。

Sub RangeCheck()

Dim myRange As Range
Dim lRowW as Long

'adjust the sheet name to your sheet name
Set myRange = Sheets("Sheet1").Range("I:I").SpecialCells(xlCellTypeVisible).Find(What:="abc")

If Not myRange Is Nothing Then
    'found abc, insert checked - presumably the last row of column W?
    lRowW = Sheets("Sheet1").Range("W" & Sheets("Sheet1").Rows.Count).End(xlUp).Row

    Sheets("Sheet1").Range("W" & lRowW).Value = "Checked"

    'if not last row in column W, then use the below
    'Sheets("Sheet1").Range("W1").Value = "Checked"
Else
    'if it does not find your string "abc" then insert the check you want to run here

End If

'clear object
Set myRange = Nothing

End Sub

答案 1 :(得分:0)

您可以使用以下内容:

Dim c As Range

    'Going through each visible cell in *I* column. Change 5000 to the numbers of rows your sheet have

     For Each c In Worksheets("tempp").Range("I1:I5000").SpecialCells(xlCellTypeVisible)

        'Checking if cell contains abc

        If InStr(c.Value, "abc") > 0 Then

            Worksheets("tempp").Range("W" & c.Row).Value = "Check"

        End If

    Next

让我们知道它是否有效!