Vlook-Up using a Macro

John_Daly

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

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
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,214,920
Messages
6,122,272
Members
449,075
Latest member
staticfluids

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