VBA - 求解器不变

时间:2017-06-01 14:58:09

标签: excel-vba loops for-loop solver vba

我正在尝试在Solver代码中使用“for”设置VBA循环,如下所示:

问题是当我尝试将String置于条件中时:

    SolverOk SetCell:=address_1, MaxMinVal:=1, ValueOf:=0, ByChange:=address_2, Engine:=1, EngineDesc:="GRG Nonlinear"
                    'SolverDelete CellRef:=address_11, Relation:=3, FormulaText:="0"
                    SolverAdd CellRef:=address_11, Relation:=3, **FormulaText:=address_12**

地址应为“AX155”,因为它读取列字母“AX”并与“155”行连接。 当我结束运行SolverBox时,它表示以下内容:enter image description here

前两个条件应为:$ AV $ 155< = $ AW $ 155和$ AU $ 155< = $ AX $ 155 它完全错了。

下面的图片是电子表格,其中包含引用的数据enter image description here

代码:

Sub interpolacao()

Dim myarray As Variant
Dim myarray_2 As String

Dim column_1, column_2, column_3, column_4, column_5, column_6 As String
Dim address_1, address_2, address_3, address_4, address_5, address_6, address_7, address_8, _
    address_9, address_10, address_11, address_12 As Variant
Dim address_13, address_14, address_15, address_16, address_17, address_18, address_19, _
    address_20, address_21, address_22, address_23, address_24 As String


    For x = 3 To 3

    myarray_2 = myarray(x)


                    column_1 = Worksheets("PRICES_ASIA_PET_FOB_WEEKLY").Cells(134, 47).Text
                    address_1 = "" & column_1 & myarray_2 & ""

                    column_2 = Worksheets("PRICES_ASIA_PET_FOB_WEEKLY").Cells(134, 46).Text
                    address_2 = "" & column_2 & myarray_2 - 3 & ":" & column_2 & myarray_2 & ""

                    column_3 = Worksheets("PRICES_ASIA_PET_FOB_WEEKLY").Cells(134, 44).Text
                    address_3 = "" & column_3 & myarray_2 & ""
                    address_4 = "" & column_3 & myarray_2 - 1 & ""
                    address_5 = "" & column_3 & myarray_2 - 2 & ""
                    address_6 = "" & column_3 & myarray_2 - 3 & ""

                    address_7 = "" & column_2 & myarray_2 & ""
                    address_8 = "" & column_2 & myarray_2 - 1 & ""
                    address_9 = "" & column_2 & myarray_2 - 2 & ""
                    address_10 = "" & column_2 & myarray_2 - 3 & ""

                    column_4 = Worksheets("PRICES_ASIA_PET_FOB_WEEKLY").Cells(134, 47).Text
                    column_5 = Worksheets("PRICES_ASIA_PET_FOB_WEEKLY").Cells(134, 50).Text
                    address_11 = "" & column_4 & myarray_2 & ""
                    address_12 = "" & column_5 & myarray_2 & ""

                    column_6 = Worksheets("PRICES_ASIA_PET_FOB_WEEKLY").Cells(134, 48).Text
                    column_7 = Worksheets("PRICES_ASIA_PET_FOB_WEEKLY").Cells(134, 49).Text
                    address_13 = "" & column_6 & myarray_2 & ""
                    address_14 = "" & column_7 & myarray_2 & ""

            SolverReset
            SolverOk SetCell:=address_1, MaxMinVal:=1, ValueOf:=0, ByChange:=address_2, Engine:=1, EngineDesc:="GRG Nonlinear"
                    'SolverDelete CellRef:=address_3, Relation:=3, FormulaText:="0"
                    SolverAdd CellRef:=address_3, Relation:=3, FormulaText:="0"
            SolverOk SetCell:=address_1, MaxMinVal:=1, ValueOf:=0, ByChange:=address_2, Engine:=1, EngineDesc:="GRG Nonlinear"
                    'SolverDelete CellRef:=address_4, Relation:=3, FormulaText:="0"
                    SolverAdd CellRef:=address_4, Relation:=3, FormulaText:="0"
            SolverOk SetCell:=address_1, MaxMinVal:=1, ValueOf:=0, ByChange:=address_2, Engine:=1, EngineDesc:="GRG Nonlinear"
                    'SolverDelete CellRef:=address_5, Relation:=3, FormulaText:="0"
                    SolverAdd CellRef:=address_5, Relation:=3, FormulaText:="0"
            SolverOk SetCell:=address_1, MaxMinVal:=1, ValueOf:=0, ByChange:=address_2, Engine:=1, EngineDesc:="GRG Nonlinear"
                    'SolverDelete CellRef:=address_6, Relation:=3, FormulaText:="0"
                    SolverAdd CellRef:=address_6, Relation:=3, FormulaText:="0"

            SolverOk SetCell:=address_1, MaxMinVal:=1, ValueOf:=0, ByChange:=address_2, Engine:=1, EngineDesc:="GRG Nonlinear"
                    'SolverDelete CellRef:=address_7, Relation:=3, FormulaText:="0"
                    SolverAdd CellRef:=address_7, Relation:=3, FormulaText:="-0.01"
            SolverOk SetCell:=address_1, MaxMinVal:=1, ValueOf:=0, ByChange:=address_2, Engine:=1, EngineDesc:="GRG Nonlinear"
                    'SolverDelete CellRef:=address_8, Relation:=3, FormulaText:="0"
                    SolverAdd CellRef:=address_8, Relation:=3, FormulaText:="-0.01"
            SolverOk SetCell:=address_1, MaxMinVal:=1, ValueOf:=0, ByChange:=address_2, Engine:=1, EngineDesc:="GRG Nonlinear"
                    'SolverDelete CellRef:=address_9, Relation:=3, FormulaText:="0"
                    SolverAdd CellRef:=address_9, Relation:=3, FormulaText:="-0.01"
            SolverOk SetCell:=address_1, MaxMinVal:=1, ValueOf:=0, ByChange:=address_2, Engine:=1, EngineDesc:="GRG Nonlinear"
                    'SolverDelete CellRef:=address_10, Relation:=3, FormulaText:="0"
                    SolverAdd CellRef:=address_10, Relation:=3, FormulaText:="-0.01"

            SolverOk SetCell:=address_1, MaxMinVal:=1, ValueOf:=0, ByChange:=address_2, Engine:=1, EngineDesc:="GRG Nonlinear"
                    'SolverDelete CellRef:=address_11, Relation:=3, FormulaText:="0"
                    SolverAdd CellRef:=address_11, Relation:=1, FormulaText:=address_12
            SolverOk SetCell:=address_1, MaxMinVal:=1, ValueOf:=0, ByChange:=address_2, Engine:=1, EngineDesc:="GRG Nonlinear"
                    'SolverDelete CellRef:=address_13, Relation:=3, FormulaText:="0"
                    SolverAdd CellRef:=address_13, Relation:=1, FormulaText:="""" & address_14 & """"

            SolverSolve UserFinish:=True
            SolverFinish 'KeepFinal:=1
你可以帮帮我吗?

0 个答案:

没有答案