VB: Comparing two cells in separate sheets and copy adjacent cells to a new sheet

PolAmer

New Member
Joined
Feb 24, 2021
Messages
3
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi,
I'm trying to compare CustomerID #s in two separate worksheets (WS2 and WS1) and when there is a match copy the customerID # and next 50 adjacent cells from WS1 to the new sheet.
I am having issues coping the data to a new worksheet (Ws3). Any help would be greatly appreciated.

Sub Customers()

Dim Cl As Range
Dim Ws1 As Worksheet, Ws2 As Worksheet, Ws3 As Worksheet

Set Ws1 = Sheets("Sheet1")
Set Ws2 = Sheets("Sheet2")
Set Ws3 = Sheets("Sheet3")

With CreateObject("scripting.dictionary")

For Each Cl In Ws2.Range("A2", Ws2.Range("A" & Rows.Count).End(xlUp))
.Item(Cl.Value) = Cl.Offset(, 1).Resize(, 50)
Next Cl
For Each Cl In Ws1.Range("A2", Ws1.Range("A" & Rows.Count).End(xlUp))
Cl.Offset(, 10).Resize(, 50).Value = .Item(Cl.Value)

Next Cl

End With
End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Try changing your output line to this:-
VBA Code:
            Ws3.Range(Cl.Address).Offset(, 10).Resize(, 50).Value = .Item(Cl.Value)
 
Upvote 0
Or maybe
VBA Code:
Sub Customers()

   Dim Cl As Range
   Dim Ws1 As Worksheet, Ws2 As Worksheet, Ws3 As Worksheet
   
   Set Ws1 = Sheets("Sheet1")
   Set Ws2 = Sheets("Sheet2")
   Set Ws3 = Sheets("Sheet3")
   
   With CreateObject("scripting.dictionary")
   
      For Each Cl In Ws2.Range("A2", Ws2.Range("A" & Rows.Count).End(xlUp))
         .Item(Cl.Value) = Cl.Resize(, 51)
      Next Cl
      For Each Cl In Ws1.Range("A2", Ws1.Range("A" & Rows.Count).End(xlUp))
         If .Exists(Cl.Value) Then
            Ws3.Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(, 51).Value = .Item(Cl.Value)
         End If
      Next Cl
   
   End With
End Sub
 
Upvote 0
Solution
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,185
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