Excel VBA Find
When we use Find in a normal worksheet we press keyboard shortcut CTRL + F and type the data we need to find and if not desired value we go to the next match, if there is a lot of such matches it is a tedious task but when we use FIND in VBA it does the tasks for us and give us the exact match and it takes three arguments, one is what to find, where to find and where to look at.
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, 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,
Find Function Syntax
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.
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 Find function which are optional. The only mandatory field required is what 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.
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.
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 Find 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.
Things to Remember
- 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 downloadable excel sheet. You may also have a look at other articles related to Excel VBA –