【问题标题】:Loop in VBA Skipping Values在 VBA 中循环跳过值
【发布时间】:2016-02-10 19:43:36
【问题描述】:

我在 Access VBA 中有以下代码。

Public Sub CalculateVol()

Dim vol As Double
Dim rs As Recordset
Dim rs2 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

DoCmd.RunSQL "DELETE * FROM HolderTable"
'Clears out the old array from the holder table.

Dim I As Integer
Dim x As Date

userdate = InputBox("Please Enter the Date (mm/dd/yyyy)")

x = userdate

Dim BucketTermAmt As Long
BucketTermAmt = InputBox("Please Enter the Term Amount")

For I = 0 To 76

MaxOfMarkAsofDate = x - I



strSQL = "SELECT * FROM VolatilityOutput WHERE CurveID=" & Forms!Volatility.cboCurve.Value & " AND MaxOfMarkAsofDate=#" & MaxOfMarkAsofDate & "# ORDER BY MaxOfMarkasOfDate, MaturityDate"

Set rs = CurrentDb.OpenRecordset(strSQL, Type:=dbOpenDynaset, Options:=dbSeeChanges)
Set rs2 = CurrentDb.OpenRecordset("HolderTable")



If rs.RecordCount <> 0 Then

    rs.MoveFirst

    rs.MoveLast


    Dim BucketTermUnit As String
    Dim BucketDate As Date
    Dim MarkAsOfDate As Date
    Dim InterpRate As Double


    Dim b As String

    b = BucketTermAmt

    BucketTermUnit = Forms!Volatility.cboDate.Value
    BucketDate = DateAdd(BucketTermUnit, b, MaxOfMarkAsofDate)
    InterpRate = CurveInterpolateRecordset(rs, BucketDate)

    rs2.AddNew
    rs2("BucketDate") = BucketDate
    rs2("InterpRate") = InterpRate
    rs2.Update


End If

Next I


vol = EWMA(0.94)

Forms!Volatility!txtVol = vol

Debug.Print vol


End Sub

基本思想是用户为 MaxofMarkAsofDate 输入一个日期。然后代码在 VolatilityOutput 表中找到 MarkAsofDate 的实例,并将其用作计算 InterpRate 的参考点。它将这个数字存储在 HolderTable 中。然后它循环相同的过程,除了使用用户输入的 MarkAsofDate 的前一天,然后是前一天,依此类推,总共 76 次。

第一部分工作正常,但循环给我带来了麻烦。如果它没有在表格中找到用户输入的日期,它将跳过它,但仍将其计为循环。因此,虽然我想要 76 个数据点,但我可能只得到 56 个,例如,如果它跳过 20 个日期。所以我想阻止它跳过,或者只是继续循环直到 HolderTable 总共有 76 个数字。我该怎么做?

【问题讨论】:

    标签: ms-access vba


    【解决方案1】:

    听起来你想要一个 while 循环,因为编写的 for 循环总是会执行相同的次数。看起来您可能需要第二个计数器来增加您的日期。

    while count < 76
        'get rs here
        if rs.RecordCount <> 0 Then
            'do everything else
            count = count + 1
        end if
        dateCounter = dateCounter + 1
    loop
    

    【讨论】:

    • 谢谢,这正是我想要的。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2015-04-07
    • 2014-12-08
    • 2017-04-01
    • 2013-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多