Sub or Function not defined !!! How to place code in VBA

afzalw

New Member
Joined
Jul 24, 2012
Messages
20
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.


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
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
push ALT + F11.

On the left side you see a window.

Choose insert => module

Click on module and then on the right side you paste the code you posted.

You leave the editor with the red cross.

Then Excel 2007:

Developer => macro => and push on the name of your macro.
 
Upvote 0
No its all there is to be for this code. I know how to put a module but I am not familiar with the difference in a Sub() and Private Sub()
 
Upvote 0
You will need to remove the word Private for the sub to show up in the list of subs.
 
Upvote 0

Forum statistics

Threads
1,223,098
Messages
6,170,100
Members
452,301
Latest member
QualityAssurance

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top