Compare sheets of two workbooks and import in a new workbook with duplicates ignored

DreyFox

Board Regular
Joined
Nov 25, 2020
Messages
61
Office Version
  1. 2016
Platform
  1. Windows
Hi there,

I have a weird request. I have a Destination File named Scheduler with the following columns A and B:
1635164686654.png

Then I have a Source File called MOSTATUS, with the following columns A and B:
1635164743329.png

Both MOSTATUS and Scheduler are different workbooks.

What I would like to achieve is, when the user hits the "Import data" button, the VBA script would compare Columns A and Columns B of the Destination and the Source File, delete the duplicate rows in the Source File, delete all the data in the Destination File, and import the remaining data in the Source File into the Destination File.

I found these links, but I'm not sure how any of them could be of use to me:

Any help would be greatly appreciated. Thank you for your assistance and time.
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
To clarify for example MO00003831-AL in the Destination File should be compared to possible matches of MO00003831-AL in the Source File. If found in the Source File for the same match, that entire row should be deleted.
 
Upvote 0
To help anyone who may need it in the future, this is what I ended up doing:

VBA Code:
Private Sub CommandButton1_Click()
  Dim sh1 As Worksheet, sh2 As Worksheet, dic As Object
  Dim r1 As Range, r2 As Range, a As Variant, b As Variant
  Dim i As Long, lr1 As Long, lr2 As Long
  Dim wb As Workbook
  Dim fPath
  
  fPath = Application.GetOpenFilename(FileFilter:="Excel Files (*.XLSX), *.XLSX", Title:="Select File To Be Opened")
  Set wb = Workbooks.Open(fPath)
  
  Set sh1 = Workbooks("p1.xlsm").Worksheets("Sheet1")
  Set sh2 = wb.Worksheets("Sheet1")
  Set dic = CreateObject("Scripting.Dictionary")
  
  dic.CompareMode = vbTextCompare
  lr1 = sh1.Range("A" & Rows.Count).End(xlUp).Row
  lr2 = sh2.Range("A" & Rows.Count).End(xlUp).Row
  a = sh1.Range("A1:B" & lr1).Value2
  b = sh2.Range("A1:B" & lr2).Value2
  Set r1 = sh1.Range("A" & lr1 + 1)
  Set r2 = sh2.Range("A" & lr2 + 1)
  
  For i = 1 To UBound(a, 1)
    dic(a(i, 1) & a(i, 2)) = i
  Next
  For i = 1 To UBound(b, 1)
    If dic.exists(b(i, 1) & b(i, 2)) Then
      Set r2 = Union(r2, sh2.Range("A" & i))
    End If
  Next
  r2.EntireRow.Delete
  
  
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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