在另一个工作表中查找动态范围中的文本

时间:2017-01-23 12:56:14

标签: excel vba excel-vba

我正在创建一个VBA应用程序,它将找到我在某个范围内输入的文本(应该是动态的,以便我将来输入更多内容)。因此,范围内输入的文本将查找另一个工作表列中的单词:

示例:

Words to look for

它会查找另一张表中输入的单词。

Dim Main as Worksheet
Set Main = Sheets("Sheet1")
Dim Raw2 as Worksheet
Set Raw2 = Sheets("Sheet2") 

LookFor = Main.Range(D8:100)
Fruits = Raw2.Range("G" & Raw2.Rows.Count).End(xlUp).row 
For e = lastRow To 2 Step -1 

value = Raw2.Cells(e, 7).value

If Instr(value, LookFor) = 0 _
Then

Raw2.Rows(e).Delete 

老实说,我不知道该怎么办。提到的代码只是实验。期望的输出是删除sheet2中的任何内容,除了包含我在“查找单词”中输入的单词的行。希望您能够帮助我。谢谢。

1 个答案:

答案 0 :(得分:1)

这应该可以解决问题:

Sub Sevpoint()

Dim Main As Worksheet
Set Main = Sheets("Sheet1")
Dim Raw2 As Worksheet
Set Raw2 = Sheets("Sheet2")
Dim LooKFoR() As Variant
Dim LastRow As Double
Dim i As Double
Dim j As Double
Dim ValRow As String
Dim DelRow As Boolean

LooKFoR = Main.Range(Main.Range("G8"), Main.Range("G" & Main.Rows.Count).End(xlUp)).Value

LastRow = Raw2.Range("G" & Raw2.Rows.Count).End(xlUp).Row
For i = LastRow To 2 Step -1
    ValRow = Raw2.Cells(i, 7).Value
    DelRow = True
    'MsgBox UBound(LooKFoR, 1)
    For j = LBound(LooKFoR, 1) To UBound(LooKFoR, 1)
        If LCase(ValRow)<>LCase(LooKFoR(j, 1)) Then
        Else
            DelRow = False
            Exit For
        End If
    Next j
    If DelRow Then Raw2.Rows(i).Delete
Next i

End Sub