The direct answer: VBA has no built-in "in list" operator, so you test whether a string is in a list by looping through the list with For Each and comparing each item, by using Application.Match or Application.WorksheetFunction.Match against a range, by joining the list into a delimited string and using InStr, or by building a Dictionary or Collection and testing with its Exists method. The right choice depends on list size, whether the list lives in cells or in code, and whether you need exact or case-insensitive matching.
This matters because the naive approach — a linear loop over a long list — is the single most common cause of slow VBA macros. Understanding the alternatives lets you pick the one that fits the data source and the size of the job.
Why VBA Lacks an In Operator
VBA descends from classic BASIC, which never had a membership operator for arrays or collections. Languages such as Python (in), SQL (IN) and C# (Contains) provide one, but VBA's expression grammar does not. The closest native constructs are InStr, which searches inside a single string, and the Like operator, which does pattern matching on one string. Neither understands a list.
As a result, every "is this string in the list" test in VBA is really a search you write yourself, or a call into a library object that performs the search for you. The Excel object model supplies Application.Match, and the Scripting Runtime supplies Dictionary; both are far faster than a hand-written loop once the list grows beyond a few hundred items.
The Four Main Approaches Compared
Each method has a distinct performance profile and a distinct set of pitfalls. The table below summarises them before the detailed discussion.
- Loop with For Each or For i: works on any array or collection, exact comparison, O(n) per lookup. Fine for short lists, slow for long ones.
- Application.Match: works on a worksheet range or a one-dimensional array, returns an index or an error, O(n) but implemented in compiled code, so much faster than a VBA loop.
- InStr on a joined string: fast and simple, but vulnerable to substring false positives and delimiter collisions.
- Dictionary.Exists: near-constant-time lookup after a one-off build cost, ideal when the same list is queried many times.
Looping Through an Array or Collection
The most portable pattern uses For Each over a Variant array or a Collection and compares with = or StrComp:
- Declare a Boolean flag set to False.
- Loop over each element of the list.
- Compare the candidate string to the element, using
StrComp(candidate, element, vbTextCompare) = 0if the comparison must ignore case. - Set the flag to True and exit the loop on a match.
- Test the flag afterwards.
This is readable and needs no references, but each call scans the whole list until it finds a hit. With 10,000 items and 10,000 lookups, that is on the order of a hundred million comparisons, which will feel slow. Note that VBA's default = comparison for strings is case-sensitive unless Option Compare Text is set at the top of the module — a frequent source of surprise.
Using Application.Match Against a Range
When the list lives in worksheet cells, Application.Match is the natural tool. It takes a value, a one-dimensional range or array, and a match type (0 for exact). It returns the position as a Double, or an error value if nothing is found. Because the search runs in Excel's compiled engine rather than the VBA interpreter, it is typically an order of magnitude faster than a VBA loop over the same data.
The critical detail is error handling: when there is no match, Application.Match returns an error Variant rather than raising a trappable error, so you must test with IsError before using the result. WorksheetFunction.Match behaves differently — it raises run-time error 1004 on failure, which requires an error handler or On Error Resume Next. Mixing the two up is a classic bug. Match is also limited to lists that fit in a single row or column.
The InStr Trick and Its Traps
A popular shortcut joins the list into one string with a delimiter and calls InStr:
If InStr(1, "|" & Join(listArray, "|") & "|", "|" & candidate & "|", vbTextCompare) > 0 Then ...
The surrounding delimiters are essential. Without them, searching for "Ann" inside "|Anna|Joanne|" produces a false positive. Even with delimiters, the method breaks if any list item or the candidate itself contains the delimiter character. It is fast for moderate lists and very compact, but it is the least robust of the four approaches and should be reserved for lists you control completely.
Dictionary and Collection Lookups
The Scripting Runtime's Dictionary offers an Exists method with effectively constant-time lookup. You add each list item as a key once, then test candidates with a single call. For repeated lookups against a stable list, this is usually the fastest option in pure VBA, and it handles case sensitivity through its CompareMode property. The trade-offs are a reference to the Microsoft Scripting Runtime (or late binding with CreateObject("Scripting.Dictionary")) and the memory cost of the keys. A Collection can also be used, but it has no direct existence test, so you must either loop or trap the error raised by accessing a missing key — the latter is slow and considered poor practice.
Choosing the Right Method
Decide by answering three questions:
- Where does the list live? Cells favour
Application.Match; code arrays favour a loop or Dictionary; a fixed short list favours a loop. - How many lookups will you perform? One or a few favours Match or a loop; thousands favour a Dictionary built once.
- Does case matter? If not, set
Option Compare Text, usevbTextComparewithStrComp, or set the Dictionary'sCompareModebefore adding keys.
A reasonable default: use Application.Match for worksheet lists, a Dictionary for repeated lookups in code, and a plain loop only for short, one-off checks.
Common Problems and Misconceptions
Several errors recur in practice. The most common is assuming InStr or = is case-insensitive by default; it is not, unless Option Compare Text is declared. Another is forgetting that Application.Match returns an error value rather than raising one, so the result is silently misused. A third is using InStr without delimiters and getting substring matches. Finally, many developers rebuild a Dictionary inside a loop instead of once outside it, destroying the performance advantage entirely.
Frequently Asked Questions
Is there an "in" operator in VBA?
No. VBA has no membership operator. You must loop, use Application.Match, join and search with InStr, or use a Dictionary's Exists method.
How do I check a string against a list ignoring case?
Add Option Compare Text at the top of the module, or use StrComp with vbTextCompare, or set the Dictionary's CompareMode to vbTextCompare before adding keys.
Why does Application.Match not raise an error when nothing is found?
Application.Match returns an error Variant, which you test with IsError. WorksheetFunction.Match raises run-time error 1004 instead. The two behave differently and are often confused.
Which method is fastest for large lists?
A Dictionary built once and queried with Exists is generally fastest for many lookups, while Application.Match is fastest for a single lookup against a worksheet range. A VBA loop is slowest for large lists.
Can I use InStr to test membership?
Yes, if you wrap both the list and the candidate in delimiters to prevent substring matches, but it fails when items contain the delimiter. It is a convenience, not a robust membership test.