下面的脚本形成了找出那些没有按递增顺序排列的日期值的行。但是当我运行代码时得到一个ERROR
说法No value given one or more required parameters
代码
Option Explicit
Dim conn, cmd, rs
Dim clauses(36), i
Dim xlApp, xlBook
Set conn = CreateObject("ADODB.Connection")
With conn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "Data Source=""D:\WIPData\AravoMacro\Finalscripts\GE_Wing_To_Wing_Report.xlsx"";" & _
"Extended Properties=""Excel 12.0;HDR=Yes"""
.Open
End With
For i = 0 To 36
clauses(i) = "[Task" & i + 1 & " Start Date] < [Task" & i + 2 & " Start Date]"
Next
Set cmd = CreateObject("ADODB.Command")
cmd.CommandText = "SELECT * FROM [GEWingToWingMay25$] WHERE " & Join(clauses, " OR ")
cmd.ActiveConnection = conn
Set rs = cmd.Execute
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlBook = xlApp.Workbooks.Add
xlBook.Sheets(1).Range("A1").CopyFromRecordset cmd.Execute
有谁可以帮我离开这里吗?
编辑
谢谢,
编辑1
现在我尝试了这个
Set cmd = CreateObject("ADODB.Command")
'cmd.CommandText = "SELECT * FROM [GEWingToWingMay25$] WHERE " & Join(clauses, " OR ")
cmd.CommandText = "SELECT * FROM [GEWingToWingMay25$] WHERE " & "#2012/10/01# > #2012/10/015#"
MsgBox(cmd.CommandText)
cmd.ActiveConnection = conn
Set rs = cmd.Execute
收到错误class doesn't support Automation
快照2
编辑3
我更新如下:
cmd.CommandText = "SELECT * FROM [GEWingToWingMay25$] WHERE #" & CDate([Task1StartDate]) & "# > #" & CDate([Task2StartDate]) _
"# > #" & CDate([Task3 Start Date]) & "# > #" & CDate([Task4 Start Date]) _
"# > #" & CDate([Task5 Start Date]) & "# > #" & CDate([Task6 Start Date]) _
"# > #" & CDate([Task7 Start Date]) & "# > #" & CDate([Task8 Start Date]) _
"# > #" & CDate([Task9 Start Date]) & "# > #" & CDate([Task10 Start Date]) _
"# > #" & CDate([Task11 Start Date]) & "# > #" & CDate([Task12 Start Date]) _
"# > #" & CDate([Task13 Start Date]) & "# > #" & CDate([Task14 Start Date]) _
"# > #" & CDate([Task15 Start Date]) & "# > #" & CDate([Task16 Start Date]) _
"# > #" & CDate([Task17 Start Date]) & "# > #" & CDate([Task18 Start Date]) _
"# > #" & CDate([Task19 Start Date]) & "# > #" & CDate([Task20 Start Date]) _
"# > #" & CDate([Task21 Start Date]) & "# > #" & CDate([Task22 Start Date]) _
"# > #" & CDate([Task23 Start Date]) & "# > #" & CDate([Task24 Start Date]) _
"# > #" & CDate([Task25 Start Date]) & "# > #" & CDate([Task26 Start Date]) _
"# > #" & CDate([Task27 Start Date]) & "# > #" & CDate([Task28 Start Date]) _
"# > #" & CDate([Task29 Start Date]) & "# > #" & CDate([Task30 Start Date]) _
"# > #" & CDate([Task31 Start Date]) & "# > #" & CDate([Task32 Start Date]) _
"# > #" & CDate([Task33 Start Date]) & "# > #" & CDate([Task34 Start Date]) _
"# > #" & CDate([Task35 Start Date]) & "# > #" & CDate([Task36 Start Date]) & "#;"
EDIT4正如谷歌链接所暗示的那样,如果HDR
设置为是,那么 excel 表应该有第一行作为字段级标签,我的 excel 有。但是为什么会出现错误呢?
更新代码
Option Explicit
Dim conn, cmd, rs
Dim clauses(34), i
Dim xlApp, xlBook
Dim tempDate,LenDate
Set conn = CreateObject("ADODB.Connection")
With conn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "Data Source=""D:\AravoVB\GE_Wing_To_Wing_Report.xlsx"";" & _
"Extended Properties=""Excel 12.0;HDR=Yes"""
.Open
End With
For i = 0 To 34
clauses(i) = "[Task" & i + 1 & " Start Date] > [Task" & i + 2 & " Start Date]"
tempDate=tempDate & "[Task" & i + 1 & " Start Date],"
Next
Set cmd = CreateObject("ADODB.Command")
cmd.CommandText = "SELECT " & tempDate &" FROM [GEWingToWingMay25$] WHERE [Business Process ID] NOT IN (" & "SELECT [Business Process ID] FROM [GEWingToWingMay25$] WHERE " & Join(clauses, " AND ") & ")"
MsgBox(cmd.CommandText)
cmd.ActiveConnection = conn
Set rs = cmd.Execute
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlBook = xlApp.Workbooks.Add
xlBook.Sheets(1).Range("A1").CopyFromRecordset cmd.Execute
现在所有的start date
每一行都没有值,因此IN
运算符内部的 SQL Query 没有返回任何值,因此外部 SQL 查询提供了所有数据。所以知道如何Null values
在 SQL 查询where
子句评估中忽略这些。任何想法?