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

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.

Related

Excel: Turn duplicates in a link into blank cells

I have a list of names that includes duplicates (all in Column A of my worksheet). What I am trying to do is convert the duplicates into blank cells. I do need to keep the values in the rows where column A ends up with blanks. In the example below, I demonstrate what I currently have in my list and the second table demonstrates what I need the result to look like. The names Mike, Bill and Jim are the duplicates that are converted to blanks, but next to those blanks I still have the values I need (Xs in columns 1 and 2). The reason I want to get blanks is because I will filter those blanks out and remove them from a master table I am working.
I used the available tools in excel to identify duplicates (conditional formatting), and then "remove duplicates" but when I do that Mike, Bill, and Jim are deleted and all of my data shifts around and it doesn't work for what I am trying to do.
I am wondering if there is a formula that I can possibly use? or perhaps a macro/vba? Any help would be greatly appreciated! Thank you.
name
column_1
column_2
bill
jim
mike
sandra
mike
x
bill
x
x
dave
x
x
jim
x
name
column_1
column_2
bill
jim
mike
sandra
x
x
x
dave
x
x
x
I have your data starting in Column A1 with a Header (called Name in A1). Then I use the formula- in Column B2 =COUNTIF($A$2:A2,A2) * kindly note, that the 1st part of the range is 'Anchored' with the absolute reference aka the '$' sign. this will start at the top of the range and when copied down will include the 1st cell (with Bill in it).
so in column B, it looks to Col A for how many times that name occurs. I would filter on Column B and show all cells NOT equal to 1 (uncheck the filter box for '1') and remove the rows and you will be left with the 1st occurance of the name.
when you delete, delete entire rows in filtered data (this will remove the spaces
Create a helper column and put the following formula into all of its cells:
=COUNTIF(A$2:A2;A2)-1
All entries with a duplicate (in a row above it) will contain a value > 0

Search for duplicate text string in two columns and highlight, excel

I'm looking for a way to search and highlight duplicate text strings in two different columns in Excel; this means that the cell content doesn't have to be identical, instead of that is what I need is that if the content of column A is somehow contained in any cell of column B, both cells get highlighted.
For example, let's say that I have two columns, one named "Patient" and another one called "Couples". So, what I would need is to make a comparison between both columns, and if one of the patient's names is within a couple, both cells get highlighted:
Column A. Patient name | Column B. Couple name
John Smith | Adriana Lewis - Mark Rutte
Peter Brown | Giaccomo Down - Rosy Lawn
Jerry Goldsmith | Bob Loewe - Gigi Pink
Ewan Thompson | Sonia Farrel - John Smith
In this example, the content of A2 ("John Smith") is also contained in B5 ("Sonia Farrel - John Smith"), so that I would need that both A2 and B5 get highlighted. Also, both columns don`t have the same range, one is shorter than the other, since there are more names than couples; and it can happen that two names in different cell are contained in a single couple, so that all three cells should get highlighted.
I have tried everything, with no success... please help!
Multiple ways to do this but here's one option with conditional formatting.
Rule applied to data in column A, using COUNTIF and wildcards.
=COUNTIF($B$2:$B$5,"*"&A2&"*")>0
Rule applied to data in column B, using ISNUMBER, SEARCH and SUMPRODUCT.
=SUMPRODUCT(--ISNUMBER(SEARCH($A$2:$A$5,B2)))>0

Conditional subtraction from multiple cells

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

Pick Out Email From a Row

I have over 100k rows similar to:
Joe | 123 | email#domain.com
Bob | 456
Ben | 567 | Denver | email#email.com
The emails could be at any point/cell in that row and some cells have # in them.
How could I get the following output:
Joe | email#domain.com
Bob |
Ben | email#email.com
or maybe just tag the email address onto the end of the row and then hide the other columns?
I've tried various bits and pieces but am getting nowhere fast.
Now that I know you can have it in every column, Add a column left of column A. In your example you then have B1="Joe", etc.
Then put this formula in A1:
=IFERROR(OFFSET(A1,0,SUM(IFERROR(IF(FIND("#",$B1:$O1)>0,1,0),0)*COLUMN($B1:$O1))-1),"")
Adjust the range $B1:$O1 to match your needs. I suggest you make it as tight as possible because array formulas are resource-intensive.
========================
If the email addresses were always in the last column of a given row, and if there weren't any blanks in the row until the last value, you could just do that:
First, Add a column left of column A. In your example you then have B1="Joe", etc.
Then, put this formula in cell A1
=OFFSET(A1,0,COUNTA($B1:$XFD1))
and drag and drop it on all your rows. (I'm using Excel 2010, hence XFD in the above formula. Adjust as you see fit, just make sure you use a range that covers the maximum number of columns for your dataset)
A bit of fun really.
Embolden the names and for the rest Replace All # without formatting with # and Font style Bold. Copy into Word, Select All, Find what: * with Font Not Bold, Use wildcards and replace with nothing. Copy back into Excel and Go To Special, Blanks. Right click on one of selection and Deleteā€¦, Shift cells left.

Match name and copy row from sheet 1 to sheet 2 on corresponding column

I asked a very similar question to this one here, but I am trying to identify if a cell's numerical value is contained in a list of cells on a different sheet. If the cell from sheet A matches a cell in sheet B, mark a different column in B with a corresponding row in sheet A, otherwise leave it blank. An example is below:
Sheet A
Column A | Column B
-------------------
1 John
2 Sue
4 Bob
I would like the corresponding Sheet B to populate Column B like this:
Sheet B
Column A | Column B
-------------------
2 Sue
3
4 Bob
=IF(ISNUMBER(MATCH(I2, 'SALT, WOD, Champion Members'!A:A, 0)), "Y", "N")
I have been using the above answer to populate a different column in the same workbook, and I'm thinking I can maybe use this formula, but instead of "Y" or "N", somehow preserve the row.
You need to use VLOOKUP as already mentioned. But you will need to use another function to check for existence of the value, else you will get #N/A against ID 3
I used COUNTIF
=IF(COUNTIF($A$2:$A$4,E2)=0,"",VLOOKUP(E2,$A$2:$B$4,2,FALSE))
Use the VLOOKUP function:
=VLOOKUP(A1;Sheet1!A:B;2;FALSE)
Where A1 is the value you want to look up, Sheet1!A:B is the original sheet with the data.

Resources