How to SUM a range preevaluated by a formula - excel-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.

Related

How to add white spaces in a field of Excel which is equal to length of a longer word

I have a scenario where I want my Microsoft excel field to have the same length of the longest word in the column.
Basically lets say if I have:
ACBBASDBBADSAD
BADFDFDDF
So here I want to have the second word with less characters to have white spaces at its end to match the length of the first word.
=&" " this definitely helps but I am unable to achieve the above scenario
Consider this screenshot:
In column B the length of each cell of column A is established with the formula =len(A1) copied down.
Cell D2 has the range name MaximumLength and the formula =max(B:B).
With that in place, you can create the padded values with this formula in cell G1, copied down:
=A1&REPT("*",MaximumLength-LEN(A1))
If you don't want to use the helper column and helper cell, you can use this array formula instead:
=A2&REPT("*",MAX(LEN(A1:A15))-LEN(A2))
This formula must be confirmed with Ctrl-Shift-Enter. It is advisable to use defined ranges, not whole columns in array formulas, hence the range in LEN(A1:A15). Adjust as desired.
I've used the "*" character so it is visible. Replace it with a space " " in your scenario.
You can add this formula to count maximum characters and use on some cell, because you will need to press a command for it to work, so every cell can't contain this formula, let's say it is on Z1:
=MAX(LEN($A:$A))
Certify to press ctrl+shift+enter on the formula
Then you use this formula on your cells:=REPT(" ";Z1-LEN(A2))&A2
Edit: Sorry, anwsered late, teylyn is more complete.

excel-formula sum over 1st character within a range in which 1st character is always an integer

I have a column of survey responses to a question in which the data is an integer 1 through 5 inclusive followed by text (e.g., "5 stars - I love it"). I want to sum the integers in the 1st characters. The dirty way would be to create a new column in which all but the first character is stripped, but I thought I'd be slick and avoid this.
However, my CSE array formula seems to be summing COUNTA(...) instead of summing over the LEFT(...,1) in the cells
{=SUM(NUMBERVALUE(LEFT(AC$62:AC$9999,1)))}
I'm wondering why there's an implicit COUNTA included here and whether CSE formula is not a good way to approach this.
with data in A1 through A5, use:
=SUMPRODUCT(--LEFT(A1:A5,1))
As you see, the formula discards everything after the leading digit.
NOTE:
If you replace blanks with zeros, the formula will work.
EDIT#2:
You can avoid the helper column if you use the array formula:
=SUMPRODUCT(--(LEFT(IF(A1:A6="",0,A1:A6),1)))
Array formulas must be entered with Ctrl + Shift + Enter rather than just the Enter key.

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)

Looking up a value in a specified column

Something I've wanted to do quite a bit lately, and can't work out how to do, is MATCH in a column I pass as an argument. Essentially, I have a two dimensional array, and I want to be able to find the first occurrence of a given value in the nth column, for any given value of n, and return the row number it occurs at. Alternatively (and more-or-less equivalently), I want to be able to search in the column with a given column header. Is there any way to do this?
Effectively, I want to simulate the non-existent function =MATCH(lookup_value,lookup_array,lookup_column,[match_type])
I've kludged together a horrible bodge job using INDIRECT, which works, but offends me horribly.
=MATCH(lookup_value,INDIRECT("R"&<top of array>&"C"&<left of array>+<column reference>&":R"&<bottom of array>&"C"&<left of array>+<column reference>,FALSE),FALSE)
This formula should work for you and will avoid INDIRECT. Anytime you can avoid using Indirect, I recommend doing so.
=MATCH(lookup_value,INDEX(lookup_array,0,MATCH(lookup_header,array_headers,0)),0)
If you aren't looking up the column by column header and just have the column number, then it becomes easier:
=MATCH(lookup_value,INDEX(lookup_array,0,column_number),0)
You could do something like this:
Set findCell = ActiveSheet.Range("A:Z").Find(What:="term_to_search")
Will select a header based on your search term.
Set range = ActiveSheet.Range(findCell, findCell.Offset(DEF_MAX_ROWS, 0))
Set up a range which will search from that header down a whole column.
For column references beyond Z you might switch notation (Excel Options, Formulas, Working with formulas and check R1C1 reference style) and, assuming the value to be looked up is in 'A1' (R1C1) with the column number in 'A2' (R2C1) apply:
=MATCH(R1C1,INDIRECT("C"&R2C1,0),0)
to save some complexity in converting a string of two or three characters into the relevant column number.
Say we have a two dimensional array: B3:E17 and we wish to locate Happiness in the third column of that array.In G1 enter:
B3:E17
In G2 enter:
3
In G3 enter:
=ADDRESS(ROW(INDIRECT(G1)),COLUMN(INDIRECT(G1))+$G$2-1) & ":" & ADDRESS(ROW(INDIRECT(G1))+ROWS(INDIRECT(G1))-1,COLUMN(INDIRECT(G1))+$G$2-1)
This will display the address of that third column. Then in G4 enter:
=MATCH("Happiness",INDIRECT(G3),0)
For example:
You can specify a range in a formula using the INDIRECT function. So, for example, if you put the letter designation for the column you want to search in cell A75, you could use:
=MATCH("Value_To_Match", INDIRECT(A75 & ":" & A75), 0)
So, if the value in A75 is G, the string built up in the INDIRECT call is G:G, and the MATCH will look through column G for "Value_To_Match" and return the row number in which it's found.
Using this idea, you can put a formula into A75 that generates the column designation for the column you want to search. For example, if your column headers are all in row 1, and the header you want to search for is in A74, you can do:
=CHAR(MATCH(A74, 1:1, 0) + 64)
using the CHAR function to convert numbers into ASCII characters, so 65 becomes A, 66 becomes B, etc. Note that this will only work if you don't have columns past Z. You'd need a more fussy formula to do the right thing with AA, etc.
You can overcome the annoyances of dealing with column letters by using R1C1 notation instead, which you can activate by adding a second parameter of FALSE to the INDIRECT expression. Now, instead of specifying your column by a letter, you'll specify it using a number. This simplifies the column-finder in A75:
=MATCH(A74, 1:1, 0)
and also the INDIRECT expression in your overall MATCH:
=MATCH("Value_To_Match", INDIRECT("C" & A75, FALSE), 0)

String argument to INDIRECT

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.

Resources