jufglanville
New Member
- Joined
- Sep 11, 2017
- Messages
- 23
Hi all,
I'm looking to write a macro that looks a two sheets (Sheet1 & Sheet2). These sheets each contain a table of data. I'm hoping to run code that looks down column U in Sheet1 and see if that value exists in column U in Sheet2. If it does I want to copy the corresponding row from Sheet1 and place it at the bottom of Sheet2. Sometimes there are multiple matches of the same value found in Sheet1 and in that instance I would want to copy all rows containing the match and place at the bottom of Sheet2.
I stumbled across this code which I think could be used as a base. It searches my rows and places the value found in column U of Sheet1 next to the matching data in column AL Sheet2. (I want to place the entire row not just the value in column U, and I want to place it at the bottom of the table not next to it). This code also places "No Match" if no match is found. This isn't required in my code.
I'm looking to write a macro that looks a two sheets (Sheet1 & Sheet2). These sheets each contain a table of data. I'm hoping to run code that looks down column U in Sheet1 and see if that value exists in column U in Sheet2. If it does I want to copy the corresponding row from Sheet1 and place it at the bottom of Sheet2. Sometimes there are multiple matches of the same value found in Sheet1 and in that instance I would want to copy all rows containing the match and place at the bottom of Sheet2.
I stumbled across this code which I think could be used as a base. It searches my rows and places the value found in column U of Sheet1 next to the matching data in column AL Sheet2. (I want to place the entire row not just the value in column U, and I want to place it at the bottom of the table not next to it). This code also places "No Match" if no match is found. This isn't required in my code.
Code:
Sub NoteMatch()
Dim lastRow1 As Long
Dim lastRow2 As Long
Dim tempVal As String
lastRow1 = Sheets("sheet1").Range("U" & Rows.Count).End(xlUp).Row: x = 1
lastRow2 = Sheets("sheet1").Range("U" & Rows.Count).End(xlUp).Row: x = 1
For sRow = 2 To lastRow1
tempVal = Sheets("Sheet1").Cells(sRow, "U").Text
For tRow = 2 To lastRow2
If Sheets("Sheet2").Cells(tRow, "U") = tempVal Then
Sheets("Sheet2").Cells(tRow, "AL") = Sheets("Sheet1").Cells(sRow, "U")
End If
Next tRow
Next sRow
Dim match As Boolean
'now if no match was found, then put NO MATCH in cell
For lRow = 2 To lastRow2
match = False
tempVal = Sheets("Sheet2").Cells(lRow, "U").Text
For sRow = 2 To lastRow1
If Sheets("Sheet1").Cells(sRow, "U") = tempVal Then
match = True
End If
Next sRow
If match = False Then
Sheets("Sheet2").Cells(lRow, "BE") = "NO MATCH"
End If
Next lRow
End Sub