Thanks Joel,
One question though, do I use this instead of, or in conjunction with the
MergeDistinct Code?
This is the code I have adapted to merge the two lists into one (I probably
should have placed this in the initial posting, sorry!).
My modifications included changing the ColumnToMatch, to ColumnToMatch1 &
ColumnToMatch2, as both columns are side by side (K and L) on the one
spreadsheet (as opposed to Column C on Sheets 1 & 2 as Mr. Pearson originally
designed the code to do).
Column E is the column on my spreadsheet I need the code to analyis before
it transfers the data from ColL, and then ColK to the (1column) list. NB: The
data in ColE is not transferred.
Sub MergeDistinct()
'MergeDistinct
'This procedure merges two lists into a separate list that contains no
duplicate values.
Dim R As Range 'Range loop variable
Dim LastCell As Range 'Last Cell in input columns
Dim WS As Worksheet 'Worksheet Reference
Dim N As Long 'Result of Duplicates test.
Dim M As Long 'Rows in merged list
Dim StartList1 As Range 'First Cell of first list to merge
Dim StartList2 As Range 'First Cell of second list to merge
Dim StartOutputList As Range 'First Cell of merged list
Dim ColumnToMatch1 As Variant 'Column in input lists to test for duplicates
Dim ColumnToMatch2 As Variant 'Column in input lists to test for duplicates
Dim ColumnsToCopy As Long 'Number of Columns in each input list to
copy to output.
'This is the column in the input lists that is to be tested for duplicates
ColumnToMatch1 = "L"
ColumnToMatch2 = "K"
'This is the number of columns from each list to be merged that are copied
to the result list.
ColumnsToCopy = 1
'The output list begins in this cell.
Set StartOutputList = Worksheets("Dwg_TakeOffs").Range("A2")
'The first list to be merged starts here
Set StartList1 = Worksheets("database").Range("L5")
Set WS = StartList1.Worksheet
With WS
M = 1
'get the last used cell in the first list to be merged
Set LastCell = .Cells(.Rows.Count, StartList1.Column).End(xlUp)
'loop though the range of values
For Each R In .Range(StartList1, LastCell)
If R.Value <> vbNullString Then
N = Application.CountIf(StartOutputList.Resize(M, 1), _
R.EntireRow.Cells(1, ColumnToMatch1).Text)
'if N = 0, then the item is not in the merged result
'list, so copy the data over. If N > 0, we've already
'encountered the value, so do nothing
If N = 0 Then
StartOutputList(M, 1).Resize(1, ColumnsToCopy).Value = _
R.Resize(1, ColumnsToCopy).Value
'M is the number of rows in the merged list. Increment it.
M = M + 1
End If
End If
Next R
End With
'The second list to be merged starts here.
Set StartList2 = Worksheets("Database").Range("K5")
Set WS = StartList2.Worksheet
With WS
Set LastCell = .Cells(.Rows.Count, StartList2.Column).End(xlUp)
For Each R In .Range(StartList2, LastCell)
If R.Value <> vbNullString Then
N = Application.CountIf(StartOutputList.Resize(M, 1), _
R.EntireRow.Cells(1, ColumnToMatch2).Text)
If N = 0 Then
StartOutputList(M, 1).Resize(1, ColumnsToCopy).Value = _
R.Resize(1, ColumnsToCopy).Value
M = M + 1
End If
End If
Next R
End With
End Sub