String argument to INDIRECT - excel

I have an unwieldy cell argument along the lines of:
COUNTIFS(E1:E300,"5",$O$2:$O$300,"Apple")
I would like to use INDIRECT as follows:
COUNTIFS(INDIRECT("E"&A1&":E"&A2),"5",INDIRECT("O"&A1&":O"&A2),"Apple") INDIRECT("E"&A1&":E"&A2))
Where A1 holds 1 and A2 holds 300. But again this is unwieldy, Ideally instead of A1 and A2, I would have a cell A3 containing. Similarly for A4
"E"&A1&":E"&A2
Then use
COUNTIFS(INDIRECT(A3),"5",INDIRECT(A4),"Apple") INDIRECT(A3))
Is this possible?
I can get it to work with a simple string like, but as soon as I add an & then it no longer works.

You don't say how 'it no longer works'.
Your logic and example works fine for me although I'm not sure what the final 'stray' INDIRECT statement is in your code lines?
If there is more than one criterion, COUNTIFS returns the count of the ANDed criteria not the sum of the counts.

Related

Convert text formula of a function to a 'real' formula

I have in cell A1 the formula VLOOKUP(A2;Table3[#All];3;FALSE). I am struggling to find a way to output the result of the previous formula in B1.
So apparently, you can calculate a text formula using the function "Evaluate" accessible only with VBA like so :
Function Eval(Ref As String)
Application.Volatile
Eval = Evaluate(Ref)
End Function
Then you're able to call it everywhere in your sheet for example, if you put 1+2 in cell A1 and =Eval(A1) in cell B1, you get output 3 in B1. So it works fines in this case!
Works well when you 'evaluate' a simple number too:
HOWEVER
I found out that Evaluate does not work on texts that include a function (like my above formula of Vlookup).
For example if you put Eval(A1) in A2 (where 'Eval' is a function itself) then put =Eval(A2) in B2, you get "#VALUE".
Same output with above formula.
Does anybody know how to calculate a text formula like : VLOOKUP(A2;Table3[#All];3;FALSE) (without having to add "=" by hand before the formula) ?
Thank you!
I'm not sure if it's documented in a more authoritative place - this link does mention it - but Evaluate requires a comma as the argument separator in formulas, regardless of your local setting.
As far as the VLOOKUP goes, either change the ; to , in the original text, or use SUBSTITUTE and Eval the result.

Why can't I nest the ADDRESS() formula inside the ROW() formula in Excel?

=ADDRESS(3,1) 'evaluates to $A$3
=ROW($A$3) 'evaluates to 3
Why can't I nest them?
=ROW(ADDRESS(3,1)) 'Gives an error.
Try:
=ROW(INDIRECT(ADDRESS(3,1)))
instead of using ADDRESS which returns a string, consider using INDEX which will return a cell reference. The general format of INDEX is:
INDEX(Range you want to look in, rows down from top row, columns right for first column)
so in order to reference your whole sheet like address would you would need to select the range of the entire sheet:
=INDEX($A$1:$XFD$1048576,3,1)
The above formula actually returns the cell reference of $A$3 ($ is due to 3 and 1 being hard coded) then turns around and displays the contents of $A$3. As a result you don't actually see the $A$3. On the interesting side of things it also means you can define a range with INDEX(...):INDEX(...). To finish off your formula you would nest the INDEX in your ROW function as follows:
=ROW(INDEX($A$1:$XFD$1048576,3,1))
This avoids the use of the volatile function of INDIRECT and some of its other restrictions.

How to SUM a range preevaluated by a formula

Lets say we have two cells A1 and A2:
A1: A, B, 13
A2: C,B,14
Which are Strings. I want to add the values at the end of each cell-string.
Hence what I do to extract a value of a specific cell:
=VALUE(RIGHT(A1, SEARCH(",",A1))))
Returns 13 as a VALUE (or int).
Now, is there a way to add A1 and A2 in a SUM function? I could not find a way to loop through each A cell in a SUM range and work the VALUE magic on it.
A very bothersome solution would be to add all the values together like so:
=SUM(VALUE(RIGHT(A1, SEARCH(",",A1))),VALUE(RIGHT(A2, SEARCH(",",A2))))
But that does not seem optimal. I tried around with IF but it seems that what I am missing is simply the While/For loop to iterate through a range.
With data in A1 through A10, pick a cell and enter:
=SUMPRODUCT(--(TRIM(MID(SUBSTITUTE(A1:A10,",",CHAR(1),2),FIND(CHAR(1),SUBSTITUTE(A1:A10,",",CHAR(1),2))+1,9999))))
The formula isolates the string after the second comma; converts it into an integer; and then sums the integers.
(just be sure that your SUMPRODUCT() covers only cells with data and not any blank cells)
EDIT#1:
a somewhat shorter formula is:
=SUMPRODUCT(--TRIM(RIGHT(SUBSTITUTE(A1:A10,",",REPT(" ",LEN(A1:A10))),LEN(A1:A10))))
EDIT#2:
The "shorter" formula works by replacing all commas with a great number of spaces, so many spaces in fact, that when we look at the RIGHT() part of the expanded string, all we see are spaces followed by some numbers. TRIM() removes these spaces, leaving us the numbers.

Reference the location of a cell that is referenced by another cell

I am posting this question because I had a hell of a time trying to find the answer myself.
Basically I have a cell that references a cell that references another cell with some data in it. For example, A3=A2 and A2=A1 and cell A1 contains the text Hello. So cell A2 and A3 also contain the same text. See picture below:
But let's say I actually want cell A3 to show data relative to the cell position that A2 is pointing to (Remember A3=A2). I need to use the OFFSET function to do this and one would think that A3=OFFSET(A2, 0, 1) might work (click here to see how OFFSET works). But OFFSET does not work by itself. It would return the data from the cell to the right of cell A2 (shown below), instead of realizing that A2 points to A1 and then returning the data to the right of A1.
So how then do we get cell A3=B1, indirectly, by going through cell A2?
We need to use a combination of functions, one of which is INDIRECT. click here to see how INDIRECT works. So in order to use INDIRECT we need the text found in cell A2. The FORMULATEXT function (more info) will extract =A1 from cell A2 when used like so in cell A3:
A3=FORMULATEXT(A2), shown below:
Now we just need to strip off the = from the text so that we have an actual text reference that INDIRECT can use. You can do this using either the RIGHT or MID functions (right, mid) in combination with the LEN function (length). You need LEN if the number of rows in your sheet goes from single digit numbers into double digits, and so on. You also need to pass in the FORMULATEXT function again so it can compute the length of the text =A1. If you don't, it will compute the length of Hello. You also need to be aware that LEN()-1 is the correct length you need, since you are throwing away the = from the text.
For example: A3=RIGHT(FORMULATEXT(A2),LEN(FORMULATEXT(A2))-1) giving us:
Put it all together and you can OFFSET the cell that A2 references from A3 with INDIRECT like so:
A3=OFFSET(INDIRECT(RIGHT(FORMULATEXT(A2),LEN(FORMULATEXT(A2))-1)),0,1)

Have COUNTIFS ignore all blank cells (= empty cells and cells containing "")

I want to get a formula with COUNTIFS, like
=COUNTIF(A1:A3,"<>"&"")
such that when A1 = 2, A2 = "", A3 = empty, it returns 1.
Notes:
A2 contains an empty string, as the result of a formula. A3 is a blank cell, with no formulas in it.
The formula posted returns 2.
I tried using various numbers of double quotes. I always get 2.
I tried using &CHAR(34)&CHAR(34). I get 2.
The solution posted in How do I get countifs to select all non-blank cells in Excel? is what I tried, it returns 2 (not useful).
The formula would actually be =COUNTIFS(range1,cond1,range2,cond2), that is why I cannot use something like
=ROWS(A1:A3)-COUNTIF(A1:A3,"") or =ROWS(A1:A3)-COUNTBLANK(A1:A3) (see this).
range1 and range2 would come from expressions with INDIRECT, but that is probably not relevant.
I have worked it out with =SUMPRODUCT(--(expression1),--(ISNUMBER(A1:A3))), but I am specifically asking about the possibility of using COUNTIFS. Discrimination of number vs. text (e.g.) is not relevant at this point.
Blank vs. Empty string is the source of "troubles" (see, e.g., this).
Excel itself is somewhat ambiguous with respect to the definition of BLANK. In my example, ISBLANK(A2) returns FALSE, but COUNTBLANK(A2) returns 1.
I am not interested in a user Function.
Use a SUMPRODUCT function that counts the SIGN function of the LEN function of the cell contents.
    
As per your sample data, A1 has a value, A2 is a zero length string returned by a formula and A3 is truly blank.
The formula in C2 is,
=SUMPRODUCT(SIGN(LEN(A1:A3)))
I was having this exact problem, and I just found out about the "?*" wildcard which searches for any one or more characters, thus avoiding the empty string problem--genius! See Jonathan Gawrych's answer (posted right after the selected answer) here:
Excel Countif Not equal to string length of zero
Not sure if this works for the OP, since it looks like the value in A1 could need to be handled as a number not a string, but it might help anyone else who arrived here looking for a text-parsing solution.
Is using SUM instead of COUNTIFS an option? If so, I've found it to be much more flexible for filtering data sets. For example:
=SUM(IF(NOT(ISBLANK(A1:A3)),IF(NOT(ISTEXT(A1:A3)),1,0),0))
(entered as an array formula). IF(NOT(ISBLANK(x))... filters out non-blanks, then IF(NOT(ISTEXT(x))... filters out non-text. Whatever survives the filters is counted by summing 1. You can add as many filters as necessary. If you wanted to filter out only empty strings but include other text entries you could use a filter like
IF(ISTEXT(x),IF(LEN(x)>0,1,0),0)

Resources