我有一个excel的数据库,我想在这个excel表里把指定时间段的数据找出来并复制到新的excel表里,例如:excelA里保存的是原始数据,时间保存在B列,日期保存在C列,我想通过在ExcelB里指定时间段去excelA里查找数据并复制到B里来,如我想查找早上8:00到下午17:00的数据,这段代码应该怎么写呢,
Sub data()
Dim wb As Workbook
Dim cnn As ADODB.Connection
Dim rs As ADODB.Connection
Dim T1, T2 As String
T1 = "07:00:00"
T2 = "19:00:00"
Set cnn = New ADODB.Connection
Set wb = Workbooks.Open(ThisWorkbook.Path + "\原始数据.xlsx")
cnn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Extended Properties=Excel 8.0;" & "Data Source= & wb"
cnn.Open
sSql = "select 卡号,姓名,时间,日期 from 原始数据 where 发生时间>=#" & T2 & "# and 时间<=#" & T1 & "#"
Sheet2.Range("a2").CopyFromRecordset cnn.Execute(sSql)
报错已经解决,但获取不到数据,
数据量比较大,大概有好几万条,我想用vba代码来做。这样可以做吗?我本来想用sql语言来写一段,但调不通,老是报错。
数据量比较大,大概有好几万条,我想用vba代码来做。这样可以做吗?我本来想用sql语言来写一段,但调不通,老是报错。报错在补充说明里,可以麻烦你帮我看看这段代码有什么不对吗?谢谢!
追答Sub data()
Dim wb As Workbook
Dim cnn As ADODB.Connection
Dim rs As ADODB.Connection
Dim T1, T2 As String
T1 = "07:00:00"
T2 = "19:00:00"
Set cnn = CreateObject("ADODB.connection")
cnn.Open "dsn=excel files;IMEX=1;dbq=" + ThisWorkbook.Path + "\原始数据.xlsx"
sSql = "select 卡号,姓名,时间,日期 from [Sheet1$] where 时间>=#" & T1 & "# and 时间<=#" & T2 & "#"
Sheet2.Range("a2").CopyFromRecordset cnn.Execute(sSql)
End Sub
vba run-time error 3705
按照你你的修改之后报这个错,麻烦指导,谢谢