473,394 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Matching data in an Excel file

Hello everyone
I ran into a problem with Excel that i hope you can help me

The thing i have to do is as following:

In a spreadsheet (attached file), in Sheet 1, i have a column represents the Name, and a column represents a Company Code,
and another column represent the Number Code
In sheet 2, i have a column represents the Name Tag, a column represents a Company Code, and another column represent the Number Code

and i want to match the data in sheet 2 into sheet 1 in the following way:
if the company code AND the number code of a row in sheet 2 matches the company code and the number code of a row in sheet 1,
then i want to create a new column in sheet 1 , contains the Name Tag taken from Sheet 2 correspond to the company and number code

those that are in sheet 2 and not in sheet 1 will be filled in at LAST (i.e, there will be blank cells to the left of the column we created in sheet 1)
Those that are in sheet 1 but not in sheet 2 will be left blank

The attachment i have is just a small sample
i hope you can help me come up with a VBA code for doing this
or if there is another easier way to do this, please help me

thank you very much
appreciated it.
Attached Files
File Type: zip matching name.zip (4.1 KB, 102 views)
Feb 18 '10 #1
4 2688
TheSmileyCoder
2,322 Expert Mod 2GB
Add a module to your excel file, and copy paste this code:
Expand|Select|Wrap|Line Numbers
  1. Public Sub doLookup()
  2.  
  3.     Dim ms As Sheet1 'ms=MySheet
  4.     Set ms = Sheet1
  5.  
  6.     Dim mls As Sheet2 'mls=MyLookupSheet
  7.     Set mls = Sheet2
  8.  
  9.     Dim intI As Integer
  10.     intI = 2
  11.  
  12.     Dim intJ As Integer
  13.  
  14.     Dim myCol As New Collection
  15.  
  16.  
  17.  
  18.     Dim intComp As Integer
  19.     Dim intNum As Integer
  20.  
  21.     Do While ms.Range("C" & intI) & "" <> ""
  22.         intComp = CInt(ms.Range("C" & intI))
  23.         intNum = CInt(ms.Range("E" & intI))
  24.  
  25.         'Look for match
  26.         intJ = 2
  27.         Do While mls.Range("D" & intJ) & "" <> ""
  28.             If mls.Range("D" & intJ) = intComp Then
  29.                 'Found a match on first column
  30.  
  31.                 'Does second column also match?
  32.                     If mls.Range("F" & intJ) = intNum Then
  33.                         'Found a match
  34.                         ms.Range("I" & intI) = mls.Range("A" & intJ)
  35.                         'Add to collection of found items
  36.                         myCol.Add (CVar(mls.Range("A" & intJ)))
  37.                         Exit Do
  38.  
  39.                     Else
  40.                         'No match, keep looking
  41.                     End If
  42.  
  43.             End If
  44.  
  45.             intJ = intJ + 1
  46.         Loop
  47.  
  48.  
  49.         Debug.Print "Running"
  50.  
  51.  
  52.         intI = intI + 1
  53.     Loop
  54.  
  55.     'Now we have found all the matches, now to look for those that did not match.
  56.     intJ = 2
  57.  
  58.     Dim strLook As String
  59.     Dim myVar As Variant
  60.     Dim bFound As Boolean
  61.  
  62.     'Move intI 1 step back for further processing.
  63.     intI = intI - 1
  64.  
  65.     Do While mls.Range("A" & intJ) & "" <> ""
  66.         'check for it in the collection.
  67.         'If its in the collection, then proceed to next item, if not, then add it to collection, and to sheet1
  68.         bFound = False
  69.         For Each myVar In myCol
  70.             Debug.Print myVar & "=" & CVar(mls.Range("A" & intJ))
  71.             If myVar = CVar(mls.Range("A" & intJ)) Then
  72.                 'Found it.
  73.                 bFound = True
  74.                 Exit For
  75.             End If
  76.         Next
  77.  
  78.         If Not bFound Then
  79.             'Add it
  80.             intI = intI + 1
  81.             ms.Range("I" & intI) = mls.Range("A" & intJ)
  82.         End If
  83.  
  84.         intJ = intJ + 1
  85.     Loop
  86.  
  87.  
  88.  
  89.     Set myCol = Nothing
  90. End Sub
  91.  
  92.  
  93.  
  94.  
Feb 18 '10 #2
TheSmileyCoder
2,322 Expert Mod 2GB
Might not be the "best" solution, its just something I threw together.
Feb 18 '10 #3
nico5038
3,080 Expert 2GB
My "Access" solution would be to link sheet1 and sheet2 to Access tables and execute an UPDATE query to update sheet1.
Check my sample database. Just place it in the folder that holds your excel workbook and relink the tables.
I've added a "Test" column in front of the "Here should the results come" to show that "Query1" after running produces the correct result.

Nic;o)
Attached Files
File Type: zip Sample.zip (7.3 KB, 104 views)
Feb 18 '10 #4
NeoPa
32,556 Expert Mod 16PB
Unless the data is very large I'd use VLookup() (Using VLookUp in Excel to Link to Excel 'Tables').

This can be done entirely in the spreadsheet, without recourse to VBA code.
Feb 19 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Ksenia Marasanova | last post by:
Hi, I have a little problem with encoding. Was hoping maybe anyone can help me to solve it. There is some amount of data in a database (PG) that must be inserted into Excel sheet and emailed....
4
by: RK | last post by:
Hi, In my application, I need to copy data from an Excel file into a SQL table. The article related to this can be found at http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B306572 ...
3
by: Chris | last post by:
Could someone please provide me an effective means of exporting data from a data set (or data grid) to Excel?
2
by: Niyazi | last post by:
Hi, I have to retrieve a data from AS400 DB2 and after working with data I have to export into one of existing Excel file. I can connect into specific library in AS400 DB2 using AS400...
3
by: Wade G. Pemberton | last post by:
Help!: I use javascript to format repetitive data from a web page input FORM into a long string , and save it as lines of comma delimited data in a text file on a Unix server. The text...
3
by: Mads Petersen | last post by:
I'm stuck in this code. Hope you can and will help me. I launch it from excel. I have made the following code work, but not as i whant. I need the ranges to be working with something like xlDown....
0
by: MKH | last post by:
Greetings, OS = Windows 2000 Server SP4 SQL = MS SQL 2000 Server SP4 Language = T-SQL I have an excel file stored in a data base table as an image data type. I would like to somehow...
8
by: rssd | last post by:
can somebody help me. I'm trying to read some excel files but i'm always getting this error No type library matching "Microsoft Excel" found at D:\Genes_datasets\exp.pl line 4 Win32::OLE(0.16):...
3
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all, I have a question for you. I have a .csv file which has many lines of data. Each line has many data fields which are delimited by ",". Now I need to extract part of data from this...
0
by: grego9 | last post by:
I have a bit of visual basic code in an excel spreadsheet that I need some help with. I am attempting to search a file called TO Cancellations2.xls for counterparty names and for each counterparty...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.