Excel null日期列需要由ADODB和vbscript更新

时间:2012-12-22 10:51:05

标签: excel-vba vbscript ado adodb vba

我正在使用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功能,通过该功能,我可以将空值设置为大于该记录的最后一列日期的值。

比方说,作为一个简短的流程#12test1日期,test2日期,交付日期为NULL,因此.vbs脚本应将其设置为16/02/2012,17/02/2012,18/02/2012

CODE:

选项明确

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

1 个答案:

答案 0 :(得分: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 ") & 
    ")"