Copy Duplicate stock name from two sheets into new sheet

Johsmite

New Member
Joined
May 31, 2022
Messages
4
Office Version
  1. 365
Platform
  1. Windows
I am using the below code to find duplicate stock name from two sheets and copy duplicate stock into another sheet. But getting error when I change the column "B" as "D"
as my stock is in column "D". Experts, can you all please help me to solve this issue as I am very beginner to vba code. Below is the Excel on which I am working.

excelcopy.PNG




Sub CopyDuplicates2sheets()
MsgBox "Process begin now. if you cannot see any result after processing, " & _
"it means there is no duplicate data between two sheets."

Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet
Dim lr1 As Long, lr2 As Long, r As Long, r3 As Long
Dim ar As Variant, i As Long

Set ws1 = Sheets("BATSUS")
Set ws2 = Sheets("RECTUS")
Set ws3 = Sheets("WList")
ws3.Cells.Clear

lr1 = ws1.UsedRange.Rows.Count
lr2 = ws2.UsedRange.Rows.Count
ws1.UsedRange.Interior.ColorIndex = xlNone
ws2.UsedRange.Interior.ColorIndex = xlNone

' build dictionary from sheet2 col B
Dim dict, key As String
Set dict = CreateObject("Scripting.Dictionary")

For r = 1 To lr2
key = Trim(ws2.Cells(r, "B"))
If Len(key) > 0 Then
If dict.exists(key) Then
dict(key) = dict(key) & ";" & r
Else
dict.Add key, r
End If
End If
Next

Application.ScreenUpdating = False
r3 = 1 ' sheet3
' scan sheet 1 looking for to match with sheet 2
For r = 1 To lr1
key = Trim(ws1.Cells(r, "B"))
If dict.exists(key) Then
' copy multiple matches
ar = Split(dict(key), ";")
For i = LBound(ar) To UBound(ar)
ws1.Range("A" & r).Resize(1, 16).Copy ws3.Range("A" & r3) ' A:F
ws2.Range("A" & ar(i)).Resize(1, 15).Copy ws3.Range("T" & r3) ' A:Q
r3 = r3 + 1
Next
End If
Next

Worksheets("WList").Activate
With ActiveSheet
.AutoFilterMode = False
.Range("B2").AutoFilter
.Range("B2").AutoFilter Field:=1, Criteria1:="<0"
.AutoFilter.Range.Offset(1).EntireRow.Delete
.AutoFilterMode = False
End With
Application.ScreenUpdating = True
MsgBox "Process finished"
End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
You are, the code will need to account for for the fact it's a data type.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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