Question : How to set up solver constraints in VBA with conditional formulas?

Is there a way to add cell reference constraints with conditional formulas? I am running solver through VBA and wanted to specify that weights equal zero if the "identity vector" to its left equals zero. This is what I have thus far, without the added constraints. I would like to add a constraint, however, that each cell from "$d$6:$d" & y be equal to zero if its corresponding cell in column A equals zero.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
y = Range("A50").Value
 
 
SolverReset
    SolverOk SetCell:="$D$3", MaxMinVal:=1, ValueOf:="0", ByChange:="$d$6:$d" & y
    SolverAdd CellRef:="$d$6:$d" & y, Relation:=3, FormulaText:="0"
    SolverAdd CellRef:="$d$6:$d" & y, Relation:=1, FormulaText:="0.035"
    SolverAdd CellRef:="$D$50", Relation:=2, FormulaText:="0.2835"
    
        
    SolverSolve
 
    
End Sub

Answer : How to set up solver constraints in VBA with conditional formulas?

Oops, that's the problem with not being able to test code - errors aren't trapped. The code below is the one to try...
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
Sub solve_it()
Dim Result As Long
Dim y As Long
Dim str1 As String
 
y = Sheets("Sheet1").[A50]
str1 = "$D$6:$D$" & y
 
'reset Solver
Application.Run "Solver.xla!Auto_Open"
Application.Run "Solver.xla!SolverReset"
 
'set up new analysis
Application.Run "Solver.xla!SolverOk", "$D$3", 1, "0", str1
 
'add constraints
Application.Run "Solver.xla!SolverAdd", str1, 3, "0"
Application.Run "Solver.xla!SolverAdd", str1, 1, "0.035"
Application.Run "Solver.xla!SolverAdd", "$D$50", 2, "0.2835"
 
'run the analysis
Result = Application.Run("Solver.xla!SolverSolve", True)
 
'finish the analysis
Application.Run "Solver.xla!SolverFinish"
    
'report on success of analysis
Select Case Result
    Case Is = 0
        Str = "Solution found, optimality and constraints satisfied"
    Case Is = 1
        Str = "Converged, constraints satisfied"
    Case Is = 2
        Str = "Cannot improve, constraints satisfied"
    Case Is = 3
        Str = "Stopped at maximum iteration"
    Case Is = 4
        Str = "Solver did not converge"
        Beep
    Case Is = 5
        Str = "No feasible solution"
        Beep
End Select
    
'report result of running Solver
MsgBox Str
    
End Sub
Random Solutions  
 
programming4us programming4us