Background color of table cell changes depending on 2 conditions - colors

i would like to change the background color of a cell. The change will depend on 2 conditions, if the final value of cell is >20 then green, if less than -20 then red.
table.cell(panel, 5, 0, str.tostring(finalvalue), bgcolor=close>20?color.green:color.red)'
I managed the first condition but how can I add the second condition pls
Thanks in advance

bgcolor=close>20?color.green:color.red)
Will change the backround to green if the close price is above 20 and to red otherwise.
What you need is a second ternary operator in this condition.
bgcolor = finalvalue > 20 ? color.green : finalvalue < -20 color.red : color.yellow)
In this statement, the background will be green if finalvalue is greater than 20, and it will be red if it is less than -20 and it will be yellow if it is in between.

Related

change the color and make bold the result in excel formula

How do I make the color of result "&O4&" green and the color of the result of "&P4&" red?
Here is the formula: ="Present: "&" "&O4&" Times"&" "&" Absent:"&" "&P4&" Times"
Here is the result: Present: 4 Times Absent: 0 Times
I want 4 to be green and 0 to be red in color and bold

Getting Excel to select from one of three range options

I'm trying to devise a dynamic spread sheet involving pension contribution percentages which increases with the age of the employees.
I've used the =now() function to calculate their ages on a dynamic basis and I now need to get excel to look at their age in cell H2 and apply the following criteria, altering automatically as their age increases into the next bracket:
if they are currently aged between 18 - 39 pension contribution is 6%
between 40 - 49 it is 7%
and over 50 it is 10%
The formula I've devised is picking up the correct percentages for those 39 & under and for those 50+ but I can't get it to recognize the 7% for those between 40 - 49.
Can anybody tell me where I'm going wrong?
=IF(H2>=OR18<=39,"6%",IF(H2>=OR40<=49,"7%",IF(H2>=OR50>100,"10%")))
Hi and welcome to Stack Overflow!
So assuming the value to test is in cell A1, the basic formula for matching within a range (say 1-10) is:
=IF(AND(A1 >= 1, A1 <= 10), "In Range", "Out of Range")
So expanding this to 3 ranges (18-39, 40-49, and 50+), and substituting your percentages, we get:
=IF(AND(A1 >= 18, A1 <= 39), "6%", IF(AND(A1 >= 40, A1 <= 49), "7%", IF(A1 >= 50, "10%")))
So a loose end that needs tying up is what to do if the age is less than 18 - currently this formula will produce FALSE. You might want to put something else in by adding a result for the second condition in the last test - so where the value doesn't match >= 50, e.g.
... IF(A1 >= 50, "10%", "NOT APPLICABLE") ...
or some other value that's appropriate.

EXCEL How to write a step function

How do you return different values in a cell based on which range the value entered in another cell comes under? Specifically, I am trying to make a step function.
For example:
IF G2 is ABOVE "0" BUT BELOW "1" THEN display "0.1"
IF G2 is ABOVE "0.99" BUT BELOW "5" THEN display "0.15"
IF G2 is ABOVE "4.99" BUT BELOW "15" THEN display "0.2"
IF G2 is ABOVE "14.99" BUT BELOW "30" THEN display "0.5"
IF G2 is ABOVE "29.99" BUT BELOW "100" THEN display "1.0"
IF G2 is ABOVE "99.99" THEN display "1.30"
So IF G2 was "£18.75" then the cell that this formula is entered in would display "£0.50" based on the value's above.
(bear in mind that this is specific to my spreadsheet and was for calculating prices i.e. 0.99 = £0.99)
Following #oli_taz's suggestion, here is a slightly more robust solution that can deal with any input:
=IF(D4<F4, 0, VLOOKUP(D4,F4:G9,2))
with the range F4:G9:
0 0.1
1 0.15
5 0.2
15 0.5
30 1
100 1.3
and D4 being the value in question, e.g. 18.75 -> result: 0.5
Numbers smaller than 0 will return 0 and numbers larger than 100 will return 1.3.
Nested if's in Excel Are ugly:
=If(G2 < 1, .1, IF(G2 < 5,.15,if(G2 < 15,.2,if(G2 < 30,.5,if(G2 < 100,.1,1.3)))))
That should cover it.

Changing Bar colors using VBA based on category label

I have a VBA code in excel to change colors of bar graph but its not working for category series.
ActiveChart.SeriesCollection(1).Interior.Color = RGB(0, 153, 64)
I want to change the color of a single bar. However, the above code changes the color of all bars.
For all bars I want one color (green) except for two bars representing two data points (Average1 and average2); these should be of a different color. Can anyone please tell me how to to this with VBA?
Jesse's answer is often the cleanest way to do it.
However, it is not accurate that "to have different colored bars they must be on different series" (my emphasis). You can mix and match colors within one series. For example, this makes the second bar of the first series red:
ActiveChart.SeriesCollection(1).Points(2).Interior.Color = RGB(255, 0, 0)
You can use this to do all kinds of neat tricks, such as highlighting bars that exceed some threshold, are associated with leap years, or whatever. You could certainly choose to highlight your average1 and average2 values this way.
If you want to change the color for a point that has a given characteristic, then you have to loop through all points until you find a point that has that characteristic. For example, if you want to color in red the point whose category (XValue) is "avg" then you could do this:
Dim c As Chart
Dim s As Series
Dim iPoint As Long
Dim nPoint As Long
Set c = ActiveChart
Set s = c.SeriesCollection(1)
nPoint = s.Points.Count
For iPoint = 1 To nPoint
If s.XValues(iPoint) = "avg" Then
s.Points(iPoint).Interior.Color = RGB(255, 0, 0)
End If
Next iPoint
Your problem isn't with the VBA, to have different colored bars they must be on different series.
Leave a gap in the base colored series and add the values you want colored on a second series and color that. Your data would look something like this:
Series | Month 1 | Month 2 | Month 3 | Month 4 | Month 5 | Month 6 | Month 7
1 10 12 15 14 10
2 17
3 18

How do I create a formula for "if x ≥ then multiply by y" and so on?

I've only used Excel for the basics.
I want to multiply the contents of the cell by a different number depending on the value in the cell. I have these ranges:
0 - 499, then multiply by 0
500 - 999, then multiply by 1
1000 - 1499, then multiply by 4
I was able to figure out the formula =IF(C21>=10000,C21*1) for if a value in cell C21 is greater than or equal to 10,000, but I don't see how to extend that to multiple ranges.
How can I write a formula to handle the multiple ranges I've listed above?
You can use another IF in the ELSE part of the expression, evaluation will stop as soon as a TRUE condition is met;
=A1 * IF(A1 < 500, 0, IF(A1 < 1000, 1, IF(A1 < 1500, 4, 0)))
(The last 0 is the case when the value is > 1499)
You can use nested IF statements for doing ranges:
=IF(C21>=500,IF(C21>=1000,IF(C21<1500,C21*4,'dontknowwhatyouwanthere'),C21*1),0)
How about nested Ifs?
=IF(A1<1000;IF(A1<500;+A1*0;+A1*1);+A1*4)
Then you've got:
If it's less than 1000 another if:
If it's less than 500 You do the " * 0 "
If it's not (you are at 500-999 range, from the first if) You do the " * 1 "
Else it's not less than 1000:
You have your " * 4 "
I used this formula and it worked:
=V4*IF(V4<600,0.2,IF(V4<800,0.22,IF(V4<1000,0.25,IF(V4<10000,0.27))))

Resources