Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
fisherman
 
Posts: n/a
Default Match two tables using unique ID number

I have two tables using item number as the identifer for each row. It is
possible that their are items in either tables that do not match the other
table. I need to match the two tables so that each unique item is in the same
row and unique items in each table are on a seperate row.
  #2   Report Post  
Dave Peterson
 
Posts: n/a
Default

So you only care about the key identifier columns???

If yes, create a new worksheet.
Copy the key column from the first worksheet into column A.
copy the key column from the second worksheet into column B.

Add headers to row 1 (if you don't have them already).

Then run this macro:

Option Explicit
Sub testme()

Application.ScreenUpdating = False

Dim wks As Worksheet
Dim ColA As Range
Dim ColB As Range
Dim iRow As Long
Dim myCols As Long

Set wks = Worksheets("sheet1")
wks.DisplayPageBreaks = False
With wks
'row 1 has headers!
Set ColA = .Range("a2", .Cells(.Rows.Count, "A").End(xlUp))
Set ColB = .Range("b2", .Cells(.Rows.Count, "B").End(xlUp))

With ColA
.Sort key1:=.Cells(1), order1:=xlAscending, header:=xlNo
End With

'change the mycols to the number of columns that
'are associated with column B

myCols = 1 ' columns B only
With ColB.Resize(, myCols)
.Sort key1:=.Cells(1), order1:=xlAscending, header:=xlNo
End With

iRow = 2
Do
If Application.CountA(.Cells(iRow, "A").Resize(1, 2)) = 0 Then
Exit Do
End If

If .Cells(iRow, "A").Value = .Cells(iRow, "B").Value _
Or Application.CountA(.Cells(iRow, "A").Resize(1, 2)) = 1 Then
'do nothing
Else
If .Cells(iRow, "A").Value .Cells(iRow, "B").Value Then
.Cells(iRow, "A").Insert shift:=xlDown
Else
.Cells(iRow, "B").Resize(1, myCols).Insert shift:=xlDown
End If
End If
iRow = iRow + 1
Loop
End With

Application.ScreenUpdating = True

End Sub


If you're new to macros, you may want to read David McRitchie's intro at:
http://www.mvps.org/dmcritchie/excel/getstarted.htm


fisherman wrote:

I have two tables using item number as the identifer for each row. It is
possible that their are items in either tables that do not match the other
table. I need to match the two tables so that each unique item is in the same
row and unique items in each table are on a seperate row.


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Seed numbers for random number generation, uniform distribution darebo Excel Discussion (Misc queries) 3 April 21st 23 09:02 PM
Finding unique items in data field for pivot tables [email protected] Excel Discussion (Misc queries) 2 July 15th 05 06:15 PM
How to get number of rows which match criteria kiranmani Excel Worksheet Functions 6 July 11th 05 03:41 PM
Count Number of Characters in a cell? AHJuncti Excel Discussion (Misc queries) 2 June 16th 05 07:39 PM
Count number of Unique values Alan Excel Worksheet Functions 4 January 6th 05 08:05 PM


All times are GMT +1. The time now is 04:27 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"