Perform Case-Sensitive Search by VBA Find MatchCase – 2 Examples

How to perform a case-sensitive search using VBA Find function. We have shown examples of using “What” and “After” arguments in this VBA Find tutorial.

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

The complete syntax of the VBA Find function is:

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

We have shown 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 to False and only used 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, still 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

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