如何使用Range计算多个细胞范围?

时间:2013-11-25 19:48:44

标签: vba excel-vba solver excel

我制作了127个细胞范围,因为它们分开放置, 代码就像这样

Dim c1, c2, c3 As Range
Set c1 = Range("W2: AO128")
Set c2 = Range("AR2: BJ128")
Set c3 = Range("BM2: CE128")
SolverOK SetCell:="$U$282", MaxMinVal:=2, ByChange:=Range("c1, c2, c3")

它回复了错误消息"对象的方法范围_Global失败"在solverok函数行中

有谁知道如何解决它? 感谢

2 个答案:

答案 0 :(得分:0)

数组可以让这更容易......

Sub Test()

Dim rng(1 To 127) As Range, x As Long, rngAll As Range

Set rng(1) = Range("W2: AO128")
Set rng(2) = Range("AR2: BJ128")
'....etc


For x = 1 To 127
    If Not rng(x) Is Nothing Then
        Set rngAll = Application.Union(rng(x), rngAll)
    Else
        Set rngAll = rng(x)
    End If
Next x
Debug.Print rngAll.Address()
'...
'SolverOK SetCell:="$U$282", MaxMinVal:=2, ByChange:=rngAll

End Sub

答案 1 :(得分:0)

此代码对我有用:

    SolverOK SetCell:="$U$282", MaxMinVal:=2, ByChange:="W2:AO128" & ";" & "AR2:BJ128" & ";" & "BM2:CE128"

可能有帮助

致谢