VBA macro that uses Excel solver

Job ID: 32557007

Budget: $30 – $250 CAD

Simple macro that will read some data in the spreadsheet and run 25 iterations of the solver to maximize the return of the portfolio. The calculations (returns and risk) of the portfolio are calculated in the spreadsheet. The solver will simply change the weights to maximize the return given a set number for the risk.

I have created the following macro, but it doesnt work:

Private Sub CommandButton2_Click()

Dim j As Integer
Dim NbPoints As Integer
Dim TargetVol As String

NbPoints = Nombre - 1

'find the min variance portfolio
SolverOk SetCell:=Range("Sdev").Address, MaxMinVal:=2, ByChange:=Range("Poids")
SolverAdd CellRef:=Range("SommePoids"), Relation:=2, FormulaText:=1
SolverAdd CellRef:=Range("Poids"), Relation:=1, FormulaText:=1
SolverAdd CellRef:=Range("Poids"), Relation:=3, FormulaText:=0
SolverSolve

Worksheets("FrontièreEfficiente").Range("MinVarRend") = Rend 'Paste Rend Min Var Port
Worksheets("FrontièreEfficiente").Range("MinVarSdev") = Sdev 'Paste Sdev Min Var Port


'generate efficient frontier the min variance portfolio
For j = 1 To NbPoints
TargetVol = CStr(Cells(49, j + 3).Value)
SolverOk SetCell:=Range("Rend"), MaxMinVal:=1, ByChange:=Range("Poids")
SolverAdd CellRef:=Range("Sdev"), Relation:=3, FormulaText:=TargetVol
SolverAdd CellRef:=Range("Poids"), Relation:=1, FormulaText:=1
SolverAdd CellRef:=Range("Poids"), Relation:=2, FormulaText:=0
' ajouter contraintes classes d'actifs

SolverSolve

Cells(48, j + 4).Value = Rend
Cells(50, j + 4).Value = IR

Next j


End Sub
Related categories: Visual Basic Data Processing Excel