如何修复VBA中的目标寻求和循环

时间:2019-07-15 10:25:41

标签: vba loops

我的代码有问题。我正在为一个学校项目制定一份退休储蓄计划,在那里我有不同的投入。输出表依赖于输出表的第一行,并进行了一些更改。我还使用目标搜索来查找第一个年度储蓄。但是,当我更改输入时,我的代码不起作用,也将不起作用...有谁可以帮助我?

Option Explicit

    Sub Savingplan()


    Dim RetAge, crntAge, numSav, fBal, tBase, CapTaxGain
    Dim Inflation, TargBal, CurrSavBal, AnnSav, DivInc
    Dim DivIncRate, CapApprRate, capAppr, yrEndBal
    Dim DivTax, TaxGains, TaxBasis, rowOffset, N



    Sheets("SavingPlan").Select
    Selection.Activate


    RetAge = Cells(15, 2).Value
    crntAge = Cells(14, 2).Value
    numSav = RetAge - crntAge

    Cells(20, 2).Value = numSav

    fBal = "G" & 24 + numSav
    tBase = "H" & 24 + numSav
    TargBal = "B" & 6



    Cells(21, 2).Formula = "=" & TargBal & " *(" & "1" & "+" & "Inflation" & ")^Numsav"
    Cells(22, 2).Formula = "=" & fBal & "-(" & fBal & "-" & _tBase & ")*CapTaxGain - B21"



    Range("B22").GoalSeek Goal:=0, ChangingCell:=Range("B19")


    'Compute output table
    Dim SavIncr, rowNum, FirstSav, YrBegBal1, DivInc1, CapAppr1, YrEndBal1,     TaxBas1
    Dim AnnSav1, CappAppr1, TaxBasis1

    FirstSav = Range("B19").Value
    CurrSavBal = Range("B7").Value
    DivIncRate = Range("B10").Value
    CapApprRate = Range("B11").Value
    DivTax = Range("B12").Value
    TaxGains = Range("B13").Value
    SavIncr = Range("B9").Value

    YrBegBal1 = CurrSavBal
    AnnSav1 = FirstSav
    DivInc1 = (YrBegBal1 + AnnSav1) * DivIncRate
    CapAppr1 = (YrBegBal1 + AnnSav1) * CapApprRate
    YrEndBal1 = (YrBegBal1 + AnnSav1 + CapAppr1) + DivInc1 * (1 - DivTax)
    TaxBasis1 = (YrBegBal1 + AnnSav1 + DivInc1) * (1 - DivTax)

    rowOffset = 25
    N = numSav - 1

    Cells(rowOffset + rowNum, 2).Value = 1
    Cells(rowOffset + rowNum, 3).Value = YrBegBal1
    Cells(rowOffset + rowNum, 4).Value = AnnSav1
    Cells(rowOffset + rowNum, 5).Value = DivInc1
    Cells(rowOffset + rowNum, 6).Value = CapAppr1
    Cells(rowOffset + rowNum, 7).Value = YrEndBal1
    Cells(rowOffset + rowNum, 8).Value = TaxBasis1


    Dim YrBegBalNew, AnnSavNew, DivIncNew, CapApprNew, YrEndBalNew, TaxBasisNew


    For rowNum = 1 To N

       YrBegBalNew = YrEndBal1
       YrEndBal1 = YrEndBalNew
       AnnSavNew = AnnSav1 * (1 + SavIncr)
       DivIncNew = (YrBegBalNew + AnnSavNew) * DivIncRate
       CapApprNew = (YrBegBalNew + AnnSavNew) * CapApprRate
       YrEndBalNew = (YrBegBalNew + AnnSavNew + CapApprNew) + DivIncNew * (1 - DivTax)
       TaxBasisNew = (TaxBasis1 + AnnSavNew + DivIncNew) * (1 - DivTax)

       YrEndBal1 = YrEndBalNew
       AnnSav1 = AnnSavNew
       TaxBasis1 = TaxBasisNew
       DivInc1 = DivIncNew
       CapAppr1 = CapApprNew

       Cells(rowOffset + rowNum, 2).Value = rowNum + 1
       Cells(rowOffset + rowNum, 3).Value = YrBegBalNew
       Cells(rowOffset + rowNum, 4).Value = AnnSavNew
       Cells(rowOffset + rowNum, 5).Value = DivIncNew
       Cells(rowOffset + rowNum, 6).Value = CapApprNew
       Cells(rowOffset + rowNum, 7).Value = YrEndBalNew
       Cells(rowOffset + rowNum, 8).Value = TaxBasisNew

       YrBegBalNew = YrEndBalNew

    Next rowNum


    End Sub

我希望能够更改输入,并且代码可以与此一起起作用。它也可以工作一次,但是当我再次运行它时就不起作用了。

0 个答案:

没有答案