If Formula with text and numbers

dilshod_k

Board Regular
Joined
Feb 13, 2018
Messages
79
Hi there,
I'm trying to write code for backtesting of trading strategy.
Given:
Active Worksheet with:
Range C2:C200 with range of Text values: Buy, Sell or empty cell
Range B2:B200 with Closing Price
Lets assume B2= 7.30 is a price per share


Reference Worksheet ("Control") with:
Range.B("10").Value= 1000 is a Buying power in USD
Range.B("11").Value=5 is commission per trade in USD

How to write formula with conditions as it follows:

if C2 = "Buy" Then (1000-5)/7.30 write down result in D2 - whole number!
if C2 = "Sell" Then (D2*7.30)-5 write down result in E2
if C2 = empty cell then do nothing

Thank you in advance
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
See if that does what you want.


Book1
BCDE
2$7.30Sell 992.8
Sheet2
Cell Formulas
RangeFormula
D2=IF(C2="Buy",ROUNDDOWN((Control!B10-Control!B11)/Sheet2!B2,0),"")
E2=IF(C2="Sell",(ROUNDDOWN((Control!B10-Control!B11)/B2,0))*B2,"")
 
Upvote 0
See if that does what you want.

BCDE

<colgroup><col style="width: 25pxpx"><col><col><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]2[/TD]
[TD="align: center"]$7.30[/TD]
[TD="align: center"]Sell[/TD]

[TD="align: right"]992.8[/TD]

</tbody>
Sheet2

[TABLE="width: 85%"]
<tbody>[TR]
[TD]Worksheet Formulas[TABLE="width: 100%"]
<thead>[TR="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "]
[TH="width: 10px"]Cell[/TH]
[TH="align: left"]Formula[/TH]
[/TR]
</thead><tbody>[TR]
[TH="width: 10px, bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "]D2[/TH]
[TD="align: left"]=IF(C2="Buy",ROUNDDOWN((Control!B10-Control!B11)/Sheet2!B2,0),"")[/TD]
[/TR]
[TR]
[TH="width: 10px, bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "]E2[/TH]
[TD="align: left"]=IF(C2="Sell",(ROUNDDOWN((Control!B10-Control!B11)/B2,0))*B2,"")[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
</tbody>[/TABLE]

Thanks a lot for a prompt response.
I'm afraid I was not able to use this formula as I need to use it as VBA code.
Apparently I did not described properly the whole story.
I've got excel file with VBA code that allows download of historical prices for hundreds if not thousands of shares. For each share code creates separate sheet with name=symbol of the share. After that code makes calculations (for each/and on each sheet separately) based on several indicators and gives signals: Buy, Sell or empty cell.
I tried to write code But as I'm just doing first steps in VBA coding I can't write with proper syntax, so I use combination of Macros recording with manual adjustments.
Here is the sample of the code I tried to write:

Dim ws As Worksheet


For Each ws In Sheets

If ws.Name <> "Control" And ws.Name <> "Response" Then

ws.Activate


Range("Q1").Select
Selection.NumberFormat = "?#,##0.00"
ActiveCell.FormulaR1C1 = "EQUITY"
Range("Q2").Select
ActiveCell.FormulaR1C1 = _
"=IF(AND(RC[-4].value = ""BUY"",RoundDown((Control!B10-Control!B11)/ActiveSheet!B2,0),""""))"
Range("Q2").Select
Selection.AutoFill Destination:=Range("Q2:Q150")
Range("R1").Select
Selection.NumberFormat = "?#,##0.00"
ActiveCell.FormulaR1C1 = "(USD)"
Range("R2").Select
ActiveCell.FormulaR1C1 = _
"=IF(AND(RC[-4].value = ""SELL"",Roundown((ActiveSheet!D2*ActiveSheet!B2) - Control!B11,0),""""))"
Range("R2").Select
Selection.AutoFill Destination:=Range("R2:R150")

Range("R2").Select

End If
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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