如何在VBA中将3个VBAvariables合并到date数据types中

'Dim two date variables Dim fromDate As Date Dim toDate As Date 'Dim 4 integer variables Dim fromyear As Integer Dim toyear As Integer Dim frommonth As Integer Dim ToMonth As Integer 'Store combo box value into these 4 integer variables fromyear = FromYearC.Value frommonth = FromMonthC.Value toyear = ToYearC.Value ToMonth = ToMonthC.Value 'Now i want to combine these integer and store into fromDate and toDate fromDate = WorksheetFunction.Date(fromyear, frommonth, 1) toDate = WorksheetFunction.Date(toyear, ToMonth, 31) 

错误:对象不支持此方法

无法将VBAvariables放入Excel工作表中?

更新SQL

  sSQLString = "SELECT officer ,NULL, SUM(IIF( isnumeric(mkt) = true and Survey='CPI' and Activity='FI' and Outcome= 'C', Totalmin, 0 )/468) ," & _ " SUM(IIF( isnumeric(Non) = true and Survey='CPI' and Activity='FI' and Outcome= 'C', Totalmin, 0 )/468) ,NULL ,NULL , " & _ "IIF(ISNULL(sum(mkt)),0,sum(mkt)),Sum(Non),sum(ICP),(sum(mkt)+Sum(Non)+sum(ICP) ) ,NULL,NULL,NULL,count(IIF( Survey='CPI' and Activity='FI' ," & _ "Totalmin, NULL )),NULL,count(IIF( Survey='CPI' and Activity='FI' and (Outcome ='C' OR Outcome='D'OR Outcome='O') , Totalmin, NULL )),NULL,SUM(IIF( Survey='CPI' and Activity='FI' ,Totalmin, 0 )),NULL,SUM(IIF( Survey='CPI' and Activity='FI' and (Outcome ='C' OR Outcome='D') ,Totalmin, 0 )) From (select officer ,rank ,year ,month ,day , survey ,activity ,outcome ,mkt,non,totalmin,ICP ,date from [2014$] UNION ALL select officer ,rank ,year ,month ,day , survey ,activity ,outcome ,mkt,non,totalmin,ICP ,date from [2015$] UNION ALL select officer ,rank ,year ,month ,day , survey ,activity ,outcome ,mkt,non,totalmin,ICP ,date from [2016$] UNION ALL select officer ,rank ,year ,month ,day , survey ,activity ,outcome ,mkt,non,totalmin,ICP ,date from [2017$])as table3 " & _ "where officer is not null and officer <> '' and officer <> ' ' and date >= #" & fromDate & "# AND date<=#" & toDate & "# group by officer" 

这里是sSQLString fromDate = #6/1/2016#toDate = #7/1/2016#

 SELECT officer ,NULL, SUM(IIF( isnumeric(mkt) = true and Survey='CPI' and Activity='FI' and Outcome= 'C', Totalmin, 0 )/468) , SUM(IIF( isnumeric(Non) = true and Survey='CPI' and Activity='FI' and Outcome= 'C', Totalmin, 0 )/468) ,NULL ,NULL , IIF(ISNULL(sum(mkt)),0,sum(mkt)),Sum(Non),sum(ICP),(sum(mkt)+Sum(Non)+sum(ICP) ) ,NULL,NULL,NULL,count(IIF( Survey='CPI' and Activity='FI' ,Totalmin, NULL )),NULL,count(IIF( Survey='CPI' and Activity='FI' and (Outcome ='C' OR Outcome='D'OR Outcome='O') , Totalmin, NULL )),NULL,SUM(IIF( Survey='CPI' and Activity='FI' ,Totalmin, 0 )),NULL,SUM(IIF( Survey='CPI' and Activity='FI' and (Outcome ='C' OR Outcome='D') ,Totalmin, 0 )) From (select officer ,rank ,year ,month ,day , survey ,activity ,outcome ,mkt,non,totalmin,ICP ,date from [2014$] UNION ALL select officer ,rank ,year ,month ,day , survey ,activity ,outcome ,mkt,non,totalmin,ICP ,date from [2015$] UNION ALL select officer ,rank ,year ,month ,day , survey ,activity ,outcome ,mkt,non,totalmin,ICP ,date from [2016$] UNION ALL select officer ,rank ,year ,month ,day , survey ,activity ,outcome ,mkt,non,totalmin,ICP ,date from [2017$])as table3 where officer is not null and officer <> '' and officer <> ' ' and date >= #6/1/2016# AND date<=#7/1/2016# group by officer 

显然,VBA不会使WorksheetFunction.Date可用。

在Rory的帮助下,我们build立了分配date值的正确方法是这样的:

 fromDate = DateSerial(fromyear, frommonth, 1) toDate = DateSerial(toyear, ToMonth + 1, 0) 

在sqlstring中使用date的正确方法是将其包含在散列号#中。

WHERE tabledate> =#“&fromDate&”#AND tabledate <=#“&toDate&”#“

如果tabledate是DateTime字段,则需要修改代码以反映date<toDate + 1不是<=toDate 。 这样你会得到包括toDate + TimeValue的logging。

这里是VBAdate时间修改

 fromDate = DateSerial(fromyear, frommonth, 1) toDate = DateSerial(toyear, ToMonth + 1, 1) 

这是新的SQLstring

WHERE tabledate> =#“&fromDate&”#AND tabledate <#“&toDate&”#“

testing查询的最佳方法是将其Debug.Print到即时窗口。 接下来,您将复制查询并在实际的数据库中运行它。 从那里你可以调整SQL。 最后,您需要修改您的VBA代码以反映SQL中的任何更改。

当您可以使用本机VBA函数时,不要使用WorksheetFunction 。 DateSerial函数在代码中执行Excel的Date在工作表中执行的操作:

改变这个:

 fromDate = WorksheetFunction.Date(fromyear, frommonth, 1) toDate = WorksheetFunction.Date(toyear, ToMonth, 31) 

至:

 fromDate = DateSerial(fromyear, frommonth, 1) toDate = DateSerial(toyear, ToMonth, 31) 

这里硬编码31 ,不是所有的月份都有31天。

月末使用工作表函数EoMonth ,而不是硬编码可能导致错误的date。

 Sub e(fromyear As Long, frommonth As Long, toyear As Long, tomonth As Long) Dim fromdate As Date, todate As Date fromdate = DateSerial(fromyear, frommonth, 1) todate = Application.WorksheetFunction.EoMonth(DateSerial(toyear, tomonth, 1), 0) MsgBox Format(fromdate, "yyyy-mm-dd") & " to " & Format(todate, "yyyy-mm-dd") End Sub Sub f() Call e(2011, 11, 2016, 2) End Sub