My Solution:
I tried three different approaches:
- ADO Connection Object with SQL (slowest)
- VSTO and Excel's Autofilter (reliable)
- LINQ to XML (fastest)
LINQ to XML offered the best performance. I converted my table into an XML file:
Then, in my code, I used StringReader to bring in the XMLwellData file (which is saved as a project resource).
'welldoc will be the file to do queries on using LINQ to XML
Dim stream As System.IO.StringReader
stream = New StringReader(My.Resources.XMLwellData)
welldoc = XDocument.Load(stream)
'clean up stream now that it's no longer needed
stream.Close()
stream.Dispose()
'***** later in the code perform my query on XML file *********
Dim query = _
From well In welldoc.<wellList>.<well> _
Where well.<name>.Value Like "TOMCHUCK 21-30" _
Select well
For Each well in query
MessageBox.Show(well.<padgroup>.value)
Next
This was amazingly simple to do what I wanted and best of all it was FAST.
Thank you for all the help and suggestions. It made all the difference for me figuring this out.
Alternative Method using Excel's Autofilter
If you're trying to use the code suggested in the other answer, this will only filter on two values:
worksheet.Cells.AutoFilter(column, criteria, Excel.XlAutoFilterOperator.xlOr);
So, to filter with multiple criteria using Excel's Auotfilter, you must pass your arguments as an array and filter on xlFilterValues.
Dim wrkbk As Excel.Workbook
Dim wrksht As Excel.Worksheet
Dim myRange As Excel.Range
Dim cell As Excel.Range
'You would add all of your wellnames to search to this List
Dim wellNames As New List(Of String)
wrksht = wrkbk.Sheets(1)
'In my excel file there is a Named Range which includes the all the information
myRange = wrksht.Range("WellLookUpTable")
'Notice, for `Criteria1:=` you MUST convert the List to an array
With wrksht.Range("WellLookUpTable")
.AutoFilter(Field:=2, Criteria1:=wellNames.ToArray, Operator:=Excel.XlAutoFilterOperator.xlFilterValues)
End With
myRange = wrksht.Range("A2", wrksht.Range("A2").End(Excel.XlDirection.xlDown)).Cells.SpecialCells(Excel.XlCellType.xlCellTypeVisible)
For Each cell In myRange
'column 11 is padgroup
MessageBox.Show(cell.Offset(0, 11).Value)
Next