Excel formula based on If logic - excel-formula

I'm trying to build a formula if the value is 15% then 0 to 5% * 2, 5% to 10% * 5, 10% to 15% * 10 and sum of the value needs to be shown as a result.

Assuming the following logic -
If value >= 15% then return 0
If value < 5% then return value * 2
If value is >= 5% and < 10% then return value * 5
If value is between >= 10% and < 15% then return value * 10
You could try -
=IF((percentage) >= 0.15, actualValue * 0,
IF((percentage) < 0.05, actualValue * 2,
IF(AND((percentage) >= 0.05, (percentage) < 0.1), actualValue * 5,
IF(AND(((percentage) >= 0.1), ((percentage) < 0.15)), actualValue * 10))))

Related

its a exercise with a solution plz explain me a if statement in this

hours = int(input("Enter the hours: "))
rate = float(input("Enter the rate: "))
if hours > 40:
pay = (hours - 40) * rate * 1.5 + 40 * rate
else:
pay = hours * rate
print("pay: ", pay)
If hours greater than 40
if hours > 40:
pay = (hours - 40) * rate * 1.5 + 40 * rate
If hours less than or equal to 40
else:
pay = hours * rate

Python - How to compute the value of n+nn+nnn+nnnn with a given digit as the value of n

Write a program that computes the value of n+nn+nnn+nnnn with a given digit as the value of n.
For example, if n=9 , then you have to find the value of 9+99+999+9999
I need some pointers to make this code dynamic in nature.... Kindly let me know
taking into account k terms and a value n :
(10**np.arange(k)).cumsum().sum()*n
Example
k=4
n=1
(10**np.arange(k)).cumsum().sum()*n
#1234
I assume that parameters are:
k - the number of numbers to sum (e.g. in the title of your post
there are 4 numbers to sum),
n - the digit with increasing number number of occurrences in each
number to sum.
Then the function counting such a sum can be expressed as:
def mySum(k, n):
return n * sum([ n1 * n2 for n1, n2 in zip(
[ i + 1 for i in range(k) ],
[ 10 ** (k - i - 1) for i in range(k) ])])
E.g. mySum(4, 2) gives 2468 (2 + 22 + 222 + 2222).
Details of the above case
If k == 4, but n == 1, we can break the sum into:
1 = 1
11 = 10 + 1
111 = 100 + 10 + 1
1111 = 1000 + 100 + 10 + 1
---------------------------------------------
1234 = 1000 * 1 + 100 * 2 + 10 * 3 + 1 * 4
Note that:
[ i + 1 for i in range(k) ] yields [1, 2, 3, 4],
[ 10 ** (k - i - 1) for i in range(k) ] yields [1000, 100, 10, 1],
so multiplication of these 2 zipped lists yields [1000, 200, 30, 4]
and sum of it is 1234.
Now, if n is e.g. 2, all that remains to be done is to multiply the
abobe sum just by n and this is the result.
Can try this out. Modify the range of loop as per program.
num = int(input("Enter a number: "))
t = 0
total = 0
for a in range(4):
t = num + t*10
total = total + t
print(total)

My VBA function returns a Value error when trying to run it

My custom VBA function returns a #VALUE! error when one of the input functions is SUM(##:##).
Whenever I only input the first two variables, the function runs fine. However, the third variable input is a SUM function and the function throws up an error. I think it is a problem with VBA not recognising it as an array or something but I'm unsure how to tackle this issue. When writing the function into the excel sheet itself, the error does not appear.
Function CommissionPicker(ProfitMargin, Profit, Revenue)
If ProfitMargin < 0.2 Then
CommissionPicker = 0 * Profit
ElseIf ProfitMargin < 0.25 Then
CommissionPicker = 0.05 * Profit
ElseIf ProfitMargin < 0.3 Then
CommissionPicker = 0.1 * Profit
ElseIf ProfitMargin < 0.4 Then
CommissionPicker = 0.15 * Profit
ElseIf ProfitMargin < 0.5 Then
CommissionPicker = 0.25 * Profit
ElseIf ProfitMargin < 0.6 Then
CommissionPicker = 0.33 * Profit
Else
CommissionPicker = (Profit - 0.5 * Revenue) * 0.5 + (0.5 * Revenue) * 0.33
End If
End Function
The expected result should be 0 x the profit if the ProfitMargin is below 0.2, 0.05 x the profit if the ProfitMargin is below 0.25, 0.1 x the profit if the ProfitMargin is below 0.3, 0.15 x the profit if the ProfitMargin is below 0.4, 0.25 x the profit if the Profit Margin is below 0.5, 0.33 x the profit if the ProfitMargin is below 0.6 and follow the following equation: (Profit - 0.5 * Revenue) * 0.5 + (0.5 * Revenue) * 0.33 if the ProfitMargin is above 60%. However the function returns a #VALUE! error.
The "Revenue" Variable is an excel SUM function.

This VBA function always returns an answer of 0

I am writing a function in excel to apply a commission rate to a profit dependent on a profit margin. The profit and profit margin are both inputs to the function. However whenever I run this function, it always returns 0 despite having inputs that should return other results.
I have already looked over the code multiple times and checked that my inputs are valid but cannot find the problem. The profit margin is a fraction in the form of a percentage.
A sample of my code is shown below:
'If Profit Margin is below 20% then no commission
'If Profit Margin is above 20% commission is 5%
'If Profit Margin is above 25% commission is 10%
'If Profit Margin is above 30% commission is 15%
'If Profit Margin is above 40% commission is 25%
'If Profit Margin is above 50% commission is 33%
Function CommissionPicker(ProfitMargin, Profit)
If 0.25 > ProfitMargin >= 0.2 = True Then
CommissionPicker = 0.05 * Profit
ElseIf 0.3 > ProfitMargin >= 0.25 = True Then
CommissionPicker = 0.1 * Profit
ElseIf 0.4 > ProfitMargin >= 0.3 = True Then
CommissionPicker = 0.15 * Profit
ElseIf 0.5 > ProfitMargin >= 0.4 = True Then
CommissionPicker = 0.25 * Profit
ElseIf ProfitMargin >= 0.5 = True Then
CommissionPicker = 0.33 * Profit
ElseIf ProfitMargin < 0.2 = True Then
CommissionPicker = 0 * Profit
Else
End If
End Function
I expect the output to be 0 if ProfitMargin is below 20%, 0.05 x the input profit value for ProfitMargin between 20% and 25%, 0.1 x the input profit value for ProfitMargin between 25% and 30%, 0.15 x the input profit value for ProfitMargin between 30% and 40%, 0.25 x the input profit value for ProfitMargin between 40% and 50% and 0.33 x the input profit value for ProfitMargin above 50%. However the equation consistently returns a value of 0.
#PEH's answer is correct for the syntax of the If-statement. However, I would suggest that you reorganize your code, this would simplify the logic and make it easier to read and maintain:
Function CommissionPicker(ProfitMargin, Profit)
If ProfitMargin < 0.2 Then
CommissionPicker = 0 * Profit
ElseIf ProfitMargin < 0.25 Then
CommissionPicker = 0.05 * Profit
ElseIf ProfitMargin < 0.3 Then
CommissionPicker = 0.1 * Profit
ElseIf ProfitMargin < 0.4 Then
CommissionPicker = 0.15 * Profit
ElseIf ProfitMargin < 0.5 Then
CommissionPicker = 0.25 * Profit
Else
CommissionPicker = 0.33 * Profit
End If
End Function
You cannot perform multiple < or > checks at once. You need to use And:
If ProfitMargin < 0.25 And ProfitMargin >= 0.2 Then
Additionally I recommend to specify a type for all your variables:
Function CommissionPicker(ByVal ProfitMargin As Double, ByVal Profit As Double) As Double
Explanation
Why does If 0.25 > ProfitMargin >= 0.2 = True Then fail?
Because it checks at first 0.25 > ProfitMargin which result is True or False so the next check would be eg True >= 0.2 or False >= 0.2. While True is -1 and False is 0 this is -1 >= 0.2 or 0 >= 0.2 which both is False. The last check then is False = True, so the If statement is False.
Alternativley to your code I recommend something like this
Function CommissionPicker(ByVal ProfitMargin As Double, ByVal Profit As Double) As Double
Select Case ProfitMargin
Case Is < 0.2
CommissionPicker = 0 * Profit
Case Is < 0.25
CommissionPicker = 0.05 * Profit
Case Is < 0.3
CommissionPicker = 0.1 * Profit
Case Is < 0.4
CommissionPicker = 0.15 * Profit
Case Is < 0.5
CommissionPicker = 0.25 * Profit
Case Else
CommissionPicker = 0.33 * Profit
End Select
End Function

Excel Formula Question

How do I write the formula that would mean this: If X<$999.00 then multiply by 0% If X >$1000.00 but < $1499.00 then multiply by 10% If X > $1499.00 but < $1999.00 then multiply by 15% If X > $1999.00 then multiply by $20%?
I think this meets the specifications of your question, but some things are ambiguous (your conditions exclude everything between $999.00 and $1000.00 inclusive, for example) so I guessed at what your real intent was.
This formula will return:
0 if X < $1000
10% of X if X >= $1000 and X < $1500
15% of X if X >= $1500 and X < $2000
20% of X in all other cases
=IF(X < 1000, 0, IF(X < 1500, X * 0.1, IF(X < 2000, X * 0.15, X * 0.2)))

Resources