I'm working on adding a collection of board games to our library.
I have a spreadsheet that lists more games than I intend to buy, but have them there to investigate. I have the prices for a couple of websites listed beside each game.
The problem is that I want a sum that doesn't include every game on the list. Instead, I want to add a column at the front that if I put an "X" indicating this is a game to get, then I want it to add the value for that. Only the values for rows that start with an "X" in column A will in added together, instead of everything.
I was hoping to do this with a formula instead of just choosing a basic =SUM formula and selecting only a few cells at a time. I assume a =SUMIF might be the answer, but I have yet to figure out how to get this to work.
Please try:
=SUMIF(A:A,"x",B:B)
where B:B is the column with the values. I suspect your difficulty may have been that when using text as the 'trigger' it needs to be in double inverted commas.
Or you might sort by ColumnA and just use =SUM as far down as there are xs.
Or filter on ColumnA to select x and Autosum ColumnB but change the 9 to 109.
SUMIF requires at least 2 parameters, and at most 3. In your case, you will need to use 3.
If you type the formula in excel, you will see a little balloon indicating:
=SUMIF(Range, Criteria, [Sum_Range])
Range is the range where you want to apply the condition, in your case, A:A.
Criteria is the condition applied to the range, in your case "x".
[Sum_Range] is the range to sum, after applying the condition.
The above will sum all the cells in the range [Sum_Range], for which the cells of Range satisfies the condition Criteria.
Translating that into an actual formula, you get pnut's formula:
=SUMIF(A:A,"x",B:B)
The above will sum all the cells in the range B:B, for which the cells of A:A satisfies the condition x (SUMIF considers the criterion as being 'equal' so it will sum only when they are equal to the criteria).
As an extra, you can change the condition to mean 'not equal' by using something like this: =SUMIF(A:A,"<>x",B:B) (the <> means not equal to in Excel).
Related
Hi I have a row of calculated values that I need to count based on a condition.
I have the conditions working in conditional formatting so that is the condition is met the cell changes color, however I need to find the totals of each color.
=AND($V1/$Z1>0.5, $V1/$Z1<=0.79)
so for this I want to only count values if this statement is true
=COUNTIF(Z:Z, (AND($V1/$Z1>0.5, $V1/$Z1<=0.79)))
currently this returns 0 when there is 10 values that meet this criteria
does anyone know if countif can be used like this?
I can see your issue, I've had a play around and am unable to crack it, if you wish to keep it in a singular cell you'll probably need to go down the route of an array formula. Its probably complicating it too much and you should look to have a helper column,
in Column AA, have the formula =$v1/$z1, then for your count write:
=countifs(AA:AA,">0.5",AA:AA,"<=0.79")
This can be done without a helper column using the Sumproduct() function.
=SUMPRODUCT(--($V$1:$V$100/$Z$1:$Z$100>0.5),--($V$1:$V$100/$Z$1:$Z$100<=0.79))
or
=SUMPRODUCT(($V$1:$V$100/$Z$1:$Z$100>0.5)*($V$1:$V$100/$Z$1:$Z$100<=0.79))
This will count all instances where the values in V1 to V100 divided by the values in Z1 to Z100 are between 0.5 and 0.79
Note that SumProduct is essentially an array formula (even though it does not require confirmation with Ctrl-Shift-Enter, but it evaluates each range as an array), so you don't want to use it with whole column references. If you want to retain calculation speed, make sure to refer to used rows only, not gazillions of empty cells.
What I'm wanting to do is have a formula in one cell that counts the values in a range that conform to a lookup of that range cell's value compared to another cell.
OMG, now that I look at it, that is totally confusing. Let me try to clarify a lot here.
Say we have Cell1, which will hold the counting formula. I have a list of values in a two-column table, Table1. The range, Range1 that Cell1 will be counting from is a range of cells that have List Validation in them. Table1 holds references to all values that can result from those Lists, in column 1. I have another cell, Cell2, which holds a number value. Column 2 of Table1 holds values that reference Cell2. I need to count the number of values from Range1 whose row matches in Table12 match the value in Cell2. Is there a way I can do this with COUNTIF without referencing each cell individually? Is there some shorthand (like Range.currentValue) that I can use to get the value of the cell currently being checked? The range is 11 rows long, and I need to do a second range that has 12 rows counted.
Man, I really don't know how to clarify that any more... I'll post this for now, in case anyone can understand what I'm saying and knows the answer, while I work on a sample spreadsheet I can upload.
I did my best to visually represent what I'm trying to accomplish:
http://gyazo.com/b83295baf3b156683a5c39b40c806504
Extended explanation: http://gyazo.com/4048802050e3dcfca7aee238acc2f7dd
Use a helper column, say, between the brown and the first blue or at the right of the setup. Use a vlookup like
=vlookup(brownvalue,BluetableRange,2,false)
Then do a countif on the helper column
=countif(HelperColumn,"<="&GreenCellAddress)
You can hide the column with the helper if it upsets your spreadsheet design.
You can (and probably should) use a helper column as Teylyn suggests. But, for when that may be inconvenient, you can also use an array formula:
=SUM(COUNTIFS(listlookupcolumn,rangeoflists,numbervaluecolumn,"<="&numbertomatch))
To enter it as an array formula, type "ctrl-shift-enter" after editing the formula, rather than just "enter"
Rough explanation: since rangeoflists is in a place where a single value is expected, the countifs is calculated once for each value, and the array of results is passed to sum. Use the "evaluate formula" feature to see the intermediate result array.
Afterthought: It occurs to me now that this does rely on listlookupcolumn containing unique values. (Almost certainly true in this example.) You can modify the formula a bit to get around this:
=SUM(SIGN(COUNTIFS(listlookupcolumn,rangeoflists,numbervaluecolumn,"<="&numbertomatch)))
The SIGN function will keep you from double counting.
Again, you must use "ctrl-shift-enter" for this to work. (Yes, as I'm sure others are ready to point out, you can also use the sumproduct hack in this instance.)
I have requirement where I have range for which I want to perform count. Because there is a condition, I want to use COUNTIF/COUNTIFS function. In criteria, I want to use range as I have multiple value that can be matched. Following is an example
Values to be counted - APPLE, MANGO, ORANGE, MANGO, APPLE, APPLE, ORANGE, ORANGE
What I want is count for APPLE and ORANGE for example but I want to keep it generelized. That means in another column I want to put values for which I want count. So at a time I can put only APPLE, at another time APPLE, MANGO and so on. If I'm still not clear enough then in another words, I want a count of values which are determined by the values in another range.
I could not find any way to use range in the 'criteria' parameter of the COUNTIF/COUNTIFS function. I know I can use sum of multiple COUNTIF with different value in each 'criteria' but that becomes hardcoded and everytime I need to change the formula.
Can someone help with how can I use a range (instead of single value) in 'criteria' ?
perhaps
=SUMPRODUCT(COUNTIF(data_range,criteria_range))
Let's assume your data list is in column A and your "values to be counted" occupy, say, D1:D5.
In cell B1, use =IF(ISNA(VLOOKUP(A1,$D$1:$D$5,1,FALSE)),0,1). This will write 1 if A1 appears in D1:D5, 0 otherwise.
Copy this formula downwards. Note that it will return 0 for a blank cell so you can extend this formula as far as you like thereby future-proofing your sheet.
In another cell, sum column B, =SUM(B1:B...) Where B... is the end of the formula range.
I don't think you can arrive at the final formula in one step using arrays.
I suggest you investigate the Excel formula DCOUNT
http://office.microsoft.com/en-us/excel-help/dcount-HP005209049.aspx
Microsoft even use a fruit based example for you !
CountIf accepts a range, and a criteria. The criteria can reference a range.
For example:
=CountIf(A:A,"Apple") counts the number of occurrences of the word "Apple" in column A.
And:
=CountIf(A:A,B2) counts the number of occurrences of *whatever value is in Cell B2, in column A.
If you use two cells to define the search criteria, say, B2 and C2, you could do:
=CountIf(A:A,B2)+CountIf(A:A,C2)
Or using CountIfs:
=CountIfs(A:A,B2,A:A,C2)
If you must put both search criteria in a single cell (seems like a problem of bad worksheet structure, rather than any good design requirement), then you can modify the criteria of any of the above functions using string functions:
Ex: Say your criteria of Apple, Orange is a single cell, C2, delimited by the comma:
=CountIf(A:A,Trim(MID(C2,1,FIND(",",C2)-1))) + CountIf(A:A,TRIM(MID(C2,FIND(",",C2)+1,LEN(C2)))
I still think you're better served using a proper CountIfs or summing two CountIf with indpendent criteria ranges, rather trying to troubleshoot errors, or build complex formula (i.e., modify the above to work even if there is no comma, or a single value, etc....)
i have some data on excel and i have on column H a list of solutions and on column G a target box that moves from 0 to 100, and i already have a code to generate the solutions in column H , but i want a code to check the target value on column G and checks column H for the nearest solution number and then puts the answer on column I and highlights it.
thank you very much for your time and effort.
You could do without macro, using a service column, say J, and conditional formatting:
on column I place the formula =MAX(J$1:J$100)-J1 and copy/fill till row 100
similarly fill service column J with formula =ABS(G1-H1)
use conditional formatting in column I to highlight where value=0
You probably will need other function names, because spreadsheets use localized interfaces. For instance, for my test I used LibreOffice in Italian...
HTH
I am assuming that the 'solutions' are positive numbers. If these solutions are arranged in ascending order, then you can use the following formula. If not, one solution can be to use a helper column to sort the values in ascending order.
Let your list of solutions in column H be from H1:H100, and your target box is in cell G1.
Then you can copy the following formula to the cell you want.
=IF(MAX(H1:H100)<=G1,MAX(H1:H100),IF(INDEX(H1:H100,1+MATCH(G1,H1:H100))-G1>G1-INDEX(H1:H100,MATCH(G1,H1:H100)),INDEX(H1:H100,MATCH(G1,H1:H100)),INDEX(H1:H100,1+MATCH(G1,H1:H100))))
Replace H1:H100 everywhere in the above formula, with the range in which solutions are present in ascending order. Also, replace G1 everywhere with the address of the cell with the target value.
If solutions are not in an ascending order, then you can use a helper column. You use one extra column for sorting the solutions into an ascending order. If your solutions are present in H1:H10, then in cell I1, you can enter the following formula.
=IF(ISERROR(SMALL($H$1:$H$100,ROW()-ROW($I$1)+1)),"",SMALL($H$1:$H$100,ROW()-ROW($I$1)+1))
Replace $H$1:$H$100 in the formula with the range in which solutions are present. Also, replace $I$1 with the cell address of the same cell where you are copying this formula. Here, since I am copying this formula into I1, I have used $I$1.
Now copy this formula down till where you have the solutions, e.g. for this example, you will copy it down upto I100 since your solutions are from H1:H100.
Now in the previous formula, replace H1:H100 with I1:I100, as this new list is sorted in an ascending order.
If this is not what you are looking for, maybe you could give me some more details, as your question is not very clear. Hope this helps.
P.S. : You can add the highlighting later, if you get the value that you require.
I think that it's easier to explain my problem with an example:
Based on the spreadsheet above, the formula =DSUM(A4:D8,B4,A1:A2) works, returning 20.
Why =DSUM(A4:D8,B4,{"OrderID";">10567"}) does not work? (It returns #VALUE!)
Based on what's said about D-Functions on this page, I think you need to have the criteria in a separate cell.
EDIT: If the goal of including the criteria in the formula is to make it more readable, you could work with named ranges instead.
EDIT 2: In response to your comments.
It's not possible to do what you want (include the criteria in the formula) because of how the DSUM() function works. Take a look at the documentation for DSUM and compare it with VLOOKUP:
The syntax for the DSum function is:
DSum( range, field, criteria )
range is the range of cells that you want to apply the criteria against.
field is the column to sum the values. You can either specify the numerical position of the column in the list or the column label in double quotation marks.
criteria is the range of cells that contains your criteria.
Note the difference:
The syntax for the VLookup function is:
VLookup( value, table_array, index_number, not_exact_match )
value is the value to search for in the first column of the table_array.
table_array is two or more columns of data that is sorted in ascending order.
index_number is the column number in table_array from which the matching value must be returned. The first column is 1.
As DSUM is looking for a range of cells that contain the criteria, there's nothing you can do to avoid passing it just that - a range of cells.
I think the best you can do is define your different criteria as named ranges, which will make it a lot easier to reference the different ones depending on what you want to do in the formula. Unfortunately, if the regular SUM function is not fast enough for you, there's not much else you can do - you will have to specify criteria in cells to use DSUM.
You may use SUMIF(A5:A8;>10567;D5:D8). A very useful and underused function.
The 2nd parameter may be a REF to another cell with the condition.
From DSUM documentation
Criteria is the range of cells that
contains the conditions that you
specify. You can use any range for the
criteria argument, as long as it
includes at least one column label and
at least one cell below the column
label in which you specify a condition
for the column.
VLOOKUP uses an array of values.
table_array Required. The range of
cells that contains the data. You can
use a reference to a range (for
example, A2:D8), or a range name.
Dsum sucks, try this:
=SUMPRODUCT((A5:A8>10569)*(B5:B8))
Generally I would advise you not to hard code stuff like this, it's "bad practice", but as you like.