Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50123

Multiple String Search

$
0
0
I have Sheet2 with data (names of products) alphabetically sorted by column yielding 26 columns plus one column for numeric data. On Sheet1 there is a list of nearly 10,000 names of products which is to be tested whether we have them on Sheet2 or not. To search one name at a time is a matter of craze, so I was thinking if we could bulk search data in Sheet1 and next to each cell the search result will be shown with the address of the cell where that product is found in Sheet2, or not found, such as:

SEARCH STRINGS SEARCH RESULT

Vodafone A4
Mirinda C105
Coca-Cola Y59
HeroHonda not found
Bournvita S27
Maggi not found
I have done some search to find similar code for what is needed above, and found the following code at: http://www.excelforum.com/excel-prog...65-search.html, which does the search for one string. Can this script be modified to yeild the desired results?

Code:

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

Always appreciating your invaluable assistance.

Viewing all articles
Browse latest Browse all 50123

Trending Articles