如何使用日期范围构建Excel宏

时间:2010-09-07 17:57:15

标签: sql excel

我创建了一个From Date& excel中的日期(Sheet1)。在单元格A3下,I hv From Date :: 01-01-2010 and cell A4 To Date :: 31-08-2010。

如何将日期范围链接到以下宏: -

Sub Sales()

Dim StrSQl As String

Con = "Provider=IBMDA400;Data Source=XXX.XXX.XXX.XXX;User Id=yyyy;Password=zzzz"

Set Db = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.recordset")
Db.connectionSTring = Con
Db.Open
StrSQl = "select myuc, sum (myac) as Amount from myabc.myqwerty where mydt >= 20100101 and mydt <= 20100831 group by (mycl)"
rs.Open StrSQl, Db, 3, 3
Sheet1.Cells(10, 1).CopyFromRecordset rs
rs.Close
Set rs = Nothing
Set cn = Nothing
End Sub

请注意excel和amp;之间的日期格式。 SQL语句。两者的格式都不同。

感谢。

1 个答案:

答案 0 :(得分:1)

也许:

FromDate=Format(Sheet1.Range("A3"),"yyyymmdd")
ToDate=Format(Sheet1.Range("A4"),"yyyymmdd")
StrSQl = "select myuc, sum (myac) as Amount from myabc.myqwerty where mydt >= "
    & FromDate & " and mydt <= " & ToDate & " group by (mycl)"