Subtract amount if outcome is above threshold, without duplicate formula [duplicate] - excel-formula

This question already has answers here:
Are there such things as variables within an Excel formula?
(9 answers)
Closed 3 years ago.
I have a lengthy formula F and want to subtract amount A if outcome of F is > A. Else the function should return the value of the formula.
I can just write out straightforward (pseudo code)
= IF ((F()>A), F()-A, F())
Drawback is that I have to write out formula F() three times. If the formula changes, I have to patch it on three locations.
Ideally, I would have a temporary result-value like this (pseudo code)
= LET $X = F(); IF (($X>A), $X-A, $X)
Of course I can do that with storing the outcome of F() in an extra cell, but I am searching for an elegant solution, all in just 1 cell.

I have an option to reduce it from three times to only two:
= F() - IF ((F()>A), A, 0)

The same problem was already discussed here on SO. (I did check for it when posting, but did not find it then.)
So I consider this question answered. (Spoiler: VLOOKUP helps, but costs extra cell too) Thanks AbdelAziz and Forward Ed.
Are there such things as variables within an Excel formula?

Related

Combining TEXTSPLIT with BYROW to return a spilled array [duplicate]

This question already has an answer here:
TEXTSPLIT combined with BYROW returns an unexpected result when using an array of strings as input
(1 answer)
Closed 3 months ago.
I can use TEXTSPLIT() on an individual cell with no problem, but I want to use a single formula to split a list of names.
This works:
However, this doesn't:
What am I doing wrong? BYROW() works fine with most functions, but it doesn't like TEXTSPLIT() for some reason.
What is the solution?
The error is due to the fact that, when using BYROW, the LAMBDA must return only a single value, not two values.
There may be more efficient methods, but one way of getting your desired output to spill over the range with just a single formula:
=HSTACK(TEXTBEFORE(F2:F4," "),TEXTAFTER(F2:F4," "))
REDUCE() could be good choice in this case. Reduce function can output result dynamically expanded both horizontally and vertically.
=IFERROR(DROP(REDUCE(0,A2:INDEX(A2:A50000,COUNTA(A2:A50000)),LAMBDA(a,b,VSTACK(a,TEXTSPLIT(b," ")))),1),"")
See this answer by #JvdV to one of my question. This is more efficient to handle multi delimiter.

Excel functions: indirect address column

First time asking a question after reading a lot in the past.
I'm running the following array function excel:
INDEX('Available Options'!$A$1:$CM$137,$B$1,
SMALL(
IF(
INDIRECT("'Feasibility Options'!"&ADDRESS($B$1,COLUMN('Feasibility Options'!$G$1),1,1)&":"&ADDRESS($B$1,COLUMN('Feasibility Options'!$O$1),1,1))=2,
COLUMN(INDIRECT("'Feasibility Options'!"&ADDRESS($B$1,COLUMN('Feasibility Options'!$G$1),1,1)&":"&ADDRESS($B$1,COLUMN('Feasibility Options'!$O$1),1,1)))),
ROW('Available Options'!1:1)))
The idea behind this is that there are a number of cells which either have 1s, 2s or 3s in (1 means default, 2 means an alternative and 3 means inactive) in a separate tab called 'Feasibility Options', and the prices for these options are held in the 'Available Options' tab. $B$1 contains a row number produced through a separate calculation.
Evaluating this formula gives a value error when resolving the Indirect part of the function, but the function works if I replace the column number formulae with column numbers as below:
INDEX('Available Options'!$A$1:$CM$137,$B$1,
SMALL(
IF(INDIRECT("'Feasibility Options'!"&ADDRESS($B$1,7,1,1)&":"&ADDRESS($B$1,15,1,1))=2,COLUMN(INDIRECT("'Feasibility Options'!"&ADDRESS($B$1,7,1,1)&":"&ADDRESS($B$1,15,1,1)))),ROW('Available Options'!1:1)))
Could someone explain why this would happen and how to fix it? I ideally need to use cell references rather than hardcoded column numbers as I will likely need to add more columns in at a later date.
Thanks for any help you can give and apologies if I've missed a previous thread which answers this sort of question.
I haven't tested this but I suspect the problem is caused by the COLUMN function which returns an array (even for a single value) and sometimes Excel has trouble processing this:
You can normally make it work by wrapping COLUMN in a SUM or MAX function, e.g.
MAX(COLUMN('Feasibility Options'!$G$1))
or using COLUMNS function instead, something like
COLUMNS('Feasibility Options'!$G$1:G1)
which can be dragged to increment
Better still would be to replace the whole INDIRECT/ADDRESS parts with INDEX, which should be simpler, shorter and more efficient. I can give you more detail on that if you're interested

Excel - SUMPRODUCT to count if sum of columns is >0 [duplicate]

This question already has answers here:
how to count multiple columns using countifs
(3 answers)
Closed 7 years ago.
I have a very similar problem to another question I found which was submitted a year ago:
how to count multiple columns using countifs
I have data over several columns as either "0" or "1", and what I would like to do is count how many of these rows of data have at least one "1" in them.
I could always create an extra column as a sum of all results for that row, but I was wondering if there was a way around it.
I have tried the SUMPRODUCT formulae as specified in the first answer for the question linked above which would be perfect if it worked, however I have found this only works if I use data stored as text eg. X and Y. When I switch my data back to "0"s and "1"s all the formula returns is 0.
I'm not sure where to go from here, whether I can still use SUMPRODUCT, or if I can find a way using SUMIFS, COUNTIFS, or logical operators somehow. Thanks in advance for any help.
You can do this in one step with a complicated array function. The following gets the number of rows that are all zeros:
{sum((1 - B1:B10) * (1 - C1:C10) * . . . )}
You can subtract this from the total number to get the rows that have at least one 1. Essentially:
(count(A1:A10) - sum((1 - B1:B10) * (1 - C1:C10) * . . . )}

Speeding up VLOOKUP [duplicate]

This question already has answers here:
How to optimize vlookup for high search count ? (alternatives to VLOOKUP)
(4 answers)
Closed 7 years ago.
I have the following formula:
=VLOOKUP(VLOOKUP(A1,[Clients]Sales!$B$1:$C$6,2,0),[Ledger]Sheet1!$G$1:$H$6,2,0)
that is working but I have over 100k lines of data and it is taking a few minutes to extract all the results.
Can it be speeded up?
Is VBA the quickest option?
It is difficult to completely answer your questions without sample data, but I think this is what you are looking for: How to optimize vlookup for high search count ? (alternatives to VLOOKUP)
If you are not familiar with VBA, I would definitely look into using INDEX-MATCH.
Hope this helps!
I imagine you'll want to use index-match. It's a pair of functions that can replicate vlookup and more, but is much faster than vlookup. I almost never use vookup for this reason any more.
I think what you want looks like below. No promises without seeing your workbook though.
=index([Ledger]Sheet1!$H$1:$H$6,match(index([Clients]Sales!$C$1:$C$6,match(A1,[Clients]Sales!$B$1:$B$6,0)),[Ledger]Sheet1!$G$1:$G$6,0))
The explanation is index pulls the nth item from a specific column. Match finds what n that happens to be.

How do I randomly select an integer from an array, that excludes each previous selection? [duplicate]

This question already has answers here:
Unique Random Numbers using VBA
(3 answers)
Closed 8 years ago.
I'm trying to make a set box of cells populate in a random appearing order. I have it just running through way more iterations than necessary so that the entire box populates. I wish to exclude previous selections to make the process quicker and more fluid. Thank you!
I am sure this has millins of anwers. what I will explain you what I would do. then you can paste your code and if you have problems someone can help you.
I would make a routine that sorts your array randomly, how? add to your array another column where you will place a ramdon number generated by excel. then sort your array according to your random number. I will make an example:
Original array:
(your int column,Rand column)
(1,0.3212)
(2,0.7423)
(3,0.0311)
(4,0.03144)
(5,0.952)
then if you order it you will get something like this:
5
2
1
4
3
then all you have to do is return next item in your array and of cource they wil never repeat.
I think this is a really fast way.
Hope it helps

Resources