0

我希望将单元格插入求解器宏的“bychange”部分,但我不知道如何添加所有单元格。我通过定义一个范围然后使用 range.address 来尝试它,但是当我尝试扩展它并在 by change 部分中使用更多单元格(通过定义范围)时它不起作用。

Sub Solver_button()
' Nominates ranges that will be used in the solver
Dim C_col As Range
Dim D_col As Range
Dim E_col As Range
Dim F_col As Range
Dim G_col As Range
Dim H_col as Range

On Error Resume Next
If range("$c$4").Value = "yes" Then C_col = range("$C$7")
If range("$d$4").Value = "yes" Then D_col = range("$D$7")
If range("$e$4").Value = "yes" Then E_col = range("$e$7")
If range("$f$4").Value = "yes" Then F_col = range("$f$7")
If range("$g$4").Value = "yes" Then G_col = range("$g$7")
If range("$h$4").Value = "yes" Then H_col = range("$h$7")

SolverOk SetCell:="$O$9", MaxMinVal:=2, ValueOf:=0, ByChange:=[c_col.address,d_col.address,e_col.address,f_col.address,g_col.address,h_col.address],
    Engine:=1, EngineDesc:="GRG Nonlinear"
SolverSolve
4

1 回答 1

1

未经测试:

Sub Solver_button()
' Nominates ranges that will be used in the solver
Dim rngChange As Range, c As Range

    For Each c In Range("C4:H4").Cells
        If c.Value = "yes" Then
            If rngChange Is Nothing Then
                Set rngChange = c.Offset(3, 0)
            Else
                Set rngChange = Application.Union(rngChange, _
                                           c.Offset(3, 0))
            End If
        End If
    Next c

    If Not rngChange Is Nothing Then
        SolverOk SetCell:="$O$9", MaxMinVal:=2, ValueOf:=0, _
           ByChange:=rngChange.Address(), Engine:=1, _
           EngineDesc:="GRG Nonlinear"
        'SolverSolve
    End If

End Sub
于 2013-10-10T22:00:30.730 回答