Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

code for pasting a formula to a specified cell

Status
Not open for further replies.

cashmire

Programmer
Apr 29, 2004
8
FI
Hello!
I have one sheet with "raw" stock price data and one sheet with the returns calculated from the raw price data.
I have managed to create a userform that inserts a new row to both of the sheets and pastes the new prices that the user inserts in the userform, however I have not succeeded in pasting the formula ((raw!A1-raw!A2)/raw!A2) for the returns in the "returns"sheet...
My code looks like this:

Private Sub cmdsend_Click()
Sheets("raw").Activate
Cells(1, 1).Activate
Selection.EntireRow.Insert Shift:=xlDown
Sheets("raw").Range("a1") = Me.txtboxmmm.Value
Sheets("raw").Range("b1") = Me.txtboxbg.Value
Sheets("raw").Range("c1") = Me.txtboxdis.Value
Sheets("returns").Activate
Cells(1, 1).Activate
Selection.EntireRow.Insert Shift:=xlDown
Sheets("returns").Range("a1") = (Sheets("raw").Range("A3") - Sheets("raw").Range("A4")) / Sheets("raw").Range("A4")
Sheets("returns").Range("b1") = (Sheets("raw").Range("b3") - Sheets("raw").Range("b4")) / Sheets("raw").Range("b4")
Sheets("returns").Range("c1") = (Sheets("raw").Range("c3") - Sheets("raw").Range("c4")) / Sheets("raw").Range("c4")

End Sub

I cant figure out how to get the formula right in the code.
Could someone please help me?
Thanks,
Cashmire
 
Do you want the formula pasted in or the value ??
To enter a formula - you must use the FORMULA property of the range and build the formula as a string

Range("A4").formula = "Raw!A3-Raw!A4/Raw!A4"

Rgds, Geoff

Never test the depth of water with both feet

Help us to help you by reading FAQ222-2244 before you ask a question
 
Thank you very much!
It worked like a charm:)
-Cashmire
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top