简体   繁体   中英

Excel VBA - Changing formula values

I need to make a macro that can change the values in 3 cells, run solver subject to a changing constraint, and then paste the solved value in a column. repeat.

In cells L78,79 and 81 I have references to columns L, R and P respectively.

Here's basically what I'm doing each time I update the values:

L78 = **L4**,  
L79 = **R4**,  
L81 = **P4**,

Run Solver

Objective: $M$73
By changing variable: $L$80

Constraint: $M$73=**M4**

Solve

Copy $M$73

Paste **N4**

I would then increase all the values in ** by 1 row and repeat.

First time here so I apologize for any poor formatting.

Dan

So I managed to come up with some basic VBA that did the trick. Far from perfect as it uses copy and paste (lol) but it worked for my purpose, which was to run a binomial option pricing model and collect implied volatilities for a few hundred options.

Code is:

Sub Macro1()
Worksheets("Filtered Puts").Activate
RowCount = 3
Do While Not IsEmpty(Worksheets("Filtered Puts").Range("H" & RowCount))
    Range("L" & RowCount).Select
    Selection.Copy
    Range("L78").Select
    ActiveSheet.Paste
    Range("R" & RowCount).Select
    Selection.Copy
    Range("L79").Select
    ActiveSheet.Paste
    Range("P" & RowCount).Select
    Selection.Copy
    Range("L81").Select
    ActiveSheet.Paste
    Range("O73").Select
        SolverReset
    SolverOptions precision:=0.001
    SolverOk SetCell:="$M$117", _
        MaxMinVal:=1, _
        ValueOf:=0, _
        ByChange:="$L$80"
    SolverAdd CellRef:=Range("M" & RowCount), _
        Relation:=2, _
        FormulaText:="$M$117"
    SolverSolve userFinish:=True
    SolverFinish keepFinal:=1
        Range("$L$80").Select
    Selection.Copy
    Range("N" & RowCount).Select
    ActiveSheet.Paste
    RowCount = RowCount + 1
    Loop
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM