For Each to compare values in 2 separate columns

Peteor

Board Regular
Joined
Mar 16, 2018
Messages
152
Hello! I usually use the following to compare values (which I recognize is clunky, but for smaller comparisons it gets the job done). I need to accomplish this comparison, but my ranges are too large, and Excel is locking up. This is my "go-to" method to accomplish this. I would love if someone could provide a bit of insight on how to "optimize" this method... Asking 1 question 79,171,600 times (115,075*688) seems a bit excessive here...

Sub Macro1()
Dim Cel As Range
Dim Rng As Range
Dim Cel2 As Range
Dim Rng2 As Range

Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False

Set Rng = Range("A2:A688")
Set Rng2 = Range("J2:J115075")

For Each Cel In Rng
For Each Cel2 In Rng2

If Cel.Value = Cel2.Value And Cel.Offset(0, 1).Value = Cel2.Offset(0, 1).Value Then
Cel2.Offset(0, 5).Value = Cel.Offset(0, 3).Value

Else
End If

Next Cel2
Next Cel

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Do you have any formulae in cols J:O?
 
Upvote 0
Ok, how about
VBA Code:
Sub Peteor()
   Dim Ary As Variant
   Dim Cl As Range
   Dim i As Long
   
   Ary = Range("J2:O" & Range("J" & Rows.Count).End(xlUp).Row).Value2
   With CreateObject("scripting.dictionary")
      For Each Cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
         .Item(Cl.Value & "|" & Cl.Offset(, 1).Value) = Cl.Offset(, 3).Value
      Next Cl
      For i = 1 To UBound(Ary)
         If .Exists(Ary(i, 1) & "|" & Ary(i, 2)) Then Ary(i, 6) = .Item(Ary(i, 1))
      Next i
   End With
   Range("J2").Resize(UBound(Ary), 6).Value = Ary
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,577
Members
449,039
Latest member
Arbind kumar

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