Hi I am Priya from India,
I am trying to get the logic in excel formula
=FALSE<=100
False will be considered as 0. if it is 0<=100 then expected output is TRUE.
But we are getting FALSE in the Microsoft Excel.
Why I am not getting True?
TRUE and FALSE can't be treated directly as numbers on Excel. However, they can be implicitly converted to numbers:
=(FALSE + 0) <= 100
Adding 0 to them does it.
The formula you mentioned in the comment, even though it seems to return a correct result - (10> = 50 AND 10 = <- 100) == FALSE, is basically incorrect, because excel calculates it as follows:
=(10>=50 & 10<=100)
=(10>="5010"<=100)
=(FALSE<=100)
= FALSE
If you want the numbers to be compared, you can use the following options:
Use the AND function:
=AND((10>=50),(10<=100))
Multiply the comparisons and check if the result is greater than zero
=(10>=50)*(10<=100)>0
Related
I got a weird case of 2 TRUE giving a FALSE.
I got this formula (I reduced it to only have the case that interest me for debugging purpose.:
=ARRAYFORMULA(
IF(ROW(JS7:JS)=7,JR7+1,
SWITCH($F7:$F,
"M",1,
"D",2,
"W",3,
"B",IF(AND(JS$7 >= 'A 2021'!$E7:$E,mod(('A 2021'!$E7:$E-JS$7),14)=0),$D7:$D,4),
"A",5,
"O",6,
0)
)
)
Basically this formula check if the row is the 7th, if yes, add 1 to the previous date to get the next day (this part is fine), else it goes into the Switch to return a value (0 to 6) based on other columns (in my case it is the "B" and should return 4).
This part in the formula return a FALSE:
AND(JS$7 >= 'A 2021'!$E7:$E,mod(('A 2021'!$E7:$E-JS$7),14)=0
yet JS$7 >= 'A 2021'!$E7:$E (checking if we are past the current date) is TRUE and mod(('A 2021'!$E7:$E-JS$7),14)=0 (using modulo to check if we are every 2 weeks) is also TRUE
I tried to replace the returned value of 4 by JS$7 >= 'A 2021'!$E7:$E to see its value and it was TRUE, same thing for the mod(). But when I tried with the AND(), it returned FALSE, yet, that AND() is made of previous parts that both returned TRUE.
Am I doing an obvious mistake here or something is fishy?
AND is not supported under ARRAYFORMULA. try:
=ARRAYFORMULA(
IF(ROW(JS7:JS)=7,JR7+1,
SWITCH($F7:$F,
"M",1,
"D",2,
"W",3,
"B",IF((JS$7 >= 'A 2021'!$E7:$E)*(MOD(('A 2021'!$E7:$E-JS$7),14)=0),$D7:$D,4),
"A",5,
"O",6,0)))
I'm trying to create an excel formula for the below logic:
If ((Value in A1 - 250000) >= 250000){
Then Value in A2 = (0.05*(250000))
}
Else{
If ((Value in A1 - 250000) <= 0){
Then Value in A2 = 0
Else{
Value in A2 = (0.05*(Value in A1-250000))
}
}
Below is the formula I wrote:
=IF(((A1-250000)>=250000), ((0.05*(250000))), IF (A1-250000)<=0, 0, (0.05*(A1-250000)))
And Excel throws following error
The formula you typed contains an error
Any help with identifying the error is appreciated.
I think this reproduces your logic:
=IF(A1>500000,0.05*250000,0.05*MAX(A1-250000,0))
or maybe even:
=0.05*MAX(0,MIN(A1-250000,250000))
although I agree that IFS might be more clear, but I think you need Office 365 for that function.
IFS is clearer:
=IFS(A1-250000 >= 250000, 0.05*250000, A1-250000 <= 0, 0, TRUE, 0.05*(A1-250000))
although if this is trying to compute something like a statistical measure, you could use an out of the box function like MEDIAN.
Reference: https://support.microsoft.com/en-us/office/ifs-function-36329a26-37b2-467c-972b-4a39bd951d45
True * True * True = -1
When I multiply an odd number of boolean variables equal to true, the result is -1. Why is this?
Isn't this:
TRUE * TRUE * TRUE
the same as this:
1 * 1 * 1?
Why does vba return a -1, and Excel return a 1 when evaluating this?
I've been using writing code like this
If Asb(TRUE * TRUE * TRUE) =1 then...
or this
If TRUE * TRUE * TRUE <> 0 then ...
I would like to understand why the code behaves this way. Thanks in advance for an explanation.
Boolean data type A data type with only two possible values, True (-1)
or False (0). Boolean variables are stored as 16-bit (2-byte) numbers.
See data type summary.
https://learn.microsoft.com/en-us/office/vba/language/glossary/vbe-glossary#data-type
You have to stick to -1 and 0 for true and false. To confirm what your language uses for true, simply assign zero to a variable (false), doesn't have to be boolean, then print not(variable) to determine what true is. This does not work the other way around unless the original value is -1. The bitwise not operator reverses all bits, and -1 is a two bit number where all bits are set, zero all bits are cleared. If you assign a variable with any other non-zero number, using bitwise not will still return true when converted to boolean.
One in binary is fifteen zeroes and a one. Flip all those bits and you get fifteen ones followed by a zero, which is a negative value because that first bit denotes negative number, but it won't be negative one. Flipping all the bits again returns it to its original value - one.
Let me know if this is not clear.
In Vba Boolean
(https://learn.microsoft.com/en-us/dotnet/api/system.boolean?view=netframework-4.8)
False : 0
True : ..., -3,-2.5,-2,-1.5, -1,1.5,2,1.5,3,......
Since Double except 0 is true, code can be written in various formats as shown below.
Sub testString()
Dim myBool As Boolean
Dim str As String
Dim n As Double
str = "Mountain"
n = InStr(str, "t") '<~~ 5
If InStr(str, "t") > 0 Then
MsgBox n
End If
'** below same
If InStr(str, "t") Then
MsgBox n
End If
'** below same
If n Then
MsgBox n
End If
End Sub
Boolean Debug test
In an HQL statement I am calculating metric_ty in my select statement and when flag_ty is 'Zero' then metric_ly takes the value 0 otherwise metric_ly is calculated.
My problem is that metric_ty gives back decimal values(eg: 54.32) while metric_ly does not(eg:54.00).
I want to know why this happens and how can i return decimal values inside an case-when loop?
select (p.total_revenue/p.total_units) as metric_ty,
(case
when flag_ty = 'Zero' THEN 0 ELSE
(total_revenue_ly/total_units_ly) END)
as metric_ly
from Com p
Is it possible to use case-when statements and get exact numbers and not rounding off?
All suggestions are most welcome
Data type is chosen based to first scalar expression (0). Problem can be solved by using 0.0 instead of 0:
SELECT (p.total_revenue/p.total_units) AS metric_ty,
(CASE
WHEN flag_ty = 'Zero' THEN 0.0
ELSE (total_revenue_ly/total_units_ly) END)
END metric_ly
FROM Com p
Im working on a wee macro to output files as a prn which will be used as an input for other software. I've had everything working properly, the only problem is that the other software requires all the integers to be printed with a decimal point.
Using excel 2007 btw
Ie if the cell value is 0 it should be printed as "0." not blank. likewise if the value is 8, it should be printed as "8." this isnt a problem with most of the values as the vast majority are like 123.45765 etc.
The code tells me its expecting a ) after the decimal point in .NumberFormat(""0."") on line 4
Windows("bdf_generator").Activate
With Worksheets(2).Range("E2:H9592").FormatConditions _
.Add(xlCellValue, xlBetween, "=0", "=9")
FormatConditions(1).NumberFormat=(""0."")
With Selection.FormatConditions(1).StopIfTrue = False
End With
End With
In your case, the line FormatConditions(1).NumberFormat=(""0."") is incorrect due to the two double quotes ("") surrounding 0..
Also note that FormatConditions(1).NumberFormat = "0." should be prepended with Selection. or else it would throw an error in runtime.
Try this instead, as I believe it is clearer (and you get intellisense):
Windows("bdf_generator").Activate
Dim r As Excel.Range
Set r = Worksheets(2).Range("E2:H9592")
Dim fc As Excel.FormatCondition
Set fc = r.FormatConditions.Add(xlCellValue, xlBetween, "=0", "=9")
fc.NumberFormat = "0."
fc.StopIfTrue = False