VBA Vlookup - Correct Way w/ Loops

SteveOranjinSteve

Board Regular
Joined
Nov 18, 2019
Messages
78
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hello,

I'm a fairly rudimentary VBA user. I'm trying to get something working in a tutorial so I can start doing some vlookups through VBA on my own, but I need an example I can work from. My guess is that in order for a successful table to be populated, we have to write some kind of loop that allows the vlookup to loop through the lookup table and return it (in the "return table".) So far all I've got is the actual VLOOKUP working, I don't know how to institute two different loops @ the same time, so that it'll loop through the one table, and then loop through the other table, return the result, and then go back to the other table and grab the next result. Can someone show me the code that would be required to do this? The link for the sheet is here.

VBA Code:
https://1drv.ms/x/s!AuLIQSvmFBoEngRYVzrkMYNwf1tZ
 
Personally I would not use vlookup in a macro, as it tends to be slow, especially on large amounts of data.
I would use a dictionary, failing that either loop through an array, or use Range.Find
Does it need to be a vlookup?
 
Upvote 0
Run this macro on the sheet "return table"

VBA Code:
Sub Macro3()
  Dim lr As Long
  lr = Sheet3.Range("A" & Rows.Count).End(xlUp).Row
  With Range("G3:G" & Range("A" & Rows.Count).End(xlUp).Row)
    .FormulaR1C1 = "=VLOOKUP(RC[-6],lookup_table!R3C1:R" & lr & "C4,3,0)"
    .Value = .Value
  End With
  With Range("H3:H" & Range("A" & Rows.Count).End(xlUp).Row)
    .FormulaR1C1 = "=VLOOKUP(RC[-7],lookup_table!R3C1:R" & lr & "C4,4,0)"
    .Value = .Value
  End With
End Sub
 
Upvote 0
Personally I would not use vlookup in a macro, as it tends to be slow, especially on large amounts of data.
I would use a dictionary, failing that either loop through an array, or use Range.Find
Does it need to be a vlookup?

No, it does not need to be a Vlookup. I am looking to practice using VBA, which is why I am doing it.

Kinda having a hard time finding practical tasks which I can apply myself to in the beginning of learning.

Do you know of any good tutorials?

Steve
 
Upvote 0
In that case, using a dictionary
VBA Code:
Sub SteveOranjin()
    Dim Cl As Range
    Dim Dic As Object
    
    Set Dic = CreateObject("scripting.dictionary")
    With Sheets("Lookup_table")
        For Each Cl In .Range("A3", .Range("A" & Rows.Count).End(xlUp))
            Dic(Cl.Value) = Cl.Offset(, 2).Resize(, 2).Value
        Next Cl
    End With
    With Sheets("Return table")
        For Each Cl In .Range("A3", .Range("A" & Rows.Count).End(xlUp))
            If Dic.Exists(Cl.Value) Then Cl.Offset(, 6).Resize(, 2).Value = Dic(Cl.Value)
        Next Cl
    End With
End Sub
For more info on dictionaries, have a look here Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery
 
Upvote 0

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