Extracting the collection of unique values from a filter in VBA

前端 未结 5 494
长发绾君心
长发绾君心 2020-12-11 06:51

I have a file which has rows extending to tens of thousands across 8 columns. One particular column contains the weekend date. I have to count the number of weekends present

5条回答
  •  一个人的身影
    2020-12-11 07:11

    You could connect to the appropriate worksheet using ADODB, and issue an SQL statement against the worksheet:

    Dim datasourcePath As String
    datasourcePath = "C:\path\to\excel\file.xlsx"
    
    Dim connectionString As String
    connectionString = _
        "Provider=Microsoft.ACE.OLEDB.12.0;" & _
        "Data Source=""" & datasourcePath & """;" & _
        "Extended Properties=""Excel 12.0;HDR=No""
    
    Dim sql As String
    sql = "SELECT DISTINCT F1 FROM [Sheet1$]" 'F1 is an autogenerated field name
    
    Dim rs As New ADODB.Recordset
    rs.Open sql, connectionString
    
    Do Until rs.EOF
        Debug.Print rs("F1")
    Loop
    

提交回复
热议问题