【发布时间】:2020-07-13 20:21:48
【问题描述】:
我在 Access VBA 中编写了一个子例程,此处显示以供参考:Looping Through Dates in strSQL in Access VBA。如下:
Sub SampleReadCurve()
Dim rs As Recordset
Dim iRow As Long, iField As Long
Dim strSQL As String
Dim CurveID As Long
Dim MarkRunID As Long
Dim MaxOfMarkAsofDate As Date
Dim userdate As String
CurveID = 15
Dim I As Integer
Dim x As Date
userdate = InputBox("Please Enter the Date (mm/dd/yyyy)")
x = userdate
For I = 0 To 150
MaxOfMarkAsofDate = x - I
strSQL = "SELECT * FROM VolatilityOutput WHERE CurveID=" & CurveID & " AND MaxOfMarkAsofDate=#" & MaxOfMarkAsofDate & "# ORDER BY MaxOfMarkasOfDate, MaturityDate"
Set rs = CurrentDb.OpenRecordset(strSQL, Type:=dbOpenDynaset, Options:=dbSeeChanges)
If rs.RecordCount <> 0 Then
rs.MoveFirst
rs.MoveLast
Dim BucketTermAmt As Long
Dim BucketTermUnit As String
Dim BucketDate As Date
Dim MarkAsOfDate As Date
Dim InterpRate As Double
BucketTermAmt = 3
BucketTermUnit = "m"
BucketDate = DateAdd(BucketTermUnit, BucketTermAmt, MaxOfMarkAsofDate)
InterpRate = CurveInterpolateRecordset(rs, BucketDate)
Debug.Print BucketDate, InterpRate
End If
Next I
End Function
运行此子例程会计算 76 个数字的范围。我想取这 76 个数字,并在下面的函数中使用它们作为“ZeroCurveInput”。
Function EWMA(ZeroCurveInput As Range, Lambda As Double) As Double
Dim vZeros() As Variant
vZeros = ZeroCurveInput
Dim Price1 As Double, Price2 As Double
Dim SumWtdRtn As Double
Dim I As Long
Dim m As Double
Dim LogRtn As Double, RtnSQ As Double, WT As Double, WtdRtn As Double
vZeros = ZeroCurveInput
m = BucketTermAmt
For I = 2 To UBound(vZeros, 1)
Price1 = Exp(-vZeros(I - 1, 1) * (m / 12))
Price2 = Exp(-vZeros(I, 1) * (m / 12))
LogRtn = Log(Price1 / Price2)
RtnSQ = LogRtn ^ 2
WT = (1 - Lambda) * Lambda ^ (I - 2)
WtdRtn = WT * RtnSQ
SumWtdRtn = SumWtdRtn + WtdRtn
Next I
EWMA = SumWtdRtn ^ (1 / 2)
End Function
我最初在 Excel VBA 中编写了这个函数,我正在尝试将它移植到 Access VBA。在 Excel 中,我只需将 76 个数字的列读取为范围并将其维度作为变量存储为数组,然后在函数中使用它。但是我不能使用 Access 中的 range 属性来做类似的事情,我不知道用什么来代替。
如何将数字存储为数组,然后将它们传递给函数?
【问题讨论】:
标签: arrays excel ms-access vba