Does X exist in Array

Jonmo1

MrExcel MVP
Joined
Oct 12, 2006
Messages
44,061
I'm still learning to use arrays, but they are very useful. To this point i have used a line like this to determine if a value exists within the array.

x = array("Test","Test2","Test3"))
Y = worksheetfunction.match("Test2", x)
IF Y <> "" Then ....etc.

Is there an easier way to check if a value exists within an array?


Thanks
 

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.
You could loop through it if you want, but I'm not sure that it gains you much, and would probably take longer if you had a larger array. But personally, I would use a variable type for the type of value that Match returns, namely a number, and then check for a non-zero return.

Code:
Sub test()
Dim x, y As Long
On Error Resume Next
x = Array("Test", "Test2", "Test3")
y = Application.WorksheetFunction.Match("Test2", x)
If y Then
    MsgBox "Found it"
End If
End Sub
 
Upvote 0
So I had the basic idea right? using match?

There's no built in VBA function that would return true/false like

ISIN("Test2", X)


suppose I could make a UDF...
 
Upvote 0

Forum statistics

Threads
1,214,925
Messages
6,122,301
Members
449,078
Latest member
nonnakkong

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