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.
4 2688
Add a module to your excel file, and copy paste this code: - Public Sub doLookup()
-
-
Dim ms As Sheet1 'ms=MySheet
-
Set ms = Sheet1
-
-
Dim mls As Sheet2 'mls=MyLookupSheet
-
Set mls = Sheet2
-
-
Dim intI As Integer
-
intI = 2
-
-
Dim intJ As Integer
-
-
Dim myCol As New Collection
-
-
-
-
Dim intComp As Integer
-
Dim intNum As Integer
-
-
Do While ms.Range("C" & intI) & "" <> ""
-
intComp = CInt(ms.Range("C" & intI))
-
intNum = CInt(ms.Range("E" & intI))
-
-
'Look for match
-
intJ = 2
-
Do While mls.Range("D" & intJ) & "" <> ""
-
If mls.Range("D" & intJ) = intComp Then
-
'Found a match on first column
-
-
'Does second column also match?
-
If mls.Range("F" & intJ) = intNum Then
-
'Found a match
-
ms.Range("I" & intI) = mls.Range("A" & intJ)
-
'Add to collection of found items
-
myCol.Add (CVar(mls.Range("A" & intJ)))
-
Exit Do
-
-
Else
-
'No match, keep looking
-
End If
-
-
End If
-
-
intJ = intJ + 1
-
Loop
-
-
-
Debug.Print "Running"
-
-
-
intI = intI + 1
-
Loop
-
-
'Now we have found all the matches, now to look for those that did not match.
-
intJ = 2
-
-
Dim strLook As String
-
Dim myVar As Variant
-
Dim bFound As Boolean
-
-
'Move intI 1 step back for further processing.
-
intI = intI - 1
-
-
Do While mls.Range("A" & intJ) & "" <> ""
-
'check for it in the collection.
-
'If its in the collection, then proceed to next item, if not, then add it to collection, and to sheet1
-
bFound = False
-
For Each myVar In myCol
-
Debug.Print myVar & "=" & CVar(mls.Range("A" & intJ))
-
If myVar = CVar(mls.Range("A" & intJ)) Then
-
'Found it.
-
bFound = True
-
Exit For
-
End If
-
Next
-
-
If Not bFound Then
-
'Add it
-
intI = intI + 1
-
ms.Range("I" & intI) = mls.Range("A" & intJ)
-
End If
-
-
intJ = intJ + 1
-
Loop
-
-
-
-
Set myCol = Nothing
-
End Sub
-
-
-
-
Might not be the "best" solution, its just something I threw together.
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)
Sign in to post your reply or Sign up for a free account.
Similar topics
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....
|
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
...
|
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?
|
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...
|
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...
|
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....
|
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...
|
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):...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
| |