Table of Contents
Excel VBA Find
Before we move to VBA and start using find function in macros we need to learn first what is a find function in excel. In normal excel in the home tab under the editing group, we can find a find function which is used to find a string or a value in a cell range or whole worksheet.
When we click on this find and select we get two options;
One is simple to find,
We can see it has also a mode of options which opens up another feature.
It does the find algorithm with four constraints, Find What, Within, Search and look in.
The second option in excel is to Find and replace which is used when we find a string but what to replace it with any other value,
Introduction to VBA Find Excel Function
We have learned above what is Find in basic excel. In VBA we write codes manually but the features are the same as normal excel. First, let us look at the syntax of VBA Find Excel Function:
If the value we are looking is found with the excel function it returns the cell where the value is and if the value is not found then the object of the function is set to nothing.
Expressions in macros are ranges defined such as range 1 or range 2. What is a keyword for what we want to search a specific value? Lookin is a keyword for what we are trying to search is it a comment or a formula or a string. Similarly, there are other constraints in VBA Find Excel function which are optional. The only mandatory field required in a find function is What which is a value we are trying to search.
Basically, VBA find Excel has one required argument which is What which value we want to search. The rest of the constraints are optional and there are many constraints in find function. Find function is similar to what a find function is in excel.
The parameter for find function is the range of cells. Like in which range we want to find a value. It can be a few columns or few cells or whole worksheet.
Examples of VBA Find Function
We will learn how to use a VBA Find Excel function with few examples.
Excel VBA Find Example #1
Suppose our data has the following values
We will try to find “Aran” in the same data.
- To write a VBA code it is necessary to have enabled the developer tab in order to be able to write VBA Codes.
- We start writing our code by writing the following code as shown below,
DimFindS As String
Dim Rng As Range
FindS = InputBox (“Enter the value you want to search”)
With Sheets (“Sheet1”) .Range (“A:A”)
- The sample is the function name given to sub.
- Find is the string we want the user we want to enter to search.
- Rng is the variable we took for the range.
- Now we ask the user to enter the value which looks like the screenshot below,
- Now we will define our find function in the module.
- The function finds the value entered by the user in the given range.
- Now we close the function by the following arguments.
- Now if we run our code first it asks for a prompt by the user for a value.
- Once the code is completed it returns the cell to where the data was found.
Excel VBA Find Example #2
In the above example, there were four unique names but what if there were more than one names in the data, such as consider the below data,
We can see that the name Aran is repeated twice in the above data. If excel has to find the name Aran it will find it in cell A2 and stop, but there is another value similar to that of A2 in cell A6. How to fetch that value? Here comes the syntax of Find(What, After) in help.
After defines cell after which reference we want to search the data.
Let us write the code for the above data.
- Always remember to enable developer tab from options and then from customizing ribbons to be able to write the code in VBA.
- In VBA we get Microsoft excel objects which is a module where we write the codes.
- Previously we were working on sheet 1 now we are working in sheet 2 so select sheet 2 for another module and a blank page appears.
- Now start writing the code by defining the function first as SUB Sample2() and press enter.
- Now we have defined our function we will start getting into the main part which is defining our variables.
- Define what does the Findd variable should have,
- Select the sheets which we are working on which is sheet 2 in this example,
- Now we will find the text whatever the user enters after A2 cell, so we define our find function as below,
- Now we close the code by ending the with and if conditions.
What the above code does is search the string after the cell A2 and returns the cell wherever it is found.
Also, you can download the VBA Find Excel Template here:- VBA FIND Function Excel Template
How to Use VBA Find Function?
The syntax for VBA Find Excel function is as below;
As defined above expressions in macros are ranges defined. What is a keyword for what we want to search a specific value. There are other constraints in VBA Find function such as lookin which are optional.
Things to remember About VBA Find Function
- First things first we need to enable the developer tab in order to use VBA.
- What is the specific value we need to find?
- If the value is not found, the object of the function is set to nothing.
This has been a Complete Guide to VBA Find Function. Here we learn how to use Excel VBA Find function with practical examples and codes. You may also have a look at other articles related to Excel VBA –