How to perform a case-sensitive search using VBA Find function
The complete syntax of the VBA Find function:
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:
What is the default value of MatchCase?
The default value is False.
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:
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:
You can see, this time exactly “out of stock” is returned rather than “Out of Stock” in the range.