2M计算的最佳方法

时间:2018-11-14 18:43:58

标签: vba nested-loops

我是入门级程序员,大部分都是自学成才的。我有一些代码正在尝试在大约2,000,000种情况下运行,以根据大约6种不同的情况(年龄,工作年限,投资策略等)找到最低价值。

当前我的代码已设置:

Sub DetermineAllReturnRequired()

Dim wsSummary As Worksheet: Set wsSummary = Worksheets("Summary")
Dim wsLowestReturn As Worksheet: Set wsLowestReturn = Worksheets("LowestReturn")

'Loop variables
Dim i As Double, j As Long, k As Long, c As Long, g As Long, w As Integer, a As Integer, y As Integer, b As Integer

'Variables same for both grades
Dim Interest As Double
Dim Years As Integer
Dim Age As Integer
Dim Found(0 To 1) As Range
Dim Arr_HighestSavings(0 To 1, 0 To 12, 20 To 40, 1 To 7, 0 To 1, 0 To 1, 0 To 1) As Double ' (c - continuation, k - years served, j - year retired, a - intersect options (60, 65, 70, 75, 80, 85, 90), g = grade, w - withdraw 4%, b - adjusted for inflation)

Application.ScreenUpdating = False

'Clears values
If wsLowestReturn.Cells(4, 2).Value <> Null Then
    For j = 20 To 40
        For k = 0 To 12
            For g = 0 To 1
                For c = 0 To 1
                    For w = 0 To 1
                        For b = 0 To 1
                            For a = 1 To 7
                                wsLowestReturn.Cells((k * 9) + (3 + a), j - 18 + c * 52 + g * 26 + b * 104 + w * 208).Value = Null
                            Next a
                        Next b
                    Next w
                Next c
            Next g
        Next k
    Next j
End If


'interest
For i = 0.05 To 0.15 Step 0.0001

    wsSummary.Range("G7") = i
    Interest = i

    ' years served when retire
    For j = 20 To 30

        wsSummary.Range("G4") = j
        Years = j

        'current years served
        For k = 0 To 12

            wsSummary.Range("G5") = k
            wsSummary.Range("G15") = k

            'grade (officer,0, enlisted, 1)
            For g = 0 To 1
                    If g = 0 Then
                        wsSummary.Range("G10") = "Officer"
                        wsSummary.Range("G6") = "22"
                    Else
                        wsSummary.Range("G10") = "Enlisted"
                        wsSummary.Range("G6") = "18"
                    End If

                'continuation pay (yes, 0, no, 1)
                For c = 0 To 1
                        If c = 0 Then
                            wsSummary.Range("G17") = "Yes"
                        Else
                            wsSummary.Range("G17") = "No"
                        End If
                    'withdrawal rate (0 = yes, no, 1)
                    For w = 0 To 1
                        If w = 0 Then
                            wsSummary.Range("G21") = "Yes"
                        Else
                            wsSummary.Range("G21") = "No"
                        End If



                Set Found(0) = wsSummary.Range("F38")

                If Found(0) = 60 Or Found(0) = 65 Or Found(0) = 70 Or Found(0) = 75 Or Found(0) = 80 Or Found(0) = 85 Or Found(0) = 90 Then
                    a = Found(0) / 5 - 11
                    b = 0
                    If Interest < Arr_HighestSavings(c, k, j, a, g, w, b) Or Arr_HighestSavings(c, k, j, a, g, w, b) = 0 Then
                        Arr_HighestSavings(c, k, j, a, g, w, b) = Interest
                        wsLowestReturn.Cells((k * 9) + 3, 1) = k
                        wsLowestReturn.Cells((k * 9) + (3 + a), j - 18 + c * 52 + g * 26 + b * 104 + w * 208) = Round(Arr_HighestSavings(c, k, j, a, g, w, b), 4)
                    End If
                End If

                Set Found(1) = wsSummary.Range("I38")

                If Found(1) = 60 Or Found(1) = 65 Or Found(1) = 70 Or Found(1) = 75 Or Found(1) = 80 Or Found(1) = 85 Or Found(1) = 90 Then
                    a = Found(1) / 5 - 11
                    b = 1
                    If Interest < Arr_HighestSavings(c, k, j, a, g, w, b) Or Arr_HighestSavings(c, k, j, a, g, w, b) = 0 Then
                        Arr_HighestSavings(c, k, j, a, g, w, b) = Interest
                        wsLowestReturn.Cells((k * 9) + 3, 1) = k
                        wsLowestReturn.Cells((k * 9) + (3 + a), j - 18 + c * 52 + g * 26 + b * 104 + w * 208) = Round(Arr_HighestSavings(c, k, j, a, g, w, b), 4)
                    End If
                End If
        'MsgBox ("Current years served " & k & " completed!")
                    Next w
                Next c
            Next g
        Next k
    Next j
'    If i = 0.07 Then
'        MsgBox ("at 7")
'    End If
Next i
'MsgBox ("Officer Calculations Are Complete")

Application.ScreenUpdating = True

MsgBox ("Calculations Are Complete at " & Now())
End Sub

我不知道如何加快速度。当前,该代码需要花费很长时间才能执行(一天/百分比)。我敢肯定,如果我能接受所有正确/错误的项目,那它将更快。或者已经确定了如何识别在j,k,g,c,w处的数组值,并跳过它。

变量的作用是更改另一张纸上的值,以确定投资等于一定金额的年龄。

感谢您提供的任何帮助。

0 个答案:

没有答案