如何在VBA中将3个VBA变量组合成日期数据类型

时间:2016-07-21 07:17:27

标签: excel vba excel-vba

'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)
  

错误:   对象不支持此方法

是否无法将VBA变量放入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

3 个答案:

答案 0 :(得分:2)

当您可以使用本机VBA功能时,请不要使用WorksheetFunctionDateSerial函数在代码中执行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天。

答案 1 :(得分:2)

显然,VBA没有使WorksheetFunction.Date可供使用。

在Rory的帮助下,我们建立了分配日期值的正确方法,如下所示:

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

在sql字符串中使用日期的正确方法是将它们包含在哈希#。

WHERE tabledate&gt; =#“&amp; fromDate&amp;”#AND tabledate&lt; =#“&amp; toDate&amp;”#“

如果tabledate是DateTime字段,那么您需要修改代码以反映日期<toDate + 1而不是<=toDate。这样您就可以获得包括toDate + TimeValue

在内的记录

这是VBA DateTime修改

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

这是新的SQL字符串

WHERE tabledate&gt; =#“&amp; fromDate&amp;”#AND tabledate&lt;#“&amp; toDate&amp;”#“

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

答案 2 :(得分:0)

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

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