Excel vba code to copy names found in multiple worksheets and count how many times they appear - vba

I need some help writing some VBA code.
Basically I have one spreadsheet with 4 worksheets (CLIENT1, CLIENT2, CLIENT3, CLIENT4)
Each worksheet contains a column called CONTACT.
This CONTACT column contains a person name
The CONTACT column can be in a different position in each worksheet ie the CONTACT column in CLIENT1 is in Column D whereas it appears in column E in CLIENT2
I would like some help writing some code that will create a new Worksheet called SUMMARY.
The SUMMARY worksheet would contain in Column A a list of all the names found in the CONTACT column and in Column B it would count the number of times that persons name was found.
So the SUMMARY worksheet would end up looking something like this.
A B
1 John Smith 4
2 Brad Black 2
3 Gary Soth 1
4 Bob Brown 6
5 Sam Drow 2
Hope this makes sense and thanks for your help
Andy

Note : I am just playing with Excel and not using VBA but think you can achieve it without using VBA also.
You can use COUNTIF and solve your problem.
I will show you step by step. I cant attach excel here so adding images. If you wish to have excel which i prepared then just give me your email, I will send it.
This is summary which I got by using the formula (you can see that highlighted :
Client Name column can get value from the sheet where you can combine all your unique client names and use that as base.
Check Client1, 2, 3 & 4 sheets.
Hope this may help you.

Just found that in Excel you can use the Remove Duplicates function to remove all unwanted names. I can then use the count function across all 4 worksheets to get the required information.
Thanks
Andy

Related

Set the value from another sheet. With a test of condition. Excel

I have a table with two sheets. On the first (1) the name is written, on the second (2) Imin and numerical meaning are written, the same name can be written several times. There are also names that are not present on the sheet (1).
Sheet 1.
A
Column A
Column B
Vova--
Ben
Sheet 2.
Column A
Column B
Ben +
2
Timmy
1
Ben T
4
Vova
6
How to put in the first sheet the sum of the values from the sheet (2)
I tried:
=IF(ColA2 = $Sheet2 ColA2; $Sheet2 ColB2)
=VLOOKUP(A1;$Sheet2.B2:B4;2)
=SUMIF($'Sheet2'A1:A4;A1;$'Sheet2'ColB1:ColB4)
Not work
Does not summarize the value
Looking for accurate coincidences
Names can end in different ways ...
Ben = Ben T
Required:
Column A
Column B
Vova--
6
Ben
3
Need help please.
I found an answer. I need to use LEFTB
=SUMIF($'Sheet2'A1:A4;LEFTB(A1;4)&"*";$'Sheet2'ColB1:ColB4)
or
=SUMPRODUCT((LEFTB($Sheet2!$A$1:$A$4;3)=A2)*$Sheet2!B2:B5)

Use of Index and Match function with Count/Counta/Countif to count the results on a 3rd wookbok

I have great success in guidance from the experts on Stack. I need guidance once more. :)
I have workbook 1 that has a copy of names on column A and email addresses on column B. Workbook 2 that has copy of names on A and email addresses on column B and data that I need copied on Column c on workbook 2.
I have a workbook 3 in which I would like to use to build a table with matched and counted from the formula I need. Below I will show.
workbook 1 pic:
Workbook 2 pic:
Workbook 3 pic:
Ultimately, I wanted the index,matched and counted total to show up on the basic table shown on workbook 3. As you can see there are 4 email addresses that match between work book 1 and workbook 2, but there are also 4 email addresses that do not match. after the matching and unmatching is found, i want the formula to give me the count of the matched and unmatched from column c in workbook 2.
so if you just use your eyes and count, you will see that rows 2 through 4 these email addresses matched between work book 1 and work book 2. that would give me a total of 2 drinks expenses and 2 food expenses from the matching found, but also give me 2 tickets expenses and 2 parking expenses from the unmatched found.
For the Matched:
=SUMPRODUCT((COUNTIFS('Sheet1'!A:A,'Sheet2'!$A$2:$A$100,'Sheet1'!B:B,'Sheet2'!$B$2:$B$100)>0)*('Sheet2'!$C$2:$C$100=$B3))
For the unMatched
=SUMPRODUCT((COUNTIFS('Sheet1'!A:A,'Sheet2'!$A$2:$A$100,'Sheet1'!B:B,'Sheet2'!$B$2:$B$100)=0)*('Sheet2'!$C$2:$C$100=$B7))
These are untested as I did not want to retype that much data, but in thoery should work.

Copying Excel values based on another value

I have a Microsoft Excel spreadsheet with a list of zip-codes and I need values from another workbook to be placed next to the zip codes based on the matching zip-code for each value. I do not know how to write macros and was wondering if someone could post the basic code for me so that I could add the correct cells and values as needed into the code. (please let me know where to add it in).
Here is a sample of what I mean:
zip code column list
11004
11005
11101
11102
11103
11104
11105
11106
11109
11120
11351
11352
11354
11355
11356
11357
zip code and value from other workbook
11693 151
For example, if the zip-code 11693 exists in the list of zip codes, I would want the value 151 placed in the cell next to it.
As mentioned above, you can use VLOOKUP, try this..
=IF(ISNA(VLOOKUP(A1,Sheet2!$A$1:$B$5,1,FALSE)),"",VLOOKUP(A1,Sheet2!$A$1:$B$5,2,FALSE))
Use above formula on Sheet1 : Column B
Here:
Sheet1 contains the Zipcode List (result 3 digits gets added to this sheet on Column B)
Sheet2 contains the Zipcode with 3 digit code next to it.

How to copy to cells from a range if the columns to the right match?

I looked for a solution for this using VLOOKUP and programmatically and I couldn't find it. I hope you guys can help.
I have two spreadsheets with same headers and similar data. One is complete the other is not. The first column (lets call it "ID") of the completed spreadsheet messed up.
I want to copy the values from the "ID" column of the incomplete version to the new version based on if the cell to the right of each (lets call it "Names") matches.
To clarify, the algorithm or formula has to look through the column "Names" of the OLD (incomplete) version and if it finds a match in the NEW version, copy it to its left.
I cannot just sort alphabetically and copy and paste, because the completed worksheet has some duplicates that may be needed.
EDIT: EXAMPLE OF MY DATA:
Sheet1 Sheet 2
ID NAME ID Name Age
112 John 156 Dog 11
113 Bob 1xx Bob 15
156 Dog 1xx Bob 16
1xx John 18
Since the ID is messed up (because the ID I work with got messed up when exporting from Google Fusion Tables) I need to copy to the NEW file the "Ids" from the OLD version. This is just a simple example, I have over 200 000 rows of data.
Assuming ID is in A1 on both sheets and that the xx indicate IDs to be replaced, please add a new ColumnA in Sheet 2 and in A2 there:
=IF(ISNUMBER(B2),B2,INDEX(Sheet1!A:A,MATCH(C2,Sheet1!B:B,0)))
copied down to suit.
The first part ISNUMBER(B2) tests for a numeric ID in the data set that is a mixture of sound and corrupt. If that is a number and corrupt there may be no way to identify the corruption from the information provided.
So if that test is passed accept the value from the corrupted sheet (ie B2).
If however the test fails, then find the relevant Name's location (for Row2 the relevant name is Dog) in the incomplete sheet (ie Row4) and use INDEX to lookup the value associated with Dog (to its left) in the incomplete sheet.
Assuming the new spreadsheet is called "New" and the old is called "Old," and assuming that the names appear in column A of each spreadsheet, starting in row 1, then use this formula in column B of the new spreadsheet:
=iferror(vlookup(New!A1,Old!$A:$A,1,false),"?????")
So your spreadsheet looks like this:
A B
1 Coke =iferror(vlookup(New!A1,Old!$A:$A,1,false),"?????")
2 Pepsi =iferror(vlookup(New!A2,Old!$A:$A,1,false),"?????")
3 Sprite =iferror(vlookup(New!A3,Old!$A:$A,1,false),"?????")
4 asdfvasdl =iferror(vlookup(New!A4,Old!$A:$A,1,false),"?????")
5 Dr. Pepper =iferror(vlookup(New!A5,Old!$A:$A,1,false),"?????")
and it should display like this:
A B
1 Coke Coke
2 Pepsi Pepsi
3 Sprite Sprite
4 asdfvasdl ???????
5 Dr. Pepper Dr. Pepper

Excel Lookup Data Based on Column Name not Cell Name

I am trying to accomplish a strange task in excel and don't know how to go about it. I'm using Excel 2007 at work and I know very basic vba.
I want to automate a process where a person takes three spreadsheets and dumps certain data from them into one master sheet. The three spreadsheets vary every month in their column order, so unfortunately I can't just program vlookups to run and get the data.
I could be wrong, but it seems like Vlookup Match, Index Match Match, etc. wouldn't work either because they are still referencing cells. I basically need something that will find a column based on the text in the column, rather than its location, because the location will change, but the text will always be the same.
I have two ideas but I have no idea if they work and don't really know where to start on implementing them:
Convert the three spreadsheets to tables and reference the headings with table syntax (I haven't been able to get this to work)
Complex VBA that IDs everything
Can anyone help point me in the right direction to accomplish this task? Thanks so much for your help.
EDIT EXAMPLE
My main template that I'm trying to dump things into is just going to have the ID#s and empty columns:
ID# AltID# Deal
1
2
3
4
5
And then another spread sheet might have look like this
ID# AltID# Deal
1 10101 AAAA
2 20202 BBBB
3 30303 CCCC
4 40404 DDDD
5 50505 EEEE
I could of course vlookup, but the problem is next month, those columns in the second spreadsheet won't be in the same place, in the same order. They could be all the way on colum DD or whatever. So I need a formula that looks them up regardless of the columns location. Just matching them by the heading text. I hope that clarifies...
If you combine VLOOKUP with MATCH you can search based on column name, not index. Remember that the MATCH function returns the numeric index of the match. Let's assume that your data looks like the below:
NAME DAY1 DAY2 DAY3
Bob 123 345 567
Tim 456 789 998
A functioning Vlookup to return the DAY2 column for Tim would look like:
=VLOOKUP("Tim", A2:D3,MATCH("DAY2",A1:D1,0),FALSE)
A couple of notes. Where I've hard coded in words, you can use cell references.
Here is one idea:
get the index of the column ID# in Sheet2 MATCH("ID#",Sheet2!$1:$1,0)
convert to a character: CHAR(MATCH("ID#",Sheet2!$1:$1,0)+64)
get the column range:
INDIRECT(
CONCATENATE("Sheet2!",
CHAR(MATCH("ID#",Sheet2!$1:$1,0)+64),":",
CHAR(MATCH("ID#",Sheet2!$1:$1,0)+64)))
similarly for column AltID#:
INDIRECT(
CONCATENATE("Sheet2!",
CHAR(MATCH("AltID#",Sheet2!$1:$1,0)+64),":",
CHAR(MATCH("AltID#",Sheet2!$1:$1,0)+64)))
with range of ID# and AltID#, we can do the match+index:
=INDEX(
INDIRECT(
CONCATENATE("Sheet2!",
CHAR(MATCH("AltID#",Sheet2!$1:$1,0)+64),":",
CHAR(MATCH("AltID#",Sheet2!$1:$1,0)+64))),
MATCH(A2,INDIRECT(
CONCATENATE("Sheet2!",
CHAR(MATCH("ID#",Sheet2!$1:$1,0)+64),":",
CHAR(MATCH("ID#",Sheet2!$1:$1,0)+64))),0))
Here is an idea to consider:
Give a name to cell ID# e.g "dataJanuary" and use this named-cell as reference using an OFFSET function then you can explore it with vlookup or index-match function or other method.
Next month even if the ID# column is moved to other column or row, it will always reference to the same data since the cell is already named with "dataJanuary".