Find multiple values and return to a list/array

Snypa

New Member
Joined
Nov 1, 2013
Messages
45
Hello guys,

I have the following code:

Code:
On Error Resume Next
                
strOut = Application.WorksheetFunction.VLookup(strSearch, Worksheets("StoreLookup").Range("A2:B2000"), 2, False)

strSearch is a string the user wants to find (with wildcards) from a shop's name and it returns the shops account number. Like a fuzzy lookup.

This is great and it returns the first store it finds. I want to change this so it returns all the stores it finds and I can ask the user which result it would like to view. Can I change the code so it returns a list/array of values from the table (both column a and b)?
 
Last edited:

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
To clarify:

The code I have:

Code:
On Error Resume Next
                
strOut = Application.WorksheetFunction.VLookup(strSearch, Worksheets("StoreLookup").Range("A2:B2000"), 2, False)

Takes a string from the user, encapsulated with wildcards (*), and returns the first match from a vlookup. I would like to know how I would continue returning matches that I can store in an array.

Any help would be greatly appreciated.
 
Upvote 0
See if you can use/adapt something like this. If I have understood correctly the store names and account numbers in the array aryResults.

Code:
Dim rngFound As Range
Dim lngFirstRow As Long
Dim lngCol As Long
Dim aryResults As Variant

With Sheets("StoreLookup").Range("A2:A2000")
  Set rngFound = .Find(What:=strSearch, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)
  If rngFound Is Nothing Then
    MsgBox strSearch & " not found"
  Else
    lngFirstRow = rngFound.Row
    ReDim aryResults(1 To 2, 0 To 0)
    Do
      lngCol = lngCol + 1
      ReDim Preserve aryResults(1 To 2, 1 To lngCol)
      aryResults(1, lngCol) = rngFound.Value
      aryResults(2, lngCol) = rngFound.Offset(, 1).Value
      Set rngFound = .FindNext(After:=rngFound)
    Loop Until rngFound.Row = lngFirstRow
  End If
End With
 
Upvote 0
Thank you @Peter_SSs, that looks good!

Can I ask, how is this storing the results? It looks like its an array of two arrays, (1, x) being column a and (2, x) being column b, is that correct?

And if so, how do I loop through the array? Sorry, I was hoping arrays would be like Python's lists but (where you can use two for loops).

Any help very welcome!
 
Last edited:
Upvote 0
Nevermind, I figured it out! (LBound(aryResults, 2) To UBound(aryResults, 2))!!

Thanks for the help!!
 
Upvote 0

Forum statistics

Threads
1,224,946
Messages
6,181,948
Members
453,075
Latest member
anandn93

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top