VBA 中每月累计 FY to date 金额
Cumulative FY to date amount per month in VBA
我需要一个函数,它会自动在一个列中汇总会计年度至今的月份值。
查询的SQL如下:
SELECT "TBD" AS CATEGORY, tbl_SP.SYSTEM, tbl_SP.T3_UNIT AS ORG, tbl_SP.T1_UNIT AS Tier1_Unit, tbl_SP.UNIT_STAFF AS Tier2_Unit, tbl_SP.T3_UNIT AS Tier3_Unit, tbl_SP.OCT_O AS OCT, tbl_SP.NOV_O AS NOV, tbl_SP.DEC_O AS [DEC], tbl_SP.JAN_O AS JAN, tbl_SP.FEB_O AS FEB, tbl_SP.MAR_O AS MAR, tbl_SP.APR_O AS APR, tbl_SP.MAY_O AS MAY, tbl_SP.JUN_O AS JUN, tbl_SP.JUL_O AS JUL, tbl_SP.AUG_O AS AUG, tbl_SP.SEP_C AS SEP, Current_M([OCT_O],[NOV_O],[DEC_O],[JAN_O],[FEB_O],[MAR_O],[APR_O],[MAY_O],[JUN_O],[JUL_O],[AUG_O],[SEP_O]) AS Current_Month_SP, AcctType([MDEP],[T3_UNIT]) AS Acct_Type
FROM tbl_SP;
例如,现在是一月,所以我想对这些列求和
[OCT] + [NOV] + [DEC] + [JAN]
为了测试它,我尝试用别名粘贴它
Current_YTD_value: [OCT] + [NOV] + [DEC] + [JAN]
有效。
那么,当我在函数中使用它时,为什么下面的代码不起作用?
(我也尝试过每个月使用 'currency'
类型,但我得到一个#Error!)
我已经注释掉了其他尝试,但留下了相关的尝试。
Public Function Current_M( lJan As Long, lFeb As Long, lMar As Long, _
lApr As Long, lMay As Long, lJun As Long, _
lJul As Long, lAug As Long, lSep As Long, _
lOct As Long, lNov As Long, lDec As Long) As Currency
Dim dtToday As Date
dtToday = Now()
Select Case Month(dtToday)
Case 1
Current_M = Nz(lOct, 0) + Nz(lNov, 0) + Nz(lDec, 0) + Nz(lJan, 0)
Case 2
Current_M = Nz(lOct, 0) + Nz(lNov, 0) + Nz(lDec, 0) + Nz(lJan, 0) + Nz(lJan, 0)
End Select
End Function
我不明白为什么函数中包含Nz()
,这里举例...
Case 1
Current_M = Nz(lOct, 0) + Nz(lNov, 0) + Nz(lDec, 0) + Nz(lJan, 0)
这可能是解决您所面临问题的线索。
由于lOct、lNov、lDec、lJan都被声明为As Long
,它们不能接受Null。换句话说,当 lOct 永远不能为 Null 时,Nz(lOct, 0)
是无用的。
如果你像这样在即时window中调用你的函数...
? Current_M(Null, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
... Access 将报错 #94,"Invalid use of Null".
但是,当您从查询中调用该函数时,您看不到该错误消息。相反,当您在数据表视图中显示查询时,Access 会显示 #Error!。
如果您希望函数接受 Null 值,请将参数声明为 Variant 而不是 Long。
这是我对问题原因的最佳猜测。但是,如果您向函数提供文本值,例如 "NA",该函数也会产生错误,它不能转换为长整数。
我需要一个函数,它会自动在一个列中汇总会计年度至今的月份值。
查询的SQL如下:
SELECT "TBD" AS CATEGORY, tbl_SP.SYSTEM, tbl_SP.T3_UNIT AS ORG, tbl_SP.T1_UNIT AS Tier1_Unit, tbl_SP.UNIT_STAFF AS Tier2_Unit, tbl_SP.T3_UNIT AS Tier3_Unit, tbl_SP.OCT_O AS OCT, tbl_SP.NOV_O AS NOV, tbl_SP.DEC_O AS [DEC], tbl_SP.JAN_O AS JAN, tbl_SP.FEB_O AS FEB, tbl_SP.MAR_O AS MAR, tbl_SP.APR_O AS APR, tbl_SP.MAY_O AS MAY, tbl_SP.JUN_O AS JUN, tbl_SP.JUL_O AS JUL, tbl_SP.AUG_O AS AUG, tbl_SP.SEP_C AS SEP, Current_M([OCT_O],[NOV_O],[DEC_O],[JAN_O],[FEB_O],[MAR_O],[APR_O],[MAY_O],[JUN_O],[JUL_O],[AUG_O],[SEP_O]) AS Current_Month_SP, AcctType([MDEP],[T3_UNIT]) AS Acct_Type
FROM tbl_SP;
例如,现在是一月,所以我想对这些列求和
[OCT] + [NOV] + [DEC] + [JAN]
为了测试它,我尝试用别名粘贴它
Current_YTD_value: [OCT] + [NOV] + [DEC] + [JAN]
有效。
那么,当我在函数中使用它时,为什么下面的代码不起作用?
(我也尝试过每个月使用 'currency'
类型,但我得到一个#Error!)
我已经注释掉了其他尝试,但留下了相关的尝试。
Public Function Current_M( lJan As Long, lFeb As Long, lMar As Long, _
lApr As Long, lMay As Long, lJun As Long, _
lJul As Long, lAug As Long, lSep As Long, _
lOct As Long, lNov As Long, lDec As Long) As Currency
Dim dtToday As Date
dtToday = Now()
Select Case Month(dtToday)
Case 1
Current_M = Nz(lOct, 0) + Nz(lNov, 0) + Nz(lDec, 0) + Nz(lJan, 0)
Case 2
Current_M = Nz(lOct, 0) + Nz(lNov, 0) + Nz(lDec, 0) + Nz(lJan, 0) + Nz(lJan, 0)
End Select
End Function
我不明白为什么函数中包含Nz()
,这里举例...
Case 1
Current_M = Nz(lOct, 0) + Nz(lNov, 0) + Nz(lDec, 0) + Nz(lJan, 0)
这可能是解决您所面临问题的线索。
由于lOct、lNov、lDec、lJan都被声明为As Long
,它们不能接受Null。换句话说,当 lOct 永远不能为 Null 时,Nz(lOct, 0)
是无用的。
如果你像这样在即时window中调用你的函数...
? Current_M(Null, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
... Access 将报错 #94,"Invalid use of Null".
但是,当您从查询中调用该函数时,您看不到该错误消息。相反,当您在数据表视图中显示查询时,Access 会显示 #Error!。
如果您希望函数接受 Null 值,请将参数声明为 Variant 而不是 Long。
这是我对问题原因的最佳猜测。但是,如果您向函数提供文本值,例如 "NA",该函数也会产生错误,它不能转换为长整数。