I found this code from a book and I don't know how to place it in VBA to correctly run it. Nothing is wrong with this code but I don't know how to use it in VBA.
I am very much familiar with using VBA and MACRO Modules, dont know how to handle this Private Sub Solve1_click().
I put the code 3 macros and it gives me this error "Sub or Function not defined !!!"
Please tell me in detail where to place this code and how to properly run it.
I am very much familiar with using VBA and MACRO Modules, dont know how to handle this Private Sub Solve1_click().
I put the code 3 macros and it gives me this error "Sub or Function not defined !!!"
Please tell me in detail where to place this code and how to properly run it.
Code:
Private Sub Solve1_Click()
Dim x() As Double
Dim bl() As Double
Dim bu() As Double
Dim g() As Double
'Input the initial values and bounds from the spreadshee
For i = 0 To n - 1
x(i) = Cells(8, 2 + i).Value ' Initial values for X variables
bl(i) = Cells(2, 2 + i).Value ' Lower bounds for X variables
bu(i) = Cells(3, 2 + i).Value ' Upper bounds for X variable
Next i
objname = "my_objfun"
full_objname = ActiveWorkbook.Name & "!" & objname
'Set the name of the print function
OPTIM1.printfun_funname "printit"
'Set the objective function name
OPTIM1.objfun_funname full_objname
'Call the optimizer
OPTIM1.optimize n, nclin, ncnlin, a(0), tda, g(0), x(0), bl(0), bu(0)
objfun_value = OPTIM1.objf 'Get the value of the objective function
'Output the X variable values for the optimal solution
For i = 0 To n + 1
Cells(10, 2 + i).Value = x(i)
Next i
Cells(11, 2).Value = objfun_value ' Output the optimal value of the objective function
End Sub
Sub my_objfun(num_variables As Long)
'The objective function — any valid Visual Basic code is allowed
objective_value = x(0) * x(3) * (x(0) + x(1) + x(2)) + x(2)
End Sub
Sub printit(n As Long, it_maj_prt As Long, sol_prt As Long, maj As Long, mnr As Long, step As Double, nfun As Long, merit As Double, violtn As Double, norm_gz As Double, cond_hz As Double, x_ptr As Long)
'The user-defined print function. The user can decide the format in which any of
'the twelve arguments to printit are to be output.
Dim xp() As Double
ReDim xp(n)
If (it_maj_prt) Then 'A major iteration
.Cells(Row, 1).Value = maj 'The major iteration count
.Cells(Row, 2).Value = mnr ' The number of minor iterations of the QP subproblem
.Cells(Row, 3).Value = Format(step, "0.00E þ00") 'The step length along the
'search Direction
For i = 0 To n + 1 'Output the current X variable values
.Cells(Row, 4 + i).Value = Format(xp(i), "##.00")
Next i
'Output the value of the augmented Lagrangian merit function at the current point.
End Sub