Which method i need to use? - excel

I have the data table:
And i have the choose table:
I need some code, that can sum value by (for example :
if cells in choose table is not empty, then sum values from rows, where direction = 'East' and city = 'NY' and month = 10. (return 1)
If cells direction and month is empty, then sum values from rows,where city = 'NY' . return (4).
And etc...
If cells in choose table is empty, then sum values without limitation. (return 15).

This can be done with a SUMIFS formula. There is no need for VBA.
I'm going to assume the 'Choose' table is on another sheet in your real world, but the formula works if it's on the same sheet - I just wouldn't do it that way due to potential circular references if columns overlap.
Formula:
=SUMIFS(D:D,A:A,IF(B10="","*",B10),B:B,IF(B11="","*",B11),C:C,IF(B12="",">0",B12))
Basically, it replaces empty entries with a wildcard (where it's text) and with ">0" where the column is numerical. Tweak as you see fit.

Related

Condensing nested if-statements with multiple criteria

The blue columns is the data given and the red columns is what is being calculated. Then the table to the right is what I am referencing. So, F2 will be calculated by the following steps:
Look at the Machinery column (D), if the cell contains LF, select column K, otherwise select column L
Look at the Grade column (E), if the cell contains RG, select rows 4:8, otherwise select rows 9:12.
Look at the Species column (A), if the cell contains MS, select rows 5 and 10, otherwise.......
Where every the most selected cell is in columns K and L, copy into column F.
Multiply column F by column C.
I don't want to make another column for my final result. I did in the picture to show the two steps separately. So column F should be the final answer (F2 = 107.33). The reference table can be formatted differently as well.
At first, I tried using nested-if statements, but realized that I would have like 20+ if statements for all the different outcomes. I think I would want to use the SEARCH function to find weather of not the cell contains a specific piece of information. Then I would probably use some sort of combination of match, if, v-lookup, index, search, but I am not sure how to condense these.
Any suggestion?
SUMPRODUCT is the function you need. I quickly created some test data on the lines of what you shared like this:
Then I entered the below formula in cell F2
=SUMPRODUCT(($I$4:$I$9=E2)*($J$4:$J$9=LEFT(A2,FIND(" ",A2)-1))*IF(ISERROR(FIND("LF",D2,1)),$L$4:$L$9,$K$4:$K$9))
The formula may look a little scary but is indeed very simple as each sub formula checks for a condition that you would want to evaluate. So, for example,
($I$4:$I$9=E2)
is looking for rows that match GRADE of the current row in range $I$4:$I$9 and so on. The * ensures that the arrays thus returned are multiplied and only the value where all conditions are true remains.
Since some of your conditions require looking for partial content like in Species and Machine, I have used Left and Find functions within Sumproduct
This formula simply returns the value from either column K or L based on the matching conditions and you may easily extend it or add more conditions.

SumIF Using Table/Named Range Instead of Single Cell Criteria

I have 2 sheets in a workbook (Sheet1, Sheet2).
Sheet 2 contains a table (Named Table1) with 5 columns:
Takeaways
Household
Clothing
Fuel
Groceries
On sheet one, I have 2 columns:
Expense Name
Expense Total
Now, what I am trying to do is:
Set the range for the Expense Name (Range 1)
Set the range for the Expense Total (Range 2)
Compare Range 1 with the respective column in the table and only add up the values for matches
For example, in Range 1 (B6:B16):
BP
Caltex
McDonalds
KFC
In Range 2 (C6:C16):
300
400
200
150
Now, all I want to do is add up the values for the Takeaways (McDonalds, KFC) and exclude anything that DOES NOT match the criteria.
So my sum total will be all occurrences of Takeaways - provided they are listed in my table - 350 in this case.
But I cannot seem to get the formula to work.
I used these sources:
https://exceljet.net/excel-functions/excel-sumifs-function
Selecting a Specific Column of a Named Range for the SUMIF Function
and ended up with this formula:
=SUMIF($B$6:$B$16;Table1[Takeaways];C6:C16)
This source:
https://excelchamps.com/blog/sumif-sumifs-or-logic/
and ended up with this formula:
=SUM(SUMIFS(C6:C16;B6:B16;Table1[Takeaways]))
Both formulae return 0.
BUT, with BOTH of them, if I change Table1[Takeaways] to "McDonalds", then it correctly identifies every occurrence of the word "McDonalds" in Range 1.
EDIT:
I have updated the formulae above to match the images below.
This is the table that contains the references:
This table contains the data:
Formula:
Cell C4 (Next to Takeaways): =SUMIF($B$6:B$16;Table1[Takeaways];C6:C16)
Cell C5 (Next to Fuel): =SUM(SUMIFS(C6:C16;B6:B16;Table1[Fuel]))
It appears that ONLY BP is being detected in the formula.
This is a an output table when I use the formulae with a single cell reference and not a table or used range:
Formula:
Cell F4 (Next to BP): =SUMIF($B$6:B$16;"BP";C6:C16)
Cell F5 (Next to Caltex): =SUM(SUMIFS(C6:C16;B6:B16;"Caltex"))
Cell F6 (Next to McDonalds): =SUMIF($B$6:B$16;"McDonalds";C6:C16)
Cell F7 (Next to KFC): =SUM(SUMIFS(C6:C16;B6:B16;"KFC"))
If I understand correctly what you're trying to achieve, I think your setup is not right conceptually.
It looks like you're trying to track expenses, and each expense (or payee) is allocated to a category ("Takeaways", "Household" etc.). From a relational-model point of view, your second table (which defines the category for each expense/payee) should only have two columns (or variables): Expense Name and Expense Category.
The table you set up ('Sheet 2') uses the categories (i.e., possible values) as different columns (i.e., variables). But there's only variable, namely the "Expense Category", and the categories themselves are the possible values.
If you set it up like that, the problem changes: you can add a dependent column to your first table that shows the category for each payee (or "Expense Name"), using a VLOOKUP() from the second table.
You can then sum the expenses for all payees matching that category.
Note: I've created the illustration using LibreOffice Calc, so there might be some small differences, but the logic is the same.
Without seeing the data in L and K I can't give you a full answer - but likely it's to do with the way you're pulling your Array
Try something similar to this
=SUMPRODUCT(SUMIFS($L$11:$L$43,$K$11:$K$43,CHOOSE({1,2},Takeaways,"anything else you wanted to sum")))
Remember SUMIFS is for multiple criteria, so if you're only calculating one, you'll need =SUMPRODUCT(SUMIF(
The way the above works is with vertical vectors only, but changing your named ranges so the table of 2 columns is 2 named ranges instead should be okay - unless it's part of your requirements
Table 2 would become expense_Name and expense_Total etc
I was about to close this as a duplicate of my own question here but there is a bit of a difference in using a named range I think. However the logic behind this follows more or less the same approach.
Working further on my partial solution below I derived the following formula:
=SUMPRODUCT(COUNTIF(Table1[Takeaways];Range1)*Range2)
The COUNTIF() part counts the number of occurrences of the cell value in your table. Therefore make sure there are no duplicates in your table. If the value is present in the table the result of COUNTIF() will be 0. This way we create a matrix of 1's and 0's. By multiplying and the use of SUMPRODUCT() we force excel to perform matrix calculations and return the correct result.
Partial solution
I used the following formula:
=SUMPRODUCT(ISNUMBER(MATCH(Range1;Table1[Takeaways]))*Range2)
The formula does the following:
The MATCH()checks if the value in Range1 is present in your table and returns the position of the matching value in your table.
The ISNUMBER() checks if a match is found by checking if the MATCH() fucntion returned a number
Multiplying this with Range2 forces matrix calculation, using the SUMPRODUCT() function
EDIT:
This worked for a really limited sample. As soon as I added the fourth row to my data the formula stopped working as intended. See screenshot:
It took the first two values into the sum correctly, the fourth is not taken into account.

How can I keep the same cell reference in if function, until the if function is true/false?

I have the following function in excel:
INDIRECT(CHAR(COLUMN()+53)&O1+1)
This function has to be the outcome of an if-statement when the statement is true. I don't want the O1 to change to O2, O3, etc.. when I drag the function down, until the statement is true. From there I want the function to change this cell reference in ascending order. So as long as the if statement is false, the reference needs to be O1.
I know that absolute referencing can be used to keep the same cell-reference ($O$1), but then the cell reference doesn't change when the statement is true either.
My data looks like this: enter image description here
My complete formula looks like this: enter image description here
=IF(P2=INDIRECT(CHAR(COLUMN()+51)&O1+1);IF(INDIRECT(CHAR(COLUMN()+51)&O2+1)="";INDIRECT(CHAR(COLUMN()+53)&O1+1);INDIRECT(CHAR(COLUMN()+51)&O2+1));IF(INDIRECT(CHAR(COLUMN()+53)&O2+1)="";"";INDIRECT(CHAR(COLUMN()+51)&O2+1)))
What I want to do is to fill a column with values of the first column in the data until the cells are empty. Then I want to fill the column with data from the i+2th column (so from column C I go to column E). In order for this to happen, I want the first cell of (column E in this case) to stay the same, until column C is empty and the column starts taking values from column E.
I hope that this description gives a clear view of what I want to do.
Thanks in advance.
What I want to do is to fill a column with values of the first column in the data until the cells are empty. Then I want to fill the column with data from the i+2th column (so from column C I go to column E). In order for this to happen, I want the first cell of (column E in this case) to stay the same, until column C is empty and the column starts taking values from column E.
It's not the same kind of solution, but this might suit your needs better than your original formula:
=
IFERROR(INDEX(OFFSET($A$1,2,0,COUNTA(A:A)-1,1),ROW($A1)+0),
IFERROR(INDEX(OFFSET($B$1,2,0,COUNTA(B:B)-1,1),ROW($A1)+1-COUNTA(A:A)),
""))
If you need it for more than 2 columns, just extend the formula by following this pattern:
=
IFERROR(INDEX(OFFSET($A$1,2,0,COUNTA(A:A)-1,1),ROW($A1)+0),
IFERROR(INDEX(OFFSET($B$1,2,0,COUNTA(B:B)-1,1),ROW($A1)+1-COUNTA(A:A)),
IFERROR(INDEX(OFFSET($C$1,2,0,COUNTA(C:C)-1,1),ROW($A1)+2-COUNTA(A:A)-COUNTA(B:B)),
IFERROR(INDEX(OFFSET($D$1,2,0,COUNTA(D:D)-1,1),ROW($A1)+3-COUNTA(A:A)-COUNTA(B:B)-COUNTA(C:C)),
""))))
Sample implementation: https://i.stack.imgur.com/MAtxW.png
I've made considerations for your extra blank row between the header and the first row of data. For anyone wanting to use this formula without the blank row in their data set simply change the Offset-Row parameter from 2 to 1:
=
IFERROR(INDEX(OFFSET($A$1,1,0,COUNTA(A:A)-1,1),ROW($A1)+0),
IFERROR(INDEX(OFFSET($B$1,1,0,COUNTA(B:B)-1,1),ROW($A1)+1-COUNTA(A:A)),
""))
You can stick the formula anywhere in your worksheet, but don't forget to change the column letters to suit the location of your fields. In your case, probably:
=
IFERROR(INDEX(OFFSET($C$1,2,0,COUNTA(C:C)-1,1),ROW($C1)+0),
IFERROR(INDEX(OFFSET($E$1,2,0,COUNTA(E:E)-1,1),ROW($C1)+1-COUNTA(C:C)),
""))
Be aware that you need to make sure your columns don't contain rows with blank cells in between names, as this will cause it to skip an equal number of names at the bottom of the column.
EDIT:
I just realized your system uses semi-colons ";" to parse Excel formulas (mine uses commas ","). Please take note of that when copying these formulas to your spreadsheets. Here's the formula again but using ";"...
=
IFERROR(INDEX(OFFSET($C$1;2;0;COUNTA(C:C)-1;1);ROW($C1)+0);
IFERROR(INDEX(OFFSET($E$1;2;0;COUNTA(E:E)-1;1);ROW($C1)+1-COUNTA(C:C));
""))

VBA Loop through rows with multiple ranges and conditions

Disclaimer: New to VBA
I have an Excel sheet where I would like to build a VBA that does the following things.
My requirement is to fill a value in row Z based on a formula determined by the text in row M.
The formula deals with summing values from row A, B, C in different combinations (determined by text in row M).
Loop this for each row starting from row 5 to row 10000.
Eg:
If cell M5 = Apple, then Z5 = A5+B5;
If cell M9 = Samsung, then Z9 = C9+A9
I know a nested IF formula does it easily but there are just too many conditions and I'm looking for an automatic and cleaner route.
Thanks!
Not a VBA, but I think this will solve the issue:
The solution requires two parts:
a) A table (or named range) to hold the string value to match, and two columns to be added. This table (BrandEq - my example) is essentially a map to indicate which cells are to be summed. Where the column (A,B,C,...) is followed by a '#" for the row to be added in later.
b) A custom formula (detailed below) in each cell (z, in your case) to return the sum of the appropriate columns.
[Please see the attached image]
Personally, I prefer tables, but either that or a named range is recommend because additional "brands" and combinations (Cell1,Cell2) (my example) can be added easily and the formula automatically updates, so no going back to the base formula.
The the "z cell" formula is:
=INDIRECT(SUBSTITUTE(VLOOKUP(Sheet1!$D15,BrandEq,2,FALSE),"#",TEXT(ROW(),"#####"))) +INDIRECT(SUBSTITUTE(VLOOKUP(Sheet1!$D15,BrandEq,3,FALSE),"#",TEXT(ROW(),"#####")))
This may be easier to understand working from the 'inside-out':
VLOOKUP (from the 'DATA' table/range- current row brand column,into the 'BRANDEQ' table/range, return the contents of the corresponding column - note 2 on the first line and 3 on the second line, FALSE=exact matches on the lookup value)
with this value (eg. a#) ->
SUBSTITUTE( the hashtag '#' place holder, with the current row() as a text() value)
Indirect( return the corresponding value of that cell -- from the data table, the formula is entered in 'zval' row 14 (my example) (eg. 'a#' -> a11 = 100)
This formula is called a second time, but this time returning the 'Cell2 value, all else is the same function. (eg. 'b#' ->b11 = 15)
The results are added together.
Row 11, for example, parses as follows:
INDIRECT(SUBSTITUTE(VLOOKUP(Sheet1!$D11,BrandEq,2,FALSE),"#",TEXT(ROW(),"#####")))
+
INDIRECT(SUBSTITUTE(VLOOKUP(Sheet1!$D11,BrandEq,3,FALSE),"#",TEXT(ROW(),"#####")))
INDIRECT(SUBSTITUTE(VLOOKUP('Apple',BrandEq,2,FALSE),"#",TEXT(14,"#####")))
+
INDIRECT(SUBSTITUTE(VLOOKUP('Apple',BrandEq,3,FALSE),"#",TEXT(14,"#####")))
INDIRECT(SUBSTITUTE("a#","#","11")) + INDIRECT(SUBSTITUTE("b#","#","11"))
INDIRECT("a11") + INDIRECT("b11")
100 + 15
115
If additional/different cells need to be reference they only need be included in the 'BrandEQ' Table. Note: the cells be referenced do not need to be contiguous - they can be spread throughout the row.

Value between or search in range and return value in excel

Hi All,
I have a table for employee scores levels (Table D1 to F6), there is points for each level, for example score (4.60) will be in the 3rd level which has (3) points.
I want to write a formula in the column (B) to check in the table an return the point value from the column F, You can see the examples in D2 & D3.
Regards
Adel
What you need is one of the most awesome functions ever invented... VLOOKUP
=VLOOKUP(A2,$D$2:$F$6,3, TRUE)
(Paste in B2 and drag down)
Quick explanation of the arguments.
A2 is the value we're going to be looking up in the table
$D$2:$F$6 is the "table" we're going to be looking up. It's going to search the first column of it (You could have anything you wanted in Column D, VLOOKUP Only looks at the first column).
3 is the column number of our table that contains the answers we want to be looking up, and returning (in this case, F).
TRUE means we want to search ranges, not just exact values. If it was FALSE then we'd only get the numbers we wanted if we entered the precise scores.
Of course, using this method, there isn't any upper bounds to it, a value of say 20 would give us the last row (i.e. 5 points), however, you could fix this easily using an IF statement.
=IF(A2 > $E$6, "N/A", VLOOKUP(A2,$D$2:$F$6,3, TRUE))
This could of course be done a bit neater if you believed you were going to be adding more rows to the table later, but it works for now.
Use the Below formula in cell B2:
=VLOOKUP(A2,$D$2:$F$6,3, TRUE)

Resources