嵌套for循环,步骤不起作用

时间:2016-06-09 20:33:40

标签: vba for-loop next

我有以下代码。正如你可以看到下一步的循环。两个内部循环正常工作,但外部循环根本没有运行。它似乎与我如何" dim"变量,但说实话,我不确定为什么这不起作用。为了简化我删除了内循环中的一些代码,但我认为这不会影响下一个循环。

Sub AlgorithmOptimizertest()

    Dim PBmin As Double
    Dim PBmax As Double
    Dim PBstep As Double
    Dim PRORmin As Double
    Dim PRORmax As Double
    Dim PRORstep As Double

    Dim ROEmin As Double
    Dim ROEmax As Double
    Dim ROEstep As Double

    PBmin = Sheets("ModelSummary").Range("AP6").Value
    PBmax = Sheets("ModelSummary").Range("AP7").Value
    PBstep = Sheets("ModelSummary").Range("AP8").Value
    PRORmin = Sheets("ModelSummary").Range("AO6").Value
    PRORmax = Sheets("ModelSummary").Range("AO7").Value
    PRORstep = Sheets("ModelSummary").Range("AO8").Value
    ROEmin = Sheets("ModelSummary").Range("AD6").Value
    ROEmax = Sheets("ModelSummary").Range("AD7").Value
    ROEstep = Sheets("ModelSummary").Range("AD8").Value

    For k = ROEmin To ROEmax Step ROEstep
        Range("AD5").Value = k
        For j = PRORmin To PRORmax Step PRORstep
            Range("AO5").Value = j
            For i = PBmin To PBmax Step PBstep
                Range("AP5").Value = i

                Call PorfolioBuilder

            Next i
        Next j   
    Next k

End Sub

1 个答案:

答案 0 :(得分:0)

它绝对不是代码问题。 它可能与您在单元格中的数据有关。

但要准确你应该做这样的事情:

dim wb as workbook, ws as worksheet
'put your dim's here

set wb=thisworkbook
set ws=wb.worksheets("ModelSummary")

'read your values here
ROEmin = ws.Range("AD6").Value
'also you might want to try this
'ROEMIN = ws.Cells(ws.Range("AD6").row,ws.Range("AD6").column)

'then call your loops
For k = ROEmin To ROEmax Step ROEstep
    Range("AD5").Value = k
    For j = PRORmin To PRORmax Step PRORstep
        Range("AO5").Value = j
        For i = PBmin To PBmax Step PBstep
            Range("AP5").Value = i

            Call PorfolioBuilder

        Next i
    Next j   
Next k