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

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

Related

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

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

Copy entire row from a sheet if value found in another sheet to third sheet

Trying to solve a problem with VBA/macro or Excel function, but haven't had any luck.
I have 3 sheets:
Source
Detail Listing
Final
In my Source sheet I have a list of unique numbers
A
-------
111
222
333
444
In my Detail Listing I have data like so:
A B C D
-----------------------------
333 dog fly 9
777 cat wasp 2
111 bird ladybug 8
On the Final sheet I want all the rows from the Details Listing sheet that match values found in the Source Sheet, like so:
A B C D
---------------------------------
333 dog fly 9
111 bird ladybug 8
I have tried a Vlookup in worksheet but that only gave me a once cell result. I need the entire row if a match is found.
instead of trying to find the values in source over at the detail listting, i would suggest to try doing it reverse, run a for loop over the detail_listing, and for each value in the range A:A, vlookup it in your source, if you get a match, copy the current row in the for loop, otherwise just continue to next cell.
To solve my problem I went about it another way like Avishay Cohen suggested. I added an additional column in my Detail Listing worksheet with this formula for the cells:
=IF(ISNUMBER(MATCH(A2,Source!A:A,0)),"MATCH", "NOT FOUND")
This would list "Match" if there was a match between Column A of my Detail Listing worksheet and my Source worksheet. Then I just filtered out the "NOT FOUND" and copied my visible cells to another worksheet.
ISNUMBER: Checks whether or not a value is a number - returns TRUE or FALSE
MATCH: Returns the relative position of an item in an array that matches a specified value.
I'm sure there are many ways to solve this problem but this one worked for my situation.

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".

LibreOffice populating cells

I have a spreadsheet containing data identifying the depot where a product is stored, the supplier of the product and the manufacturer of said product.
I wish to break this one spreadsheet into four .csv files and import csv files into an already created PostgreSQL db. Typically, depot.csv, product.csv, supplier.csv and manufacturer.csv.
depot.csv has <15 entries, supplier.csv & manufacturer.csv <350 entries each and product.csv < 2,000 entries.
Example of what I want to do.
I have created a list of unique depots in a worksheet called depot.
id name
paris
berlin
london
original spreadsheet data
id Depot depot_id PRODUCT NAME product_id SUPPLIED BY Manufacturer
1 Paris 1 Hand wipes Erenco Chem Group
6 London 3 Scrub Towels Chemicraft Chem Group
7 Berlin 2 WR2 Grease Greasy Bin Chem Group
the column depot_id is populated by using the following formula
=IF(B2=depot.$B$2,depot.$A$2, IF(B2=depot.$B$3,depot.$A$3, IF(B2=depot.$B$4,depot.$A$4, 9999)))
and a worksheet called depot
Now I need a formula to populate product_id but above formula is not sufficient.
Thanks.
Tommy.
This problem is probably best solved by using the vlookup function.
Okay it's working.
Formula looks like this... in cell C2 enter the following
=VLOOKUP(D2,Product.$A$2:$B4586,2,0)
explanation:
D2 = the text being searched for
Product.$A$2:$B$4586 = the search array, in another worksheet, being searched
2, = the value to be posted in cell C2. This is from the column B in the search array. The leftmost column should be the column containing the value searched for.
0 = signifies a unsorted/unordered list.
See http://forum.openoffice.org/en/forum/viewtopic.php?t=46746 for better explanation.
Also http://wiki.openoffice.org/wiki/Documentation/How_Tos/Calc:_VLOOKUP_function
Thanks to Mauritz for reinforcing my research.
Tommy.

copy only those rows whose string len is > 6 in b column cell values

is it possible to copy only those rows which has string length more than 6 characters and paste them to a new sheet? The task is I have to remove all the unwanted characters in the rows. the range is 2 to 4000 something like that
I was using looping and mid function to do that which i found on google because I found only looping solution to that on google, because I only need numbers in each cell of B column and remove all other character except &,/, ,,. Then after cleaning up all ce
lls in B column I need to check the number of digits in each row of B column and if it contains more than 6 it should be copied to new sheet and it may contain blank cells too in between
a b c
6451 1234567 somevalue
4563 12345 somevalue
3245 123456789 somevalue
2345 1234 somevalue
Now I have to copy the 1 and 3 rd row but not the other rows as length of string is less than 7. I have to check only the B column and copy the entire row if it has more than 6 digits
I think what your boss is trying to say (not very well, btw) about using variant arrays, etc. instead of worksheets is to keep it in VBA. Read in the data once and manipulate it within VBA instead of pasting and manipulating data on a worksheet to get the desired result. This of course extends to anything outside of making calls to Excel, not just variant arrays. Perhaps you are often looping through cells one by one and he is trying to say he'd prefer if you dumped a range into a variant array and worked on it inside of VBA instead.
Of course, the best method depends on the task on hand, in my opinion.
As for your row question, it's a little unclear what you're trying to do (try to be careful how you use the words 'row' 'cell' and 'column'). A row cannot have a 'string length' so I am assuming you mean cell. Please confirm.