I think if you take out the "Exit For" on line 48, that should do it (the one that says "assuming only 1 match is possible") ... That was only in there to make it more efficient. Let me know if you run into a problem
So - this:
Option Explicit
Option Base 1
Sub MatchAndPaste()
Dim strPartsSheet As String, strDetailSheet As String, strSummarySheet As String
Dim intFirstDataRow As Integer, intLastDataRow1 As Integer, intLastDataRow2 As Integer
Dim sht As Worksheet
Dim strPart As String, strDetail As String, strOrder As String
Dim intCount As Integer
Dim booMatchFound As Boolean
Dim i As Integer, j As Integer
'setup/intialise:
strPartsSheet = "Sheet1"
strDetailSheet = "Sheet2"
strSummarySheet = "Sheet3"
intFirstDataRow = 2
intLastDataRow1 = Worksheets(strPartsSheet).Cells(intFirstDataRow, 1).CurrentRegion.Rows.Count
intLastDataRow2 = Worksheets(strDetailSheet).Cells(intFirstDataRow, 1).CurrentRegion.Rows.Count
For Each sht In Worksheets
If sht.Name = strPartsSheet Or sht.Name = strDetailSheet Then
Worksheets(sht.Name).Select 'selects sheet1
Worksheets(sht.Name).Range("A1").Sort _
key1:=Worksheets(sht.Name).Columns("A"), order1:=xlAscending, _
key2:=Worksheets(sht.Name).Columns("B"), order2:=xlAscending, _
header:=xlYes
End If 'sorts pages to assending based on order.
Next sht
intCount = 0
Worksheets(strSummarySheet).Cells.ClearContents 'Clears Page three
For i = intFirstDataRow To intLastDataRow1 'Loop till end of sheet
strOrder = Worksheets(1).Cells(i, 1).Text
strPart = Worksheets(1).Cells(i, 3).Text
strDetail = "NO MATCH"
For j = 1 To intLastDataRow2
If Worksheets(1).Range("A" & i) = Worksheets(2).Range("A" & j) Then
If Worksheets(1).Range("B" & i) = Worksheets(2).Range("B" & j) Then
strDetail = Worksheets(2).Cells(j, 3).Text
intCount = intCount + 1
Worksheets(strSummarySheet).Range("A" & intFirstDataRow + intCount - 1).Value = strOrder
Worksheets(strSummarySheet).Range("B" & intFirstDataRow + intCount - 1).Value = strPart
Worksheets(strSummarySheet).Range("C" & intFirstDataRow + intCount - 1).Value = strDetail
booMatchFound = True
'Exit For 'assuming only 1 match is possible.
End If
End If
Next j
'if you get to here, you've cycled through the current record in sheet1 and attempted to
'match it against all the records in sheet2 and found no match; in this case, need to
'copy the Order and Part Number to the Summary sheet:
If booMatchFound = False Then
intCount = intCount + 1
Worksheets(strSummarySheet).Range("A" & intFirstDataRow + intCount - 1).Value = strOrder
Worksheets(strSummarySheet).Range("B" & intFirstDataRow + intCount - 1).Value = strPart
Worksheets(strSummarySheet).Range("C" & intFirstDataRow + intCount - 1).Value = strDetail
Else
booMatchFound = False
End If
Next i
With Worksheets(strSummarySheet)
.Columns("A:C").NumberFormat = "@"
.Columns("A:C").HorizontalAlignment = xlRight
.Cells(1, 1).Value = "Order Number"
.Cells(1, 2).Value = "Part Number"
.Cells(1, 3).Value = "Detail"
.Range("A1").Sort _
key1:=Worksheets(3).Columns("A"), order1:=xlAscending, _
key2:=Worksheets(3).Columns("B"), order2:=xlAscending, _
header:=xlYes
End With
End Sub
Bookmarks