Vlook-Up using a Macro

John_Daly

New Member
Joined
Feb 15, 2023
Messages
23
Office Version
  1. 365
Platform
  1. Windows

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
try this: It uses the dictionary object and variant arrays which makes the code super fast even for large numbers of rows.
VBA Code:
Sub DimIndxMat()
   Dim Ary As Variant
   Dim i As Long
   Dim Dic As Object
 
 
   Set Dic = CreateObject("Scripting.dictionary")
   With Worksheets("Sheet1")
      LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
      Ary = .Range(.Cells(1, 1), .Cells(LastRow, 2))
   End With
   For i = 2 To UBound(Ary)
      Dic(Ary(i, 2)) = Ary(i, 1)               ' Load Dictionary with Value in Column A
   Next i
   With Worksheets("Sheet1")
      LastRow = .Cells(Rows.Count, "C").End(xlUp).Row
      inarr = .Range(.Cells(1, 3), .Cells(LastRow, 3))
      outarr = .Range(.Cells(1, 4), .Cells(LastRow, 4))
  
      For i = 2 To LastRow
         If Dic.Exists(inarr(i, 1)) Then  ' check if element exists
         outarr(i, 1) = Dic(inarr(i, 1)) ' Load output array with dictionary value
         Else          ' if it not set to not found
         outarr(i, 1) = "Not Found"
         End If
        
      Next i
      .Range(.Cells(1, 4), .Cells(LastRow, 4)) = outarr  ' write out the output array
    
   End With
End Sub
 
Upvote 1
Solution
Thanks very much and also thanks for considering the large number of rows
 
Upvote 0

Forum statistics

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