我有 Sheet2 数据(产品名称)按字母顺序按列排序,产生 26 列加上一列用于数字数据。在 Sheet1 上有一个近万个产品名称的列表,无论我们在 Sheet2 上是否有它们,都将对其进行测试。一次搜索一个名字是一件很疯狂的事情,所以我在想我们是否可以在 Sheet1 中批量搜索数据,并且在每个单元格旁边,搜索结果将显示在 Sheet2 中找到该产品的单元格的地址,或未找到,例如:
SEARCH STRINGS SEARCH RESULT
Vodafone A4
Mirinda C105
Coca-Cola Y59
HeroHonda not found
Bournvita S27
Maggi not found
我进行了一些搜索以找到上述所需的类似代码,并在以下位置找到以下代码:http ://www.excelforum.com/excel-programming-vba-macros/714965-search.html ,它进行了搜索为一串。可以修改此脚本以产生所需的结果吗?
Sub DataSearch()
Dim Data() As Variant
Dim DstWks As Worksheet
Dim Food As String
Dim N As Variant
Dim R As Long
Dim Rng As Range
Dim RngEnd As Range
Dim SrcWks As Worksheet
Set SrcWks = Worksheets("Test 2")
Set DstWks = Worksheets("Test1")
R = 6
Food = DstWks.Range("E3")
N = DstWks.Range("E4")
If DstWks.Range("C6") <> "" Then
DstWks.Range("C6").CurrentRegion.Offset(0, 1).ClearContents
End If
Set Rng = SrcWks.Range("A4:E4")
Set RngEnd = SrcWks.Cells(Rows.Count, Rng.Column).End(xlUp)
Set Rng = IIf(RngEnd.Row < Rng.Row, Rng, SrcWks.Range(Rng, RngEnd))
ReDim Data(1 To Rng.Rows.Count, 1 To Rng.Columns.Count)
Data = Rng.Value
For I = 1 To UBound(Data, 1)
If Data(I, 1) = N And InStr(1, Data(I, 3), Food, vbTextCompare) > 0 Then
DstWks.Cells(R, "C").Resize(1, Rng.Columns.Count) = Rng.Rows(I).Value
R = R + 1
End If
Next I
End Sub
始终感谢您的宝贵帮助。