Perform Case-Sensitive Search by VBA Find MatchCase

How to perform a case-sensitive search using VBA Find function

The complete syntax of the VBA Find function:

expression.Find (What, After, LookIn, LookAt, SearchOrder, SearchDirection, MatchCase, MatchByte, SearchFormat)

We have shown you examples of using “What” and “After” arguments in this VBA Find tutorial.

In this tutorial, we will show how to use MatchCase in the Find function.

Our sample sheet:

VBA find match sheet

What is the default value of MatchCase?

The default value is False.

That means, if you do not provide a value, the search will be case-insensitive.

In the example below, we do not provide a value as False and only use the “Word” argument.

See the code and result to have an idea:

Sub find_case()

Dim cell As Range

Set cell = Range("D2:D11").Find("out of stock")

MsgBox cell.Address

End Sub

Output:

VBA find match False

You can see, we searched for “out of stock” in our sample sheet’s D column (Status) – all small letters. Though it’s “Out of Stock” in the D7 cell, the Find function returned its cell address (D7).

Find MatchCase value = True example

Now let us execute the same code as above and use MatchCase = True:

Sub find_case()
Dim cell As Range

Set cell = Range("D2:D11").Find("out of stock", MatchCase:=True)

MsgBox cell.Address

End Sub

Result:

VBA find match True example result

You can see, this time exactly “out of stock” is returned rather than “Out of Stock” in the range.

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!