Filter table with multiple criteria

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,834
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Further to my post here:

Code:
https://www.mrexcel.com/board/threads/filter-data-using-sql.1158918/#post-5622447

the code that does work is as follows:

Code:
Sub GetData()

    Dim cn As ADODB.Connection
    Dim rs As ADODB.Recordset
    
    Dim strcon As String
    Dim strSQL As String
        
    Set cn = New ADODB.Connection
    Set rs = New ADODB.Recordset
    
    rs.CursorLocation = adUseServer
    
    
strcon = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
             "Data Source=" & ThisWorkbook.FullName & ";" & _
             "Extended Properties=""Excel 12.0 Macro;" & _
             "HDR=Yes;" & _
             "IMEX=1;" & _
             "MaxScanRows=0"";"
             
    cn.Open ConnectionString:=strcon
        
    strSQL = "SELECT * " & _
             "FROM [DataSheet$] " & _
             "WHERE [DataSheet$].[Fruit] =""orange"""
    
    rs.Open Source:=strSQL, _
            ActiveConnection:=cn
    
    wksData.Cells(1,1).CopyFromRecordset Data:=rs
        
    rs.Close
    cn.Close
            
    Set rs = Nothing
    Set cn = Nothing
       
End Sub

But if I have multiple criteria, such as:

Code:
strSQL = "SELECT * " & _
             "FROM [DataSheet$] " & _
             "WHERE [DataSheet$].[Fruit] =""orange"" AND [DataSheet$].[Fruit] = ""apple"""

would it be quicker to do a For Next Loop, as in:

Code:
Dim CriteriaArray() As Variant
    
    CriteriaArray() = Array("orange", "apple")
    
    Dim i As Integer
    
    For i = LBound(CriteriaArray(), 1) To UBound(CriteriaArray(), 1)
    
        strSQL = "SELECT * " & _
                 "FROM [Data$] " & _
                 "WHERE [Data$].[Segments] = """ & CriteriaArray(i) & """ " 
        
        rs.Open Source:=strSQL, _
                ActiveConnection:=cn
        
           wksData.Cells(1,1).CopyFromRecordset Data:=rs
        
        rs.Close
        
    Next i

Thanks
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

Forum statistics

Threads
1,215,465
Messages
6,124,982
Members
449,201
Latest member
Lunzwe73

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