0

我正在使用 MS Excel-2010。我有一个如下的 excel 表:

Process#       requirementrcvd     designdate     codingdate      test1 date      test2 date   deliverdate

  11           10/11/2009          12/12/2009     02/02/2011      02/03/2011      09/03/2011   10/04/2011
  12           10/11/2010          12/12/2011     15/02/2012      
  13           10/11/2009          12/12/2009     02/02/2011      02/03/2011  

所有日期都以递增的值给出。但我试图获得一些ADODB功能,通过它我可以将空的日期值设置为大于该记录的最后一列日期的值。

比如说,作为一个示例过程#12 test1 date,test2 date,delivery date 是NULL,所以通过 .vbs 脚本它应该设置为16/02/2012,17/02/2012,18/02/2012.

代码:

选项显式

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\Final Scripts\GE_Wing_To_Wing_Report - Copy.xlsx"";" & _
    "Extended Properties=""Excel 12.0;HDR=Yes"""
.Open
End With
'tempDate=""
For i = 0 To 34
clauses(i) = "IIf(IsNull([Task" & i + 1 & " Start Date]),Date()+"& i &",[Task" & i + 1 & " Start Date]) < IIf(IsNull([Task" & i + 2 & " Start Date]),Date()+"& i &",[Task" & i + 2 & " Start Date])"
tempDate=tempDate & "NVL([Task" & i + 1 & " Start Date],Date()+"& i &"),"
Next
'LenDate=Len(tempDate)-1
'tempDate=Mid(tempDate,1,LenDate)
MsgBox(tempDate)

Set cmd = CreateObject("ADODB.Command")
cmd.CommandText = "SELECT * FROM [GEWingToWingMay25$] WHERE [Business Process ID] NOT IN (" & "SELECT [Business Process ID] FROM [GEWingToWingMay25$] WHERE " & Join(clauses, " OR ") & ")"
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
'xlBook.Sheets(1).Cells(1,25).Value=cmd.CommandText
4

1 回答 1

0

您需要 UPDATE 语句而不是 SELECT 语句。具体是这样的:

cmd.CommandText = _
    "UPDATE [GEWingToWingMay25$] " & _
    "SET [Task1 Start Date] = Now() " & _
    "WHERE [Business Process ID] NOT IN (" & _
        "SELECT [Business Process ID] " & _
        "FROM [GEWingToWingMay25$] " & _
        "WHERE " & Join(clauses, " OR ") & 
    ")"
于 2012-12-22T20:26:12.343 回答