Domanda

I made 127 ranges of cells, because they are located separately, the code is like this

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

it replies error message "method range of object _Global failed" in the solverok function line

does anyone know how to solve it? thanks

È stato utile?

Soluzione

An array would make this easier...

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

Altri suggerimenti

This code works for me :

    SolverOK SetCell:="$U$282", MaxMinVal:=2, ByChange:="W2:AO128" & ";" & "AR2:BJ128" & ";" & "BM2:CE128"
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top