How to copy data from one sheet to another based on matching criteria - vba

Source data:
Target:
I'm not sure if this is possible, but I need to copy data from a source file to a target file based on a column in the target file being an exact match to values in a column from the source file.
Based on the attached image, Coverage Pattern Name exists in both files. I need to copy the coverage term data over but the issue is there may be multiple coverage term values for the same type of coverage pattern name. I need to bring in all unique coverage terms.
Essentially, if coverage description from (Target file) exists as coverage pattern name (from source file), then I need to populate covtermpattern (in target file) from coverageterm (from source file).
Is this possible?

You are looking for the vlookup function.
If I get you right and assuming that:
Both sheets are in the same file;
Target is a sheet named "target", and Source is a sheet named "source";
That "Coverage description" column is column source!A, then
in the cells of "target!covtermpattern write something like:
=ifna(vlookup(A2,source!K:K,1,FALSE), "---")
Explained
= ifna() will return the value of the first instruction unless it is an error. I.e, there is no match on Source from Target. In that case, it will return the character "---". You can later filter them.
vlookup() is the function to import the values of a certain row, given a match of two values.
A2 points to the value we are looking for in vertically in a column (or subset) that we specify in the following instruction.
source!K:K represents the abovementioned search space: the whole column K from sheet "source".
1 represents how many spaces to the right of the match is the value we are looking for. 1 represents the very column specified (K). 2 would lead you to column N. 3 to M [...]
FALSE lets you retrieve approximate values. It only works on numerical matches, though.
Hope it helps!

Related

Data Mapping Between Excel Files and Third Part Applications

I'm working on a macro which named "MasterTool". All of macro code will be in this "MasterTool". This tool's main role is taking value of certain string name from excel sheet (input sheet) and write this values another excel sheet (output sheet) for same string name.
Additionally there's 2 different term I would like to describe you from now to understand easily.
Input Sheet
Excel file which stores some specific informations (E.g. Vehicle Height : 3m ) ( One of the cell has my string value: Vehicle Height and adjacent next cell has it's value 3m )
Output Sheet
This is also an excel sheet like report page which contains also specification information which comes from Input Sheet.
I wonder is it common usage data mapping methods in Excel VBA ?
My first attempt is:
Naming cells in Input Sheet like that: strCellVehicleHeight and I'm writing string name: Vehicle Height, also naming cell of this feature's value strCellVehicleHeightValue and I'm writing 3.
On output sheet I named cells same and then mapping via cell names. It's a bit hard way to use I think.
Isn't there more possible ways or other formats like XML JSON etc. I would be glad if you help me about this situation.

I would like to match the data of two columns in two different excel workbook

I have two excel WORKBOOK (WB)
In excel WB 1, I have column for student IDs and Advisor name.
In excel WB 2, I have IDs to be matched to WB 1 IDs if the Advisor name is "John" to show "TRUE' in a column in WB2.
Can you pls tell me what are the formulas to try AND EXPLAIN THE COMPONENTS OF THE FORMULA?
aTTACHED IS THE SCREENSHOT OF DATA.
pLEASE NOTE ITS IN DIFFERENT EXCEL WORKBOOKS NOT SHEETS.
Taking your provided formula I will run you through the minor change that will make this work:
=IF(ISNUMBER(MATCH(C7:C17&"John",'[wORKBOOK1 NAME]SHEET1'!$A$2:$A$13&'[wORKBOOK1 NAME]SHEET1'!$B$2:$B$13,0)),"True","False")
This of course is assuming that the Forenames are in $B$2:$B$13...
What this is doing is simply forming a string value of the ID with the Forname stuck onto the back of it. MATCH will reference an array of values that it creates from the criteria within itself - the ID with the Forename.
The match formula will work in the same way, returning an integer representing the index location within the array when the match is found.
I will point out that the formula does not accept an array for the first argument, so currently only the first cell is being searched... You will want to update the formula and perhaps have this as a column checking each individual ID:
=IF(ISNUMBER(MATCH(C7&"John",'[wORKBOOK1 NAME]SHEET1'!$A$2:$A$13&'[wORKBOOK1 NAME]SHEET1'!$B$2:$B$13,0)),"True","False")
If you wanted this as just one formula it gets a bit more complicated as you must form an array using an if statement and Evaluate against that array:
=IF(ISNUMBER(MATCH(1,IF(C7:C17&"John"='[wORKBOOK1 NAME]SHEET1'!$A$2:$A$13&'[wORKBOOK1 NAME]SHEET1'!$B$2:$B$13,1,0),0)),"True","False")
This must be entered as an array formula (While still in the formula bar hit Ctrl + Shift + Enter)
The if statement will check for the match and build a new binary array (1 for a match and 0 for no match), the MATCH formula then is simply checking if a match is found in that new array by searching for 1.

VBA Excel wildcard string search, copy and paste to new cell

I am looking for help in trying to do the following.
I have large amount of data in Excel (100K+ rows) which have many columns, in one of the columns (lets just say col A) there is a string of data in each cell that lists file path info indicating a java installation. I need to search within that string to find common characters that start and end the same but will have different data in-between, so wildcard would be needed to identify. After identifying the data in the string search for each cell in the specified column (col A) I need to copy and paste the data identified to a different column located just right of the data (col B) on same worksheet for each row. So would look something like this:
Example
COLUMN A (original data string)
C:\app\Java\jre7\bin\...
C:\Program Files (x86)\Java\jre6\bin\...
C:\app\JAVA\jdk1.7.0._21\jre\bin\...
C:\JAVA\JDK-1_5_0_16_i586\jre\bin\...
COLUMN B (copy & paste to here)
jre7
jre6
jdk1.7.0._21
JDK-1_5_0_16_i586
Would need wildcard search to pick up anything specific between the \ & \ that starts with letter j and would be followed by two letters which could be re or dk but would have to allow for character or number to follow this in any length. Would be something like \j??*#*\ for the search always starting with same first two characters, followed by two letters, then possibly another character or nothing at all before a version number, then can be no characters or many after the version number.
As you can see from examples I am trying to pick up the version info with version numbers in them and do not want to get dir info with jre or jdk only in them, since most of my data have these dir listed somewhere in the file path string.
Then copying this info and pasting into Col B as shown in example is what I am trying to do.
Any help would be greatly appreciated as this is a manual process that would benefit greatly from automating.
There's actually a really quick way to do this using formulas. You could combine all of these into one formula if you wish, but I spread it into four simple formulas with the fourth giving you the answer.
Assuming the first string is in cell A1:
B1 = =SEARCH("java",A1)
C1 = =FIND("\",A1,E1)
D1 = =FIND("\",A1,F1+1)
E1 = =MID(A1,F1+1,G1-F1-1)
E1 will have your answer. Autofill down the columns and then copy>paste values in column E and delete columns B-D.

Find out if a column contains a certain number

Right now I have a macro that loops through a worksheet that contains data about different machine parts performs various actions on another worksheet using that data. Now I want it to add each part number to a column in a third worksheet, but only if it doesn't already exist there. Here is my code for adding the part numbers:
Rows("1:1").Insert Shift:=xlDown
Range("A1").Value = cpn
I have the list spoted after each added part number. Is there any way better than a loop to find out if the part number already exists?
You could use the COUNTIF method. Just like this:
Application.CountIf(Range("A:A"), valueThatYouWantToSearchFor)
It will return the number of cells that contains this value.

Column references in formulas

I am a little stuck at the moment. I am working on an array of data and need to find a way to input column numbers into formulas.
-I have used the match function to find the corresponding column number for a value.
ex. "XYZ" matched with Column 3, which is equivalent to C1:Cxxxxxx
-now for inputing the C1:Cxxxxxx into a formula to get data for that particular column, I would like to be able to directly reference the Column 3 part, because I plan on using this workbook in the future and the column needed to run the calculation may or may not be column 3 the next time I use it.
- is there any way to tell excel to use a formula to tell excel which column to use for an equation?
so a little more detail, I have the equation
=AND(Sheet3!$C$1:$C$250000=$A$4,Sheet3!$B$1:$B$250000=$B$4)
instead of specifying to use column C, is there a way to use a formula to tell it to use C?
EDIT: more additional info;
"i am basically running the equivalent of a SQL where statement where foo and bar are true, I want excel to spit out a concatenated list of all baz values where foo and bar are true. ideally i would like it to ONLY return baz values that are true, then I will concat them together separately. the way I got it now, the expression will test every row separately to see if true; if there is 18K rows, there will be 18K separate tests.. it works, but it's not too clean. the goal is to have as much automated as possible. *i do not want to have to go in and change the column references every time I add a new data arra*y"
Thanks
You can use INDEX, e.g. if you have 26 possible columns from A to Z then this formula will give you your column C range (which you can use in another formula)
=INDEX(Sheet3!$A$1:$Z$250000,0,3)
The 0 indicates that you want the whole column, the 3 indicates which column. If you want the 3 can be generated by another formula like a MATCH function
Note: be careful with AND in
=AND(Sheet3!$C$1:$C$250000=$A$4,Sheet3!$B$1:$B$250000=$B$4)
AND only returns a single result not an array, if you want an array you might need to use * like this
=(Sheet3!$C$1:$C$250000=$A$4)*(Sheet3!$B$1:$B$250000=$B$4)
You could use ADDRESS to generate the text, you then need to use INDIRECT as you are passing a string rather than a range to the fomula
=AND(INDIRECT(ADDRESS(1,3,,,"Sheet3") & ":" & ADDRESS(250000,3))=$A$4
,INDIRECT(ADDRESS(1,2,,,"Sheet3") & ":" & ADDRESS(250000,2))=$B$4)
Obviously replace the 3s and 2s in the ADDRESS formulae with your MATCH function you used to get the column number. The above assumes the column for $B$1:$B$25000 is also found using `MATCH', otherwise it is just:
=AND(INDIRECT(ADDRESS(1,3,,,"Sheet3") & ":" & ADDRESS(250000,3))=$A$4
,Sheet3!$B$1:$B$25000=$B$4)
Note a couple of things:
You only need to use "Sheet3" on the first part of the INDRECT
Conditions 3 and 4 in the ADDRESS formula are left as default, this
means they return absolute ($C$1) reference and are A1 style as
opposed to R1C1
EDIT
Given the additional info maybe using an advanced filter would get you near to what you want. Good tutorial here. Set it up according to the tutorial to familiarise yourself with it and then you can use some basic code to set it up automatically when you drop in a new dataset:
Paste in the dataset and then use VBA to get the range the dataset uses then apply the filter with something like:
Range("A6:F480").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
Sheets("Sheet1").Range("A1:B3"), Unique:=False
You can also copy the results into a new table, though this has to be in the same sheet as the original data. My suggestion would be paste you data into hidden columns to the left and put space for your criteria in rows 1:5 of the visible columns and then have a button that gets the used range for your data, applies the filter and copies the data below the criteria:
Range("A6:F480").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Sheets _
Range("H1:M3"), CopyToRange:=Range("H6"), Unique:=False
Button would need to clear the destination cells first etc, make sure you have enough hidden columns etc but it's all possible. Hope this helps.