Add offset to my macro

emukiss10

Board Regular
Joined
Nov 17, 2017
Messages
201
Hello Guys!

I need little help with my script. It compares A2:A from two Sheets and if match it is copying cell F of one shhet to cell K of the other.
It works fine But...

I want to compare not only A2:A but A2:A and Offset(,3) to Offset(,4)

something like
Code:
Kl.Value & Kl.Offset(,3) 
 to 
K2.Value & K2.Offset(,4)


My working Macro
Code:
    Dim Kl As Range, K2 As Range
    Dim ValK As Variant, ValK2 As Variant
    Dim dict As Scripting.Dictionary, dict2 As Scripting.Dictionary
    
    Set dict = New Scripting.Dictionary
    dict.CompareMode = vbTextCompare
    Set dict2 = New Scripting.Dictionary
    dict2.CompareMode = vbTextCompare
 
    With dict
        For Each Kl In Sheets(1).Range("A2", Sheets(1).Range("A" & Rows.count).End(xlUp))
            If Not .Exists(Kl.Value) Then .Add Kl.Value, Kl.row
        Next Kl
    End With
    With dict2
        For Each K2 In Sheets(2).Range("A2", Sheets(2).Range("A" & Rows.count).End(xlUp))
            If Not .Exists(K2.Value) Then .Add K2.Value, K2.row
        Next K2
    End With
    For Each ValK In dict.keys
        For Each ValK2 In dict2.keys
            If ValK Like ValK2 Then
                With Sheets(1)
                    .Cells(dict(ValK), "K").Value = Sheets(2).Range("F" & dict2(ValK2))
                End With
            End If
        Next ValK2
    Next ValK



Or mayby it can be done better (faster - I have over 1M rows n one sheet and aboput 50K i second)

Best Regards!
W.
 
Last edited:

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
How about
Code:
Sub Comparedata()

   Dim Cl As Range
   Dim v1 As String
   Dim Ws1 As Worksheet
   Dim Ws2 As Worksheet
   
   Set Ws1 = Sheets(1)
   Set Ws2 = Sheets(2)
   With CreateObject("Scripting.dictionary")
      For Each Cl In Ws1.Range("A2", Ws1.Range("A" & Rows.Count).End(xlUp))
         v1 = Cl.Value & Cl.Offset(, 3).Value
         If Not .exists(v1) Then .Add v1, Cl.Offset(, 10).Value
      Next Cl
      For Each Cl In Ws2.Range("A2", Ws2.Range("A" & Rows.Count).End(xlUp))
         v1 = Cl.Value & Cl.Offset(, 4).Value
         If .exists(v1) Then Cl.Offset(, 5).Value = .Item(v1)
      Next Cl
   End With
End Sub
 
Upvote 0
Hmm... I might explained it wrong..

ex.

IF Sheets(2).Range("A500") is found in Sheets(1).Range("A245") And Sheets(2).Range("D500") is match with Sheets(1).Range("E245")
Than
copy Sheets(2).Range("F500") to Sheets(1).Range("K245")

Im sorry Fluff for my engilsh. Explaining is sometimes tough.
 
Last edited:
Upvote 0
My fault, I misread your code. Try
Code:
Sub Comparedata()

   Dim Cl As Range
   Dim v1 As String
   Dim Ws1 As Worksheet
   Dim Ws2 As Worksheet
   
   Set Ws1 = Sheets(1)
   Set Ws2 = Sheets(2)
   With CreateObject("Scripting.dictionary")
      For Each Cl In Ws2.Range("A2", Ws2.Range("A" & Rows.Count).End(xlUp))
         v1 = Cl.Value & Cl.Offset(, 3).Value
         If Not .exists(v1) Then .Add v1, Cl.Offset(, 5).Value
      Next Cl
      For Each Cl In Ws1.Range("A2", Ws1.Range("A" & Rows.Count).End(xlUp))
         v1 = Cl.Value & Cl.Offset(, 4).Value
         If .exists(v1) Then Cl.Offset(, 10).Value = .Item(v1)
      Next Cl
   End With
End Sub
 
Upvote 0
Perfect.

Just for kicks - can it copy lets say more than one value? Like from any two or more columns? F and G for exampe?
 
Upvote 0
Yup
Code:
Sub Comparedata()

   Dim Cl As Range
   Dim v1 As String
   Dim Ws1 As Worksheet
   Dim Ws2 As Worksheet
   
   Set Ws1 = Sheets(1)
   Set Ws2 = Sheets(2)
   With CreateObject("Scripting.dictionary")
      For Each Cl In Ws2.Range("A2", Ws2.Range("A" & Rows.Count).End(xlUp))
         v1 = Cl.Value & Cl.Offset(, 3).Value
         If Not .exists(v1) Then .Add v1, Cl.Offset(, 5).Resize(, 2).Value
      Next Cl
      For Each Cl In Ws1.Range("A2", Ws1.Range("A" & Rows.Count).End(xlUp))
         v1 = Cl.Value & Cl.Offset(, 4).Value
         If .exists(v1) Then Cl.Offset(, 10).Resize(, 2).Value = .Item(v1)
      Next Cl
   End With
End Sub
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,098
Messages
6,170,099
Members
452,301
Latest member
QualityAssurance

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