Elite Membership

VBA FIND

Written by WallStreetMojo Team WallStreetMojo Team Content Writers & Editors Our team of international writers & editors do in-depth research and fact check while creating informative articles, covering all aspects from basic to advanced. They employ their expertise to bring best possible content to our readers. 20+ years of experience Finance Excel View Full Profile
Reviewed by Dheeraj Vaidya, CFA, FRM Dheeraj Vaidya, CFA, FRM Content Reviewer & Course Director A former J.P.Morgan and CLSA Equity Analyst, Dheeraj specializes in financial modeling, AI, forecasting, and valuations. In his career spanning almost two decades, he has trained and mentored more than 100,000 students and professionals on a range of topics. 20+ years of experience CFA, FRM, IIT Delhi, IIM Lucknow Financial Modeling View Full Profile
Updated Dec 23, 2024
Read Time 13 min

FIND Function of VBA Excel

The FIND function of VBA excel searches a specific value in a specified range. It looks for the first instance of such value and if a match is found, the function returns the cell containing it. However, if a match is not found, the function does not return anything. The FIND function of VBA can return either an exact or a partial match.

Download FREE VBA FIND Excel Template and Follow Along!
Download Excel Template

For example, the following code looks for the string โ€œroseโ€ in the range A1:A10 of โ€œsheet1.โ€

With Sheets(“Sheet1”).Range(“A1:A10”)

Set Rng = .Find(What:=โ€œroseโ€)

The purpose of using the FIND function in VBA is to locate the desired value in a given dataset. With a VBA code, one can automate the task of finding values in Excel. Similar to the VBA FIND, there is a โ€œfind and replaceโ€ feature in Excel too. Let us revisit the latter to understand the former.

A Revisit to the โ€œFind and Replaceโ€ Feature of Excel

In this section, the โ€œfind and replaceโ€ dialog box of Excel has been explained briefly. The steps to find and replace a value in a worksheet are listed as follows:

Step 1: Press the keys โ€œCtrl+Fโ€ together to access the โ€œfind and replaceโ€ feature of Excel. Alternatively, from the โ€œeditingโ€ group of the Home tab, click the โ€œfind & selectโ€ drop-down. Next, select the option โ€œfind.โ€

VBA Find

Step 2: The โ€œfind and replaceโ€ dialog box appears, as shown in the following image. Click โ€œoptionsโ€ to see more features.

VBA Find and Replace

Step 3: The succeeding dialog box is displayed. This box helps find the value specified in the โ€œfind whatโ€ box. The search is subject to the following constraints:

  • Within: This determines whether the search would be conducted in a worksheet or workbook.
  • Search: This decides whether the search would be conducted in rows or columns.
  • Look in: This decides whether the search would be conducted in formulas, values or comments of Excel.

At any point of time, one can click โ€œoptionsโ€ to go back to the window shown in step 2.

one can click options to go back to the window

Step 4: Click the โ€œreplaceโ€ option in the โ€œfind and replaceโ€ dialog box. The โ€œreplace withโ€ option appears, as shown in the following image. This option is used when one value needs to be replaced by another.

Click the replace option in the find and replace dialog box

This is how the โ€œfind and replaceโ€ feature of Excel works. Let us now return to the FIND function of VBA Excel.

Syntax of the FIND function of VBA

The syntax of the FIND function of VBA is stated as follows:

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

The โ€œexpressionโ€ is the range object, which precedes the FIND function in a VBA code. The search range can be one or more rows, columns or the entire worksheet. The FIND function of VBA excel accepts the following arguments:

  • What: This is the value to be searched. It can be numeric, textual or any other data type of Excel. This argument is the same as the โ€œfind whatโ€ option of the โ€œfind and replaceโ€ window of Excel.
  • After: This indicates the cell after which the search will begin. It is entered as a single cell reference. If this argument is omitted, the search begins after the cell in the upper-left corner of the specified search range.
  • LookIn: This is the place (or data) where the value needs to be searched. It can be a comment (xlComments), formula (xlFormulas) or value (xlValues). The default value of this argument is xlFormulas. Further, this argument is the same as the โ€œlook inโ€ option of the โ€œfind and replaceโ€ window of Excel.
  • LookAt: This decides whether to match the content of the entire cell (exact match) or to match a part of the cell content (partial match). The constants are xlWhole and xlPart for exact and partial matches respectively. The default value of this argument is xlPart.
  • SearchOrder: This suggests the order of the search. One can specify whether the search will be in rows (xlByRows) or columns (xlByColumns). The default value of this argument is xlByRows. Further, this argument is the same as the โ€œsearchโ€ option of the โ€œfind and replaceโ€ window of Excel.
  • SearchDirection: This indicates the direction in which the search will be carried out. One can search downwards or in the next cell with the constant xlNext. Alternatively, one can search backwards (upwards) or in the previous cell with the constant xlPrevious. The default value of this argument is xlNext.
  • MatchCase: This decides whether the search is case-sensitive or not. If the search is case-sensitive, this argument is specified as true, otherwise it is false. The default value of this argument is false.
  • MatchByte: This is used if one has installed or selected double-byte language support. It must be specified as true, if double-byte characters are to be matched with double-byte characters. It must be specified as false, if double-byte characters are to be matched with their single-byte equivalents.
  • SearchFormat: This indicates whether the value to be searched should be in a specific format (like bold or italics) or not. If the search value should follow a formatting technique, this argument is specified as true, otherwise it is false. The default value of this argument is false.

Only the argument โ€œwhatโ€ is required. The rest of the arguments are optional.

The Excel VBA Find returns either of the following outcomes:

  • If a match is found, the function returns the first cell where the value is located.
  • If a match is not found, the function returns nothing. This is because the object of the function is set to nothing.

Alternatively, in case a match is not found, a customized message specified in the MsgBox function can be returned (refer to the code of the succeeding examples).

Note 1: The search begins after the cell specified in the โ€œafterโ€ argument and continues till the last cell of the search range. If the value is not found till this last cell, the search begins again from the first cell of the search range till the cell specified in the โ€œafterโ€ argument.

So, the cell specified in the โ€œafterโ€ argument is searched at the end of the search process. For more details on the usage of the โ€œafterโ€ argument, refer to example #2 of this article.

Note 2: Each time the FIND function of VBA is used, the settings of LookIn, LookAt, SearchOrder, and MatchByte are saved. So, if these values are omitted the next time the function is used, Excel VBA uses the previously saved values. Hence, it is recommended to explicitly state the given arguments each time search is performed by using the FIND function of VBA.

How to use the FIND Function of VBA Excel?

Let us consider some examples to understand the working of the Excel VBA Find.

Example #1โ€“Return the Cell Containing the First Instance of the Search Value

The following image shows some names in column A. We want to perform the following tasks in Excel VBA:

  • Write a VBA code to search the first instance of the name โ€œAranโ€ in column A.
  • Ensure that cell A2 is selected on running the code.

Use the FIND function of VBA.

VBA Find Example 1

The steps to search the given name (first instance) by using the FIND function of VBA are listed as follows:

Step 1: Make sure that the Developer tab is enabled in Excel. This tab will help write VBA codes. Once enabled, it will appear on the Excel ribbon, as shown in the following image.

Note: For steps related to enabling the Developer tab, click the given hyperlink.

VBA Find Example 1-1

Step 2: From the Developer tab, click โ€œvisual basic.โ€ Next, double-click โ€œsheet1โ€ and a blank pane will appear on the right side.

Write the following VBA code in this pane. This is shown in the succeeding image.

Sub Sample ()
Dim FindS As String
Dim Rng As Range
FindS = InputBox (“Enter the value you want to search”)
With Sheets (“Sheet1”) .Range (“A:A”)
VBA Find Example 1-2

Explanation of the code: The given code is explained as follows:

  • โ€œSampleโ€ is the function name given to โ€œsub.โ€
  • โ€œFindSโ€ contains the InputBox message.
  • โ€œRngโ€ is the variable defined for the range. In this example, the range is the entire column A (A:A).

Once the code is run, the InputBox message will appear the way it is shown in the following image. However, before running the code, let us complete it first by executing the subsequent steps.

Note: The InputBox function of VBA displays a dialog box in which the user is required to enter an input.

VBA Find Example 1-3

Step 3: Define the FIND function within the code. Notice that in the โ€œwhatโ€ argument, we have entered โ€œfindstring.โ€ Instead, in place of โ€œfindstring,โ€ one can enter whatever one needs to search within the defined range.

So, since โ€œAranโ€ needs to be searched in column A, it can be entered in place of โ€œfindstring.โ€ But, ensure that this name is entered within double quotes. In this way, one can specify the โ€œwhatโ€ argument of the FIND function.

VBA Find Example 1-4

Step 4: Close the function by entering the arguments (โ€œend if,โ€ โ€œend with,โ€ and โ€œend subโ€) shown in the following image.

VBA Find Example 1-5

Step 5: Save the file containing the VBA code as a macro-enabled workbook (.xlsm extension). Next, from the โ€œrunโ€ tab, select โ€œrun sub/user form (F5).โ€ This command runs the code.

On running the code, the succeeding dialog box appears. It shows the InputBox message that we had entered in step 2. In this box, enter the name โ€œAranโ€ (with or without double quotes) and click โ€œOk.โ€ Excel VBA will select cell A2, which contains the name โ€œAran.โ€

Had โ€œsheet1โ€ not contained the name โ€œAran,โ€ Excel VBA would have returned the message โ€œnothing found.โ€ This message is specified in the MsgBox function of the code.

Note: By default, Excel VBA returns the first instance of the search value in the defined range. However, for Excel VBA to return cell A2, the โ€œwhatโ€ argument as well as the following dialog box, should contain the string โ€œAran.โ€

VBA Find Example 1-3

Example #2โ€“Return the Cell Containing the Second Instance of the Search Value

The following image contains some names in the range A1:A10. Notice that the name โ€œAranโ€ appears twice in column A. Write a VBA code to search and select the second instance of the name โ€œAranโ€ in column A (i.e., cell A6).

Use the FIND function of VBA Excel.

Example 2

The steps to search the second instance of the given name by using the FIND function of VBA are listed as follows:

Step 1: Access the Developer tab from the Excel ribbon. Click โ€œvisual basicโ€ displayed on the left side of this tab.

From โ€œMicrosoft Excel objects,โ€ select โ€œSheet2.โ€ This is because โ€œSheet2โ€ of Excel contains the dataset shown in the question of the example.

Note: For more details on Customizing the ribbon, click this hyperlink.

Example 2-2

Step 2: Keep โ€œsheet2โ€ selected and from the โ€œinsertโ€ tab, choose โ€œmodule.โ€ A blank pane appears on the right side, as shown in the following image.

Example 2-3

Step 3: Begin to write the code in the blank pane. Define the function as โ€œsub sample2()โ€ and press the โ€œEnterโ€ key. This is the first part of the code, which is written without the double quotation marks, as shown in the following image.

Example 2-4

Step 4: Define the variables of the code. This time โ€œrng1โ€ is the variable defined for range. The same is shown in the following image.

Example 2-5

Step 5: Define the InputBox function for the โ€œfindSโ€ variable. The InputBox message stays the way it was written in step 2 of the preceding example.

Example 2-6

Step 6: Enter the name of the worksheet in which the FIND function needs to conduct a search. Specify the range to be searched as well. So, we enter โ€œsheet2โ€ and range โ€œA:Aโ€ within double quotation marks. This is shown in the following image.

Example 2-7

Step 7: Define the FIND function. At present, the โ€œwhatโ€ argument contains โ€œfindstring.โ€ Since the name โ€œAranโ€ is to be searched in column A, enter this name in place of โ€œfindstring.โ€ Ensure that this name is entered within double quotes.

Further, since the second instance of the name โ€œAranโ€ needs to be searched, specify the โ€œafterโ€ argument. Enter โ€œA2โ€ as the โ€œafterโ€ argument. This is because we want the search to begin after cell A2.

The arguments of the VBA FIND function are shown in the following image.

Example 2-8

Explanation of the โ€œafterโ€ argument: The search begins after cell A2 and continues till the last cell of column A. This is because the search range has been specified as column A (A:A).

So, since the search begins from cell A3, the value โ€œAranโ€ is found in cell A6 of โ€œSheet2.โ€ Hence, cell A6 will be selected by the FIND function on running the code.

Had the name โ€œAranโ€ not been found from cell A3 till the last cell of column A, the search would again begin from cell A1 and end at cell A2 this time. Thus, the cell specified in the โ€œafterโ€ argument is searched right at the end of the search process.

Step 8: Close the code by ending the โ€œifโ€ and โ€œwithโ€ conditions. Close the โ€œsubโ€ argument by writing โ€œend sub.โ€ The complete code is shown in the succeeding image.

Run the code by selecting โ€œrun sub/user form (F5)โ€ from the โ€œrunโ€ tab of Excel VBA. The InputBox message appears asking for a value to search. When the name โ€œAranโ€ is entered in this box, the outcome is the selection of cell A6. This cell contains the second instance of the name โ€œAran.โ€

Had a match not been found in the entire โ€œsheet2,โ€ the output would have been โ€œnothing found.โ€ This response is defined by the MsgBox function of the code.

Example 2-9

Frequently Asked Questions (FAQs)

1. Define the FIND function of VBA Excel.

The FIND function of VBA searches for a specified value in the range defined by the user. To search, a VBA code is written by entering some or all arguments of the FIND function. One can specify the direction of search, order of search, data to be searched, the format of the search value, and so on.

The FIND function returns the cell containing the specified value. If a match is not found, the function does not return anything.

Note: For the syntax of the FIND function, refer to the heading โ€œsyntax of the FIND function of VBAโ€ of this article.

2. How to find the last occurrence of a text string by using the FIND function of VBA?

To find the last occurrence, specify the โ€œafterโ€ argument in the VBA code. This argument tells VBA the exact cell after which the search should begin. Note that, at a given time, a single cell reference can be supplied in this argument. It is not possible to list multiple cell references in the โ€œafterโ€ argument.

The โ€œafterโ€ argument is entered following the โ€œwhatโ€ argument of the FIND function. For instance, if the search range is A1:A25 and cell A20 contains the second last occurrence of the search value, the code is written as follows:

With Sheets (โ€œSheet4โ€).Range(โ€œA1:A25โ€)
Set Rng = .Find(What:=โ€œtextstringโ€, After:=Range(โ€œA20โ€))

With this code, the search for the value โ€œtextstringโ€ begins after cell A20 in โ€œsheet4.โ€ The FIND function searches in the range A21:A25 and returns the last occurrence of the search value. If a match is not found in the range A21:A25, the function searches in the range A1:A20.

Note: For details related to the working of the โ€œafterโ€ argument, refer to โ€œnote 1โ€ of the syntax and the โ€œexplanationโ€ in example #2 (after step 7) of this article.

3. By using the FIND function of VBA, how can one find a string by specifying some of its characters?

To find a string by specifying a part of it, either enter the LookAt argument as xlPart or omit this argument. By default, the FIND function matches the characters of the search value with the entire string. Then it returns the cell containing this entire string.

For instance, the code containing a part of a string is written as follows:

With Sheets(“Sheet4”).Range(“A1:A25”)
Set Rng = .Find(What:=”ssa”, LookAt:=xlPart)

This code searches the characters โ€œssaโ€ in the range A1:A25 of โ€œsheet4.โ€ The cell containing the value โ€œtext messageโ€ is returned, which is called a partial match. Hence, irrespective of whether the characters of the search value are placed at the beginning, middle or end of the string, Excel VBA returns a corresponding match.

Note: The xlPart constant can be omitted from the code because it is the default value of the FIND function. But, if this argument is specified, ensure that it is not placed within double quotation marks.

This has been a complete guide toย the VBAย FINDย function. Here we learn how to use Excel VBA FIND function with practical examples and a downloadable Excel sheet. You may also look at other articles related to Excel VBAโ€“