使用ADODB搜索已关闭的Excel工作簿

时间:2015-05-30 13:37:26

标签: excel-vba vba excel

我正在尝试查询已关闭的excel工作簿,并已成功完成该操作。我想要做的是修改我的搜索查询:2件事 第一件事:如何查询空单元格 第二件事:特定日期之间的查询(now()&amp; now() - 1) - 尝试从查询中检索昨天的日期..我的列日期也采用这种格式(2015年5月29日上午7:25)< / p>

这是我在下面使用的脚本:

Sub Pull_Data_from_Excel_with_ADODB()

Dim cnStr As String
Dim rs As ADODB.Recordset
Dim query As String
Dim var1
Dim var2
var1 = Date - 1

var2 = "not"

Dim fileName As String
fileName = "C:\Signin-Database\DATABASE\Signin-Database.xlsm"

    cnStr = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
           "Data Source=" & fileName & ";" & _
           "Extended Properties=Excel 12.0"


query = "SELECT * FROM [Sheet1$D:E] WHERE [Time_in] = '" & var1 & " ' AND [Time_out] ='" & var2 & "'"



Set rs = New ADODB.Recordset
rs.Open query, cnStr, adOpenUnspecified, adLockUnspecified

Cells.Clear
Range("A2").CopyFromRecordset rs

Dim Cell As Range, i As Long
With Range("A1").CurrentRegion
    For i = 0 To rs.Fields.Count - 1
        .Cells(1, i + 1).Value = rs.Fields(i).Name
    Next i
    .EntireColumn.AutoFit

    End With
    End Sub

1 个答案:

答案 0 :(得分:0)

我相信我发现许多试验错误

这是下面的最新工作代码

Sub Pull_Data_from_Excel_with_ADODB()

Dim cnStr As String
Dim rs As ADODB.Recordset
Dim query As String
Dim var1
Dim var2
var1 = Now() - 1
var2 = Now()
Dim fileName As String
fileName = "C:\Signin-Database\DATABASE\Signin-Database.xlsm"

    cnStr = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
           "Data Source=" & fileName & ";" & _
           "Extended Properties=Excel 12.0"


query = "SELECT * FROM [Sheet1$D:H] WHERE [Time_in] < '" & var2 & "' AND [Time_in] >  '" & var1 & "' AND [Time_out] is null"

Set rs = New ADODB.Recordset
rs.Open query, cnStr, adOpenUnspecified, adLockUnspecified

Cells.Clear
Range("A2").CopyFromRecordset rs

Dim Cell As Range, i As Long
With Range("A1").CurrentRegion
    For i = 0 To rs.Fields.Count - 1
        .Cells(1, i + 1).Value = rs.Fields(i).Name
    Next i
    .EntireColumn.AutoFit





    End With

End Sub