2013-11-25 78 views
-1

我製成127個範圍的細胞,因爲它們分別位於, 的代碼是這樣的如何使用Range來計算多個單元格範圍?

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") 

它回覆錯誤消息中的SolverOK函數線

確實「對象_Global的方法範圍失敗」任何人都知道如何解決它? 感謝

+0

你想改變'c1'&'c2'&'c3'範圍或'C1:c3'細胞? –

+1

您需要創建所有這些範圍的聯合。通過將它們創建爲單獨的變量,使您的生活比使用數組更難一些... –

+1

還應該查看此鏈接 - http://www.solver.com/standard-excel-solver-處理問題大小限制#決策變量的限制您的模型中有太多更改單元格。 –

回答

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 
相關問題