0

I am querying the excel using the below code to get the rows matching a certain condition:

For i = 90 To Worksheets("My Sheet").Range("E65536").End(xlUp).Row
' This for loop is to iterate through the list of dates in the excel sheet
Value = Worksheets("My Sheet").Range("E" & i).Value - #1/1/1900# + 2
'Using the above line i am converting the date i want to filter the date 
 'into numeric
strQuery = "SELECT * FROM [My another sheet$A3:I" & LastRow & "] WHERE [Date1]< " &
Value & " AND ([somefield] = 'dog' or [somefield] = 'cat')"
    ' Here in the SQL query i am trying to see how many cats and dogs are satisfying with my date criteria
    Set rs = New ADODB.Recordset
        With rs
            .ActiveConnection = cn
            .Source = strQuery
            .CursorType = adOpenDynamic
            .CursorLocation = adUseClient
            .LockType = adLockOptimistic
            .Open
        End With


' Executing the select query on excel as database using ADODB api
    If rs.RecordCount = 0 Then        
        Else
            rs.MoveLast        
    End If
 Worksheets("My Sheet").Range("F" & i).Value = rs.RecordCount
 rs.Close
 ' Moving to the next date in the list
Next

I have data something like this

  1. 01/01/2012 dog A
  2. 02/01/2012 cat B
  3. 03/01/2012 cat C
  4. 04/01/2012 rat D
  5. 05/01/2012 cat E
  6. 06/01/2012 parrot F

and my list has dates like this

  1. 12/31/2010 - Here i have to find how many cats and dogs are purchased from the shop with date less than this date
  2. 12/31/2011 - Here i have to find how many cats and dogs are purchased from the shop with date less than this date

The purpose of the for loop is to iterate through these dates and modifying the select query This are the values i am receiving from the above code

32 35 37 44 57 64 71 78 84 86 89 91 91 But i should get these values 32 36 37 47 57 66 73 81 84 89 90 91 91 I read somewhere like that we should issue the command rs.movelast before using the record count property but still it is showing wrong result on some iterations..

Is it because of the loop and i am using the same record set.

The reason i know the count should be is because i just did the same filtering on excel columns and it is showing different count

There is no datatype changing in the columns i.e. each column has same data structure from start to end

Please help me on this.

I hope the question is clear..please let me know if more details are needed.

4

1 回答 1

1

尝试这个。它消除了循环。Sheet7 有一个日期列表,而 Sheet8 有数据。

Sub groupbydate()
    sSQL = "TRANSFORM Count(b.Dates) AS CountOfDates " _
    & "SELECT 'Total' AS Total " _
    & "FROM [Sheet7$] a, [Sheet8$] b " _
    & "WHERE b.Animal In ('cat','dog') " _
    & "AND b.[dates]<=a.[dates] " _
    & "GROUP BY 'Total' " _
    & "PIVOT a.Dates"

    strFile = ActiveWorkbook.FullName

    strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFile _
        & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"

    ''Late binding, so no reference is needed

    Set cn = CreateObject("ADODB.Connection")
    Set rs = CreateObject("ADODB.Recordset")

    cn.Open strCon

    rs.Open sSQL, cn, 3, 3

    For i = 1 To rs.Fields.Count
        Worksheets("Sheet8").Cells(1, 5 + i) = rs.Fields(i - 1).Name
    Next
    Worksheets("Sheet8").Cells(2, 6).CopyFromRecordset rs

    ''Tidy up
    rs.Close
    Set rs = Nothing
    cn.Close
    Set cn = Nothing

End Sub
于 2012-09-22T13:45:48.217 回答