VBA to compare values in two columns i.e (Id Number and Dates) on separate sheets for unique values then extract row content to third sheet

Argh_Work

New Member
Joined
Oct 17, 2023
Messages
11
Office Version
  1. 2013
Platform
  1. Windows
Hi All,

I had some amazing help from @Flashbond on a query earlier this week in comparing values between worksheets and extracting rows to a third. I wasn't sure of the etiquette of building onto an initial query so have created a new post. The results from the first query have opened my thinking to what might be possible. And I was wondering if the below would be possible?

In the initial query I needed any unique numbers from the "Compare" worksheet Column A that didn't appear in the "Master" worksheet Column A to extract the row across to the "Result" worksheet. This worked amazingly (thanks again @Flashbond ).

Would it be possible for a query to check against two columns. For example in the screenshot below 897661827 appears in both the "Master" and "Compare" Worksheet. But in the "Compare" worksheet there is a third date.
So I wouldn't need Row 3 897661827 01/07/2023 and Row 4 897661827 05/07/2023 extracted but I would need the Row 5 897661827 17/07/2023 extracted? I'd also still need any unique numbers from the "Compare" worksheet column A to be extracted to the results worksheet.

I really appreciate the help of everyone. I'm not at the stage where I can contribute solutions but hopefully with the learning, I'm doing it won't be too long.

Thanks again everyone.
 

Attachments

  • master.PNG
    master.PNG
    178.7 KB · Views: 36
  • Compare.PNG
    Compare.PNG
    140.5 KB · Views: 31
  • Result.PNG
    Result.PNG
    18.1 KB · Views: 33

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hi,

Something like this should work fine if I understood correctly.
VBA Code:
Sub test()
  Dim compareRange As Variant, masterRange As Variant, lRow As Long, i As Long, j As Long
  Dim tempRange As Variant
  Dim dict As Object
  Set dict = CreateObject("Scripting.Dictionary")
 
  With Worksheets("Master")
  masterRange = .Range("A1:C" & .Cells(Rows.Count, 1).End(xlUp).Row)
  End With
  For i = 1 To UBound(masterRange, 1)
    If Not dict.Exists(masterRange(i, 1)&masterRange(i, 2)&masterRange(i, 3)) Then
      dict.Add masterRange(i, 1)&masterRange(i, 2)&masterRange(i, 3), 1
    End If
  Next
 
  compareRange = Worksheets("Compare").UsedRange
  With Worksheets("Result")
  ReDim tempRange(1 To UBound(compareRange, 2), 1 To 1)
  For i = 1 To UBound(compareRange, 1)
    If Not dict.Exists(compareRange(i, 1)&compareRange(i, 2)&compareRange(i, 3)) Then
      For j = 1 To UBound(compareRange, 2)
        tempRange(j, UBound(tempRange, 2)) = compareRange(i, j)
      Next
      ReDim Preserve tempRange(1 To UBound(tempRange, 1), 1 To UBound(tempRange, 2) + 1)
    End If
  Next
  ReDim Preserve tempRange(1 To UBound(tempRange, 1), 1 To UBound(tempRange, 2) - 1)
  .Range("A3").Resize(UBound(tempRange, 2), UBound(tempRange, 1)).Value = Application.Transpose(tempRange)
  End With
End Sub
 
Upvote 1
Solution

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top