Refactor my code

emukiss10

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

Can You help me refactor this code to be more noobie frendly? Or it is good as it is? (code is not mine and I bearly understand it)
It looks for the same values in two sheets and if match it copy value from other column of found row to yet another column of searching row.


Code:
    Dim Kl As Range, K2 As Range
    Dim ValK As Variant, ValK2 As Variant
    Dim dict As Scripting.Dictionary, dict2 As Scripting.Dictionary 
    Dim ar_2 As Long, ar_1 As Long
    Dim new_DG As String, new_DI As String


    Set dict = New Scripting.Dictionary
    dict.CompareMode = vbTextCompare
    Set dict = CreateObject("Scripting.Dictionary")


    With dict


        For Each Kl In Sheets(7).Range("A2", Sheets(7).Range("A" & Rows.count).End(xlUp))
            ValK = Kl.Value
            ar_2 = Kl.row
            If Not .Exists(ValK) Then .Add ValK, ar_2
        Next Kl


    End With


    Set dict2 = New Scripting.Dictionary
    dict2.CompareMode = vbTextCompare
    Set dict2 = CreateObject("Scripting.Dictionary")


    With dict2


        For Each K2 In Sheets(4).Range("f2", Sheets(4).Range("F" & Rows.count).End(xlUp))
            ValK2 = K2.Value
            ar_1 = K2.row
            If Not .Exists(ValK2) Then .Add ValK2, ar_1
        Next K2


    End With


    For Each ValK In dict.keys
        For Each ValK2 In dict2.keys
            If ValK = ValK2 Then
                new_DG = Sheets(4).Range("DG" & dict2(ValK2))
                new_DI = Sheets(4).Range("DI" & dict2(ValK2))
                With Sheets(7)
                    .Cells(dict(ValK), "M").Value = new_DG
                    .Cells(dict(ValK), "N").Value = new_DI
                End With
            End If
        Next ValK2
    Next ValK

Thank You!
W.
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Hello,
If it works, as there are always many ways to do the same thing
Shortening the syntax can sometimes make it easier to decipher ,, to a point
maybe,,
Code:
Dim Kl As Range, K2 As Range
    Dim ValK As Variant, ValK2 As Variant
    Dim dict As Scripting.Dictionary, dict2 As Scripting.Dictionary
    'Early Binding Reference - Microsoft Scrpiting Runtime
    
    Set dict = New Scripting.Dictionary
    dict.CompareMode = vbTextCompare
    Set dict2 = New Scripting.Dictionary
    dict2.CompareMode = vbTextCompare

    With dict
        For Each Kl In Sheets(7).Range("A2", Sheets(7).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(4).Range("f2", Sheets(4).Range("F" & 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(7)
                    .Cells(dict(ValK), "M").Value = Sheets(4).Range("DG" & dict2(ValK2))
                    .Cells(dict(ValK), "N").Value = Sheets(4).Range("DI" & dict2(ValK2))
                End With
            End If
        Next ValK2
    Next ValK
 
Last edited:
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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