Macro to compare 2 columns and output list with singles and sheet reference
Solved/Closed
Justdance
-
Updated on Jun 1, 2021 at 12:12 PM
TrowaD Posts 2921 Registration date Sunday September 12, 2010 Status Moderator Last seen December 27, 2022 - Jun 1, 2021 at 12:14 PM
TrowaD Posts 2921 Registration date Sunday September 12, 2010 Status Moderator Last seen December 27, 2022 - Jun 1, 2021 at 12:14 PM
Related:
- Macro to compare 2 columns and output list with singles and sheet reference
- Tentacle locker 2 - Download - Adult games
- Fnia 2 - Download - Adult games
- Mobile number list with name - Guide
- Euro truck simulator 2 download free full version pc - Download - Simulation
- Feeding frenzy 2 download - Download - Arcade
1 response
TrowaD
Posts
2921
Registration date
Sunday September 12, 2010
Status
Moderator
Last seen
December 27, 2022
555
Updated on Jun 1, 2021 at 12:17 PM
Updated on Jun 1, 2021 at 12:17 PM
Hi JustDance,
Assuming that:
then the following code will do the task:
Best regards,
Trowa
Assuming that:
- The first column is column A on Sheet1
- The second column is column A on Sheet2
- These columns contain a header
- A sheet named Sheet3 exists
- You want the result on Sheet3 in column A for values and column B for sheet references
then the following code will do the task:
Sub RunMe() Dim mFind As Range Dim lRow1, lRow2 As Long lRow1 = Sheets("Sheet1").Range("A1").End(xlDown).Row lRow2 = Sheets("Sheet2").Range("A1").End(xlDown).Row For Each cell In Sheets("Sheet1").Range("A2:A" & lRow1) Set mFind = Sheets("Sheet2").Range("A2:A" & lRow2).Find(cell.Value) If Not mFind Is Nothing Then FirstAddress = mFind.Address Do Set mFind = Sheets("Sheet2").Range("A2:A" & lRow2).FindNext(mFind) Loop While mFind.Address <> FirstAddress Else Sheets("Sheet3").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Value = cell.Value Sheets("Sheet3").Range("A" & Rows.Count).End(xlUp).Offset(0, 1).Value = "Sheet1" End If Next cell For Each cell In Sheets("Sheet2").Range("A2:A" & lRow2) Set mFind = Sheets("Sheet1").Range("A2:A" & lRow1).Find(cell.Value) If Not mFind Is Nothing Then FirstAddress = mFind.Address Do Set mFind = Sheets("Sheet1").Range("A2:A" & lRow1).FindNext(mFind) Loop While mFind.Address <> FirstAddress Else Sheets("Sheet3").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Value = cell.Value Sheets("Sheet3").Range("A" & Rows.Count).End(xlUp).Offset(0, 1).Value = "Sheet2" End If Next cell End Sub
Best regards,
Trowa