Change excel formula syntax using indirect function - excel

I want to change formula syntax basis user selection in excel drop down. For example: replace Small with Large in =Small(F3:F16,1), if user selects Large from drop down given.
I tried ="="&INDIRECT("H2")&"(F5:F16,1)" where H2 is the drop down containing Small and Large. but its not working.

Indirect can only reference range, and it cannot change formula part. you should use If/choose statement instead.
=IF(H2="Small",SMALL(F3:F16,1),LARGE(F3:F16,1))

Instead of actually using the text itself as a function, I would just use an IF statement, i.e. something like:
= IF(H2="Large",LARGE(F5:F16,1),SMALL(F3:F16,1))

Another option is to use excel functions which use numbers to refer to functions like AGGREGATE or SUBTOTAL, e.g.
=AGGREGATE(IF(H2="LARGE",4,5),4,F3:F16)

Related

Finding a range and then implementing it in another formula

I am trying to use a variable range based on a pre-defined criteria. In my case I would like to find the range of the “AUD” cells in the table. I managed to get the beginning of the range thanks to:
=ADDRESS(1;MATCH("AUD";1:1;0))
And then I found the end of the range using a slightly modified above formula:
=ADDRESS(1;(MATCH("AUD";1:1;0)+(COUNTIF(1:1;"AUD"))-1))
Then I simply combined the results with the following formula:
=(B4&":"&C4)
And the achieved result was:
$B$1:$D$1
However, I am having difficulties implementing this result inside formulas in which range must be defined, which brings me to my following questions:
Is such kind of implementation possible in EXCEL, I suspect that the result is considered as a simple text and not actually a cell reference? Is there a way I can change that?
One step further, if we trim (for example from $B$1 to just $B) can we still make the formula working?
Due to the fact that to save space I will probably write all the above formulas inside one formula and I expect this formula to become huge, would it be possible to create a VBA public function which can store the range in a variable and then just refer this variable to the formula - for example, SUMIF("=audRefCell()";"AUD";2:2).
I would like to thank you in advance for the help!

Referencing cells in a range

I have a large number of parameters that I have grouped into a named range, MyParameters, to make it easier to pass to User Defined Functions.
In a User Defined Function I can retrieve a particular parameter using row and column notion as follows:
aParameter = MyParameters(2,2)
I now want to do the same outside of VBA (eg for use in a conditional formatting formula or feed into a normal Excel function).
Is this possible?
MyParameters(2,2)
does not work in a normal Excel cell.
If I understand correctly you can use the INDEX formula:
=INDEX(MyParameters,2,2)

Is it possible to declare a variable within formula and using it into same formula in excel?

I have a big excel formula like-
=CONCATENATE( IF( AND(LEN(A1)>3,LEN(A1)>=5),CONCATENATE( VLOOKUP(NUMBERVALUE(LEFT(RIGHT(A1,5),2)),rng,2)," হাজার "),IF( AND(LEN(A1)>3,LEN(A1)=4),CONCATENATE( VLOOKUP(NUMBERVALUE(LEFT(RIGHT(A1,4),1)),rng,2)," হাজার "),"")),IF(LEN(A1)>2,IF(NUMBERVALUE(LEFT(RIGHT(A1,3),1))>0,CONCATENATE(VLOOKUP(NUMBERVALUE(LEFT(RIGHT(A1,3),1)),rng,2),"শত "),""),""),IF(NUMBERVALUE(IF(LEN(A1)>1, RIGHT(A1,2), RIGHT(A1,1)))>0,VLOOKUP( NUMBERVALUE(IF(LEN(A1)>1, RIGHT(A1,2), RIGHT(A1,1))),rng,2,FALSE),""))
In this formula I had to use 'A1' reference again and again.
I need a function or anything like this-
=DEFVAR(A1,'somevar',CONCATENATE( IF( AND(LEN(somevar)>3,LEN(somevar)>=5),CONCATENATE(....)
So that I can use the formula on 'M9' cell like this-
=DEFVAR(M9,'somevar',CONCATENATE( IF( AND(LEN(somevar)>3,LEN(somevar)>=5),CONCATENATE(....)
Is there any way to achieve this?
No, Excel formulas don't allow you to declare variables within them.
But what you could do in this case is use another cell that holds LEN(A1), and use that in your formula. Excel's very clever calculation cycle will guarantee that LEN(A1) is evaluated before any dependent formulas.
Essentially then you are using one or more other cells to hold "variables".
This also makes your spreadsheet easier to maintain. Avoid VBA if you can; (1) it's difficult to version-control, (2) not all companies permit use of .xlsm due to security issues, (3) VBA runs in a single thread.
Your basic quetion is, "declare variable within formula and use it into the same formula".
Answer is NO.
Any Formula in Excel doesn't allows to declare variable and use it further. This is a common practice while programming and needless to say how, since somewhere you have written that you are using VBA!.
Now let me tell you how indirectly we use variables in Excel formula.
It's a Cell address which works as a variable. Other is Named Range which carries more than one value like an Array. Further more you can also use a formula as variable.
Let me say how, write a formula in cell and just Name it and use that Name in any formula like we call procedure while programming. A simple example I'm showing you here.
Write formula is A2, = if(A5>=1000,Max(B2:B100),0) and just Name it like MYMAX. Then in other formula you can call it like, =if(B2 = "A01",MYMAX,"Nothing").
Hope this help you to realize that, why we need Programming.

Dynamically format a cell

I am creating an Excel Dashboard that returns Dollar Amounts in a specific cell based on a few List Selections.
What I need to do now is have the Cell dynamically format the Dollar Amount.
For example, if it returns $3,152,234.25 I want it to display $3.2M, but if it returns $756,253.67 then I want it to display $756K and so on.
Any ideas?
You could use the following custom formatting:
[>1000000]0.0,,\M;0.0,k
Result:
You can certainly get one or the other with custom formats like this:
$0,"K";-$0,"K"
$0.00,,"M";-$0.00,,"M"
$0.00,,,"B";-$0.00,,,"B"
However, to have the conditions check for K/M/B, you would probably need a macro to dynamically set your formatting based on the values in the cells
This isn't a particularly elegant solution, but you could use a formula like this:
=IF(A1/1000000>1,TEXT(A1/1000000,"$0.0")&"M",TEXT(A1/1000,"$0")&"K")
(Assumes your number is in cell A1)

IF function - is there a way to avoid repeating formula

Can't believe I don't know this, but is there a way to avoid repeating a formula in an if statement if the logical test is dependent on it?
i.e.
=IF((SUMIFS formula)=0,"",SUMIFs formula)
I want to replace that SUMIFS function in the false scenario with something short that will tell it to just programmatically repeat the formula it originally tested for. Repeating the formula twice has to have detrimental effects on processing speed. Negligible, maybe, but want to go for best-practices here. Thanks.
You can force an error like #DIV/0! and then use IFERROR, e.g.
=IFERROR(1/(1/SUMIFS_formula),"")
You can assign a Name to a formula and use the Name..............See:
Assigning a name to a formula
Relevant excerpt -
For example, let's suppose we frequently use a formula like:
=SUM(A1:A100)-SUM(B1:B100) and this resides in A101 and is copied across many columns on row 101. It would be better in this case to
create a custom formula that does this in each cell on row 101. Here
is how;
1) Select cell A101 (this is vital).
2) Go to Insert>Name>Define and
in the "Names in workbook" box type: SalesLessCosts
3) Now click in
the "Refers to" box and type: =SUM(A1:A100)-SUM(B1:B100) then click
Add.
Now you can replace the formula in cell A101 with: =SalesLessCosts.
You can also copy this across row 101 and it will change its relative
references just as the formula =SUM(A1:A100)-SUM(B1:B100) would. The
reason it does this is all down to the fact we selected A101 before
going to Insert>Name>Define and used relative references in
=SUM(A1:A100)-SUM(B1:B100) when we added it to the "Refers to" box.
If all you need to do is hide zeroes, there is an easy way:
Select all cells where you wish to hide zeroes
Go into Custom Number Formatting
Set format to "General;General;"
The custom formatting has a structure of [positive numbers];[negative numbers];[zeroes]
By making the last part blank you are effectively hiding zeroes, but showing everything else.
The advantage over conditional formatting is that you can use this on any background.
A neat trick which I sometimes use is to hide the cell value completely by using a custom format of ";;;". This way you can put images inside the cells, like the conditional formatting ones, and not see the value at all.
Try using the SUBSTITUTE function like this :
=SUBSTITUTE( VLOOKUP( H4; $D$5:$E$8; 2; 0 ); $H$1; $I$1 )
Here is an example:
Here the formula I don't want to repeat twice is the VLOOKUP function.
The result of VLOOKUP is a string found in another table (ex : "Green").
I want to check if that string matches a specific string value in $H$1 (here, "Yellow").
If it does, SUBSTITUTE replaces it with$I$1 (the error string you want. Here, "FORBIDDEN").
If it doesn't, it displays the VLOOKUP result string (the normal authorized output, like "Green").
This is useful for me because my actual formula is quite long, so I don't want to write it twice.
I also dont want to use two different cells, because I'm already applying this formula on 10 columns, meaning I should add an extra 10 columns to make it work.
In some scenarios, MAX() or MIN() can do a wonderful job.
E.g., something like this:
=IF(SUMIFSformula>0,SUMIFSformula, 0)
Can be shortened to this:
=MAX(0,SUMIFSformula)
The LET formula can be used for this exact scenario. You can define the formula as a variable and then within that same cell you can reference the variable in your formula.
The LET formula format looks like this:
=LET(name,name_value,calculation)
SUMIFS Example
Here's how it would work with your SUMIF example so that you don't have to repeat the formula:
In this screenshot we have an array A1:B7. We want to sum the values (Col B) if the name in ColA is "apple".
For this we have a standard SUMIFS formula of
=SUMIFS(B1:B7,A1:A7,"apple")
The formula is showing in E2. The result is shown in E3.
To put this into the IF statement without having to repeat the formula we can use LET as shown in the screenshot.
We create a variable with the SUMIFS formula as the value of that variable. We then write our IF statement using the variable name instead of rewriting the formula multiple times.
=LET(name,name_value,calculation)
Variable name: sumapples
Variable value: SUMIFS(B1:B7,A1:A7,"apple")
Calculation: IF(sumapples=0,"",sumapples)
Put together in the LET function it looks like this:
=LET(sumapples,SUMIFS(B1:B7,A1:B7,"apple"),IF(sumapples=0,"",sumapples))
This LET function can be used in any Excel formula, and is very useful for shortening long formulas that have repetition.
Optional: Extra complexity
If you want to you can get extra complicated by naming multiple variables.
=LET(name,name_value,name2,name_value2,calculation)
Since Excel 2007, the IFERROR statement does what the OP asked. From the help file:
Description:
Returns a value you specify if a formula evaluates to an error; otherwise, returns the result of the formula. [italics mine]
Syntax:
IFERROR(value, value_if_error)
I've since realised that this was already answered by #barry houdini above.
Here is a hack - depending on whether you are just interested in the displayed value, or whether you need to use the value in another formula:
Put your SUMIF formula in the cell (without the IF part)
Create a conditional formatting rule which sets the font color to the background color when the cell value is 0
And hey presto, you get the desired result.
As I said - it's a hack, but it does prevent the double evaluation.
There is no "clean" solution that I am aware of.

Resources