Conditional subtraction from multiple cells - excel

A |B |C |....|K |L |M |
Tom |0 | |....|Tom |Jim |Dave |
Jim |1000 | |....|15000|14000|12000|
Dave |3000 | |....| | | |
Using Google Sheets for this one. I would like the values in columns K, L, and M to read from column B, detect if the corresponding cell from A reads one of 'Tom', 'Jim', or 'Dave' for example, and then subtract the amount from the correct column to reduce a running total. I've had some trouble figuring it out and tried to use conditional formatting to solve it but can't seem to quite get there. Is there a formula I can use that will read column B and subtract the amount shown from the correct column based on the name in column A?
So to pseudo-code it:
read(column B cell);
if(column B cell - 1 column = "Tom")
{
column K - (value of column B cell)
}
else if(column B cell - 1 column = "Jim")
{
column L - (value of column B cell)
}
etc.
Is there a simple method I can use to generate this result? Also thought about changing the formatting of a cell based on the name in the cell next to it and subtracting the value of any cell with that colour but this becomes unwieldy if names are added. Any assistance would be greatly appreciated!

would there be a way to constrain the formula to a single cell so I can have the total in a single location rather than down the columns? My plan is to have the total boxes scroll with the sheet and always be visible.
Let's assume that Columns A and B continue on down the sheet, with further name + amount entries. You want to have a single row of balances for each of the people.
The balance is then some initial value less the sum of amounts for that person. Say the amounts you've shown in row 2 are the initial values; here's how you could have row 3 reflect the remaining balance for each person (This is for "Tom", copy for the others):
=K2-sumif($A$2:$B,"="&K$1,$B$2:$B)
Alternative solution
This doesn't do exactly what you want, but it is more appropriate for a running total scenario, so others may find it useful to adapt to ledgers, etc.
The IF() function can be used to decide whether or not a value in Column B applies to one of the "total" columns, K to M.
Use this formula in K3, copy to the rest of the range:
=K2-if($A2=K$1,$B2,0)
This example is in Google Sheets, but the same formula works in Excel and other "compatible" offerings.

What you do is put the formula in each column.
Column K subtracts value of column B if column A = "Tom"
Column L subtracts value of column B if column A = "Jim"
etc

Related

Excel - return value based on two inputs

I have two input values, that I want to use to return a third value.
Input 1: Y (lets say "Y" is in cell B1)
Input 2: 15 (15 in cell B2)
In another database sheet the input 1 values are sorted in the top row, the input 2 values are listed in a column in front of the wanted values.
| | X | Y | Z |
|16| a | g | k |
|15| b | h | l |
|14| c | i | l |
Fx. X,Y,Z are in row 3 and column 2,3,4.
I want a formula that returns "h" from the two inputs, Y & 15. How is this possible?
In your example above:
=INDIRECT(B1&B2) will return the value in [Y15] which is "h"
Assuming 16 is in Row4, please try:
=INDIRECT(CHAR(CODE(B1)-22)&6-MOD(B$2,14))
Here's a formula for you:
=INDEX(MatrixRange,MATCH(RowInput,MatrixStartColumn,0),MATCH(ColumnInput,MatrixStartRow,0))
MatrixRange = whatever the range of your data matrix is (in entirety) so if your data matrix starts in A3 and ends in D20 this would be replaced with A3:D20
RowInput = whatever cell you are getting search value for to find the row of appropriate data (This is Input 2 on your example)
MatrixStartColumn = Whatever column (or range) your Matrix data index starts in (these would be the numbers in your example). If the numbers on the left side of your data example are in Column A, this would be changed to A:A or A1:A50 (or wherever the last value is). The important thing for this is to use a range from the start of the column otherwise your row count will be off. If you must use a sub-range to avoid matches outside of the matrix, be sure to add the appropriate number to the end of the Match statement. For example if you are specifying MatrixStartColumn as "A3:A44", you will need to add +2 for the first 2 rows being skipped (A1 and A2). So the Index statement becomes (MatrixRange,MATCH(RowInput,MatrixStartColumn,0)+2,...
ColumnInput = whatever cell your column search value is in (your Input 1 data)
MatrixStartRow = Same as StartColumn above but for the header index of your matrix (the XYZ letters in your example). Just as above, if you must use only the range of the matrix, be sure to add your offset numbers so you get the right column.
Assuming that your inputs are at B1 (row), and B2 (Column) here is your formula:
=INDEX(B4:D6,MATCH(B1,A4:A6,0),MATCH(B2,B3:D3,0))
Here is how the formulas work:
INDEX(area with values, row , column) returns value based on row and column you provide.
MATCH(value to find, range to search) returns row/column where value was found.
Note that Match will accept only one row or column for 'range to search'.

Compare entire rows in two sheets

I have two sheets with same data and I want to compare entire row in two sheets.
On Sheet1 (old data)
Col A | Col B
1001 | My Val 1
2001 | My Val 2
3001 | My Val 3
On Sheet2 (new data)
Col A | Col B | C
3001 | My Val 3 |True
1001 | My New Val 1 |False
2001 | My New Val 2 |False
Instead of trying to think in terms of comparing rows, make the problem simpler. It is easier to compare just a single cell - so first combine your "whole row" into a single cell. This is easy by concatenating all the cells using the & symbol.
Insert a new (hidden) column C on both sheets, that combines the other columns with a formula like:
= A1 & B1
Now you have a summary that is easy to compare, because you are just looking at single cells and a single column.
On your new sheet, insert a new column D that uses VLOOKUP to see if the row exists on sheet 1:
=VLOOKUP( C1, Sheet1!C:C, 1, false)
Now this will give you an error if the row is not found, and will return the row if it is found.
Your new column E (which corresponds to your old column C) can be calculated with:
=NOT( ISERR( D1 ))
Hide unused columns as required.
I started down the path of the accepted answer, concatenating columns. But with 107 columns, this proved to be tedious.
My solution for this was to paste both sheets into one, keeping the columns aligned.
Then, on the Data tab, click Remove Duplicates. If your remaining row count equals your starting row count, they're identical. If they're not, you'll need to filter columns to find the ones that are not the same.

Search for specific number in column and fill cells from its adjacent cells

I have a list of employee ID's for each period of the year. Each ID has Specific stats for that time period. The problem I'm running into is the list order changes every period according to the employees tasks.
Cell 1A contains the Employees ID#(ID's are Numbers only if that makes a difference). Column B has the whole list of employee ID#. I need to figure out how to write a formula or vba that checks column B for the ID# that matches 1A and populate 1C-1J with the adjacent data found in column B (columns C-J).
I hope I explained myself correctly without being to confusing. I appreciate any help.
The vlookup formula looks at the first column of $B:$J (column B) and searches for $A$1 and if it finds it returns the value in the 2.(3.,4.,...,9.) column of $B:$J
A1 | B1 | =VLOOKUP($A$1,$B:$J,2,FALSE) | =VLOOKUP($A$1,$B:$J,3,FALSE) | =VLOOKUP($A$1,$B:$J,4,FALSE) | ... | =VLOOKUP($A$1,$B:$J,9,FALSE)

Add cell string to another cell if 2 cells are the same for 2 rows

I'm trying to make a macro that will go through a spreadsheet, and based on the first and last name being the same for 2 rows, add the contents of an ethnicity column to the first row.
eg.
FirstN|LastN |Ethnicity |ID |
Sally |Smith |Caucasian |55555 |
Sally |Smith |Native American | |
Sally |Smith |Black/African American | |
(after the macro runs)
Sally |Smith |Caucasian/Native American/Black/African American|55555 |
Any suggestions on how to do this? I read several different methods for VBA but have gotten confused as to what way would work to create this macro.
EDIT
There may be more than 2 rows that need to be combined, and the lower row(s) need to be deleted or removed some how.
If you can use a formula, then you can do those:
Couple of assumptions I'm making:
Sally is in cell A2 (there are headers in row 1).
No person has more than 2 ethnicities.
Now, for the steps:
Put a filter and sort by name and surname. This provides for any person having their names separated. (i.e. if there is a 'Sally Smith' at the top, there are no more 'Sally Smith' somewhere down in the sheet after different people).
In column D, put the formula =if(and(A2=A3,B2=B3),C2&"/"&C3,"")
Extend the filter to column D and filter out all the blanks.
That is does is it sees whether the names cells A2 and A3 are equal (names are the same), and whether the cells B2 and B3 are equal (surnames are the same).
If both are true, it's the same person, so we concatenate (using & is another way to concatenate besides using concatenate()) the two ethnicities.
Otherwise, if either the name, or username, or both are different, leave as blank.
To delete the redundant rows altogether, copy/paste values on column D, filter on the blank cells in column D and delete. Sort afterwards.
EDIT: As per edit of question:
The new steps:
Put a filter and sort by name and surname. (already explained above)
In column E, put the formula =IF(AND(A1=A2,B1=B2),E1&"/"&C2,C2) (I changed the formula to adapt to the new method)
In column F, put the formula =if(and(A1=A2,B1=B2),F1+1,1)
In column G, put the formula =if(F3<F2,1,0)
In column H, put the formula =if(and(D2="",A1=A2,B1=B2),H1,D2) (this takes the ID wherever it goes).
Put the formulae as from row 2. What step 3 does is putting an incremental number for the people with same name.
What step 4 does is checking for when the column F goes back to 1. This will identify your 'final rows to be kept'.
Here's my output from those formulae:
The green rows are what you keep (notice that there is 1 in column G that allows you to quickly spot them), and the columns A, B, C, E and H are the columns you keep in the final sheet. Don't forget to copy/paste values once you are done with the formulae and before deleting rows!
If first Sally is in A1 then =IF(AND(A1=A2,B1=B2),C1&"/"&C2,"")copied down as appropriate might suit. Assumes where not the same a blank ("") is preferred to repetition of the C value.

Formula to detect duplicate data in Excel (with a difficult variable!)

I need to sanitise a mailing list, and am wondering if anyone knows of a formula to find out the following:
I have an excel spreadsheet with two columns A and B, the formula I need to do is: if [value in column B is duplicated] has [blank in column A] AND [data in column A] then show the value of Column B in a new Column C.
Example of data:
COL A | COL B
11/11/12 | **name#example.com**
03/09/11 | different#example.com
| **name#example.com**
06/01/12 | anothername#example.com
The formula would detect name#example.com is duplicated & it has a date in column A & further on has no value in column A, and therefore displays: name#example.com in Column C.
Hope that makes sense!
This should work (obviously changing the ranges to cover all rows):
=IF(AND(COUNTIF($B$2:$B$5,B2)>1,A2=""),B2,"")

Resources