View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
AA2e72E AA2e72E is offline
external usenet poster
 
Posts: 400
Default comparing distinct data

1. Open the workbook; if you have made any changes, save it.
2. My assumptions: your first sheet is called Sheet1, the second is called
Sheet2 (the one from which data is discarded).
3. Both sheets contain data from Row 1 down AND row 1 contains field names,
one of the common names is ID.

Now try this code:

Sub xx()
cnn = "Provider=MSDASQL;Driver={Microsoft Excel Driver (*.xls)};DBQ=" &
ThisWorkbook.FullName
Sql = "SELECT * FROM [SHEET2$] WHERE ID IN(SELECT ID FROM [SHEET1$])"
Set adors = CreateObject("ADODB.RecordSet")
adors.Open Sql, cnn
If Not adors.EOF Then
ActiveWorkbook.Worksheets.Add
ActiveSheet.Range("A2").CopyFromRecordset adors
End If
For i = 0 To adors.Fields.Count - 1
ActiveSheet.Cells(1, i + 1).Value = adors.Fields(i).Name
Next
adors.Close
Set adors=NOTHING
End Sub

Copy the code to the ThisWorkbook module & run it. IT will add a new sheet
into which it will add the data that you want to keep.
If this does what you want or it can be adapted to do so, you can delete
Sheet2 or make the CopyFromRecordSet method apply to Sheet2.


"junkit132000" wrote:

Hi,

I was wondering if there was somebody who could help me with what I hope is
a quick problem.

Basically, I have two worksheets.
In one worksheet, I have a column full of unique id numbers. And related to
that specific id number, I have rows of information with info like street
address and name.

In another worksheet, I also have a column full of unique id numbers. Some
of these id numbers match up with the id numbers that are in the 1st
worksheet. But there's many extraneous records that are irrelevant. In this
worksheet, I have rows of information with info on City and State.

So basically, I was wondering, is there any way I can match up the records
with id numbers that match up on worksheet 1 & 2 and discard the records on
worksheet 2 that don't have a corresponding record in worksheet 1? aka, I'd
like to match up the street address, name, city and state for onto one
worksheet. There's just too many records for me to do this by hand.

I'm sorry if this was confusing, I'd be more than happy to explain further.
Any and all help would be GREATLY appreciated. Thanks!

Brian