Vlookup range as a collection

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,924
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:
Your last example worked but the previous one showed:

Rich (BB code):
Error 2042

when I hovered the mouse over c.

The test is successful, the 5 does not exist in the data, that is why error returns. It is for you to observe the difference between Application and WorksheetFunction.

Code:
Sub test()
  Dim a(), b As String, c As Variant
  a = Range("A2:B3")
[COLOR=#ff0000][B]  b = "5"[/B][/COLOR]
  c = Application.VLookup(b, a, 2, 0)
End Sub
 
Upvote 0

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
But in your first example, ie this one:

Rich (BB code):
Sub test() Dim a(), b As String, c As Variant a = Range("A2:B3") b = "3" c = WorksheetFunction.VLookup(b, a, 2, 0) End Sub


the "3" does exist but it still returns an error.
 
Last edited:
Upvote 0
But in your first example, ie this one:

Rich (BB code):
Sub test() Dim a(), b As String, c As Variant a = Range("A2:B3") b = "3" c = WorksheetFunction.VLookup(b, a, 2, 0) End Sub


the "3" does exist but it still returns an error.

But you must put 3 as text in the cell.
Or try the post with my example in post #7
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,269
Members
452,628
Latest member
dd2

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