VBA create an array and use it in a Vlookup

anglais428

Well-known Member
Joined
Nov 23, 2009
Messages
634
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I am looking to define a list in VBA and then use that list as the range in a vlookup formula.

Something like the below:

MyRange = Array("AA","AB")

Result = Application.VLookup(Left(A2, 2), MyRange, 1, False)

The ideal results would be if whatever the first two digits of the text in cell A2 is contained in MyRange then it will return those two digits - else it with provide a #N/A error. E.g. if AA001 is in cell A2, the result would be "AA"; if it was "AC" then the result would be "#N/A".
Any ideas?
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
How about
Code:
myRange = Array("AA", "AB")

result = Application.VLookup(Left(Range("A2"), 2), myRange, 1, 0)
Range("A3") = result
 
Upvote 0
On which line do you get that error?
Also what is the value in A2?
 
Upvote 0
It was on the "results" line.
The values in A2 can be a mixture of numbers and text.
I want the result to be an error (#N/A) if the two letters are not found from the lookup, else return the two letters.

Thanks for your help
 
Upvote 0
Can you supply the whole code?
 
Upvote 0
I've changed to the below and it does give an #n/a which is what I was expecting. However, how would I modify this to keep the formula (currently it is the value rather than the formula in the cell)?

myRange = Array("CS", "GN", "GS", "NA", "NC", "NF", "NI", "NL", "NO", "NP", "NR", "NZ", "R0", "SA", "SC", "SF", "SG", "SI", "SL", "SO", "SP", "SR", "SZ")


Range("C2").Select

ActiveCell.Formula = Application.VLookup(Left(Range("B2"), 2), myRange, 1, 0)
 
Upvote 0
Try
Code:
ActiveCell.Formula = "=VLookup(Left(b2, 2),{""" & Join(myRange, """;""") & """}, 1, 0)"
 
Upvote 0
Fantastic - works perfectly. I would never have thought of the Join function.

Many thanks.
 
Upvote 0
You're welcome & thanks for the feedback
 
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