【发布时间】:2017-07-24 13:21:19
【问题描述】:
我正在尝试编写一个代码来查看一组数据并选择日期范围内的集合并找到它的平均值。问题是我的函数返回整数而不是小数。我试图将我的函数数据类型更改为单、双和长,但这些都没有奏效。日期列表在 A 列中,而我要平均的值在 B 列中。我的代码:
Function MONTHLYAVERAGE(OneDate As Date, TwoDate As Date) As Double
Dim TwoDate_Row As Long
Dim i, j As Integer
LookupColumn = "A"
Dim EndRange As Long
EndRange = Range("A1", Range("A1").End(xlDown)).Rows.Count
For i = 1 To EndRange
If Range(LookupColumn & i).Value = TwoDate Then TwoDate_Row = i
Next i
For j = TwoDate_Row To 1 Step -1
If Range(LookupColumn & j).Value = OneDate Then OneDate_Row = j
Next j
Dim MyDateRange As Range
LookupColumn2 = "B"
Set MyDateRange = Range(LookupColumn2 & OneDate_Row & ":" & LookupColumn2 & TwoDate_Row)
MONTHLYAVERAGE = Format(Application.WorksheetFunction.Average(MyDateRange), Standard)
End Function
【问题讨论】:
-
Format(Application.WorksheetFunction.Average(MyDateRange), Standard)- 什么是Standard? -
这里有两件事可以请您创建一个消息框并提供以下行的结果 MONTHLYAVERAGE = Format(Application.WorksheetFunction.Average(MyDateRange), Standard),还将标准替换为“0.00”
-
在声明所有变量后为我工作。
"Standard"返回84.1。标准的定义是:显示千位分隔符,小数点左边至少一位,小数点右边两位。 -
你的意思是使用
"Standard"(字符串文字)吗?