Vlookup range as a collection

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,834
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
In a vlookup, can the range be a collection?

Say I have only two values in cells A1 and A2.

If the range is an array, it works:

Rich (BB code):
    Dim MyArray() As Variant
    
    MyArray() = Cells(1, 1).CurrentRegion.Value
    
    Dim g As Integer
    
    g = 2
    
    Dim j As Variant
    
    j = Application.VLookup(g, MyArray(), 1, False)



but I tried changing from an array to a collection and got an error:

Rich (BB code):
    Dim MyColl As Collection
    Set MyColl = New Collection
    
    Dim i As Integer
    
    For i = 1 To 2
    
        MyColl.Add Cells(i, 1).Value
        
    Next i
    
    Dim g As Integer
    
    g = 2
    
    Dim j As Variant
    
    j = Application.VLookup(g, MyColl, 1, False)



The error message was:

Rich (BB code):
Application-defined or object-defined error

Have I made a mistake with the syntax or can't the range in a vlookup be a collection?

Thanks

<strike>
</strike>
 
Last edited:

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
The search column must be text. In my example, look at column A are numbers but captured as text.


<table border="1" cellspacing="0" style="font-family:Calibri,Arial; font-size:11pt; background-color:#ffffff; "> <colgroup><col style="font-weight:bold; width:30px; " /><col style="width:76.04px;" /><col style="width:76.04px;" /></colgroup><tr style="background-color:#cacaca; text-align:center; font-weight:bold; font-size:8pt; "><td > </td><td >A</td><td >B</td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >1</td><td >TEXT</td><td >MESSAGE</td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >2</td><td style="text-align:right; ">2</td><td >hi</td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >3</td><td style="text-align:right; ">3</td><td >bye</td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >4</td><td style="text-align:right; ">4</td><td >good</td></tr></table>

Code:
Sub test()
  Dim a(), b As String, c As Variant
  a = Range("A2:B3")
  b = "3"
  c = [COLOR=#0000ff]WorksheetFunction[/COLOR].VLookup(b, a, 2, 0)
End Sub

If the value does not exist in column A, then the macro stop, to continue you can use Application:

Code:
Sub test()
  Dim a(), b As String, c As Variant
  a = Range("A2:B3")
  b = [COLOR=#ff0000]"5"[/COLOR]
  c = [COLOR=#0000ff]Application[/COLOR].VLookup(b, a, 2, 0)
End Sub
 
Upvote 0
Thanks but your first example gives me an error!

Furthermore, can the range be a collection?
 
Upvote 0
can the range be a collection?
Not that I'm aware of, it needs to be a range or an array.
You could also use a dictionary and get rid of the vlookup completely
 
Last edited:
Upvote 0
For reference, with a dictionary it would be something like
Code:
Sub tiredofit()
   Dim Cl As Range
   
   With CreateObject("scripting.dictionary")
      For Each Cl In Range("A2:A12")
         .Item(Cl.Value) = Cl.Offset(, 1).Value
      Next Cl
      Debug.Print .Item("Kent")
   End With
End Sub
EDIT:
Forgot to add sample data


Book1
AB
1CountyWard
2East Riding of YorkshireKings Park
3CambridgeshireRoman Bank
4KentWalland & Denge Marsh
5DorsetPoole Town
6Greater LondonThamesmead Moorings
7DevonTeignmouth East
8East SussexDevonshire
9South YorkshireThorne & Moorends
10SuffolkWestgate
11NorfolkCentral and Northgate
12LincolnshirePinchbeck and Surfleet
Sheet1
 
Last edited:
Upvote 0
Thanks but your first example gives me an error!

Furthermore, can the range be a collection?


What does the error say?


Remember that you must capture the numbers as text.


Or maybe I should put texts so that there is no confusion, try the following example:


<table border="1" cellspacing="0" style="font-family:Calibri,Arial; font-size:11pt; background-color:#ffffff; "> <colgroup><col style="font-weight:bold; width:30px; " /><col style="width:76.04px;" /><col style="width:76.04px;" /></colgroup><tr style="background-color:#cacaca; text-align:center; font-weight:bold; font-size:8pt; "><td > </td><td >A</td><td >B</td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >1</td><td >TEXT</td><td >MESSAGE</td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >2</td><td >x</td><td >hi</td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >3</td><td >y</td><td >bye</td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >4</td><td >z</td><td >good</td></tr></table>

Code:
Sub test()
  Dim a(), b As String, c As Variant
  a = Range("A2:B3")
[B][COLOR=#0000ff]  b = "Y"[/COLOR][/B]
  c = Application.VLookup(b, a, 2, 0)
End Sub
 
Upvote 0
You're welcome & thanks for the feedback.

Also please do not quote entire posts as it just clutters up the thread. Thanks
 
Upvote 0

Forum statistics

Threads
1,215,052
Messages
6,122,878
Members
449,097
Latest member
dbomb1414

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