Excel find column with value

Summary

This step-by-step article describes how to find data in a table (or range of cells) by using various built-in functions in Microsoft Excel. You can use different formulas to get the same result.

Create the Sample Worksheet

This article uses a sample worksheet to illustrate Excel built-in functions. Consider the example of referencing a name from column A and returning the age of that person from column C. To create this worksheet, enter the following data into a blank Excel worksheet.

You will type the value that you want to find into cell E2. You can type the formula in any blank cell in the same worksheet.

A

B

C

D

E

1

Name

Dept

Age

Find Value

2

Henry

501

28

Mary

3

Stan

201

19

4

Mary

101

22

5

Larry

301

29

Term Definitions

This article uses the following terms to describe the Excel built-in functions:

Term

Definition

Example

Table Array

The whole lookup table

A2:C5

Lookup_Value

The value to be found in the first column of Table_Array.

E2

Lookup_Array
-or-
Lookup_Vector

The range of cells that contains possible lookup values.

A2:A5

Col_Index_Num

The column number in Table_Array the matching value should be returned for.

3 (third column in Table_Array)

Result_Array
-or-
Result_Vector

A range that contains only one row or column. It must be the same size as Lookup_Array or Lookup_Vector.

C2:C5

Range_Lookup

A logical value (TRUE or FALSE). If TRUE or omitted, an approximate match is returned. If FALSE, it will look for an exact match.

FALSE

Top_cell

This is the reference from which you want to base the offset. Top_Cell must refer to a cell or range of adjacent cells. Otherwise, OFFSET returns the #VALUE! error value.

Offset_Col

This is the number of columns, to the left or right, that you want the upper-left cell of the result to refer to. For example, «5» as the Offset_Col argument specifies that the upper-left cell in the reference is five columns to the right of reference. Offset_Col can be positive (which means to the right of the starting reference) or negative (which means to the left of the starting reference).

Functions

LOOKUP()

The LOOKUP function finds a value in a single row or column and matches it with a value in the same position in a different row or column.

The following is an example of LOOKUP formula syntax:

   =LOOKUP(Lookup_Value,Lookup_Vector,Result_Vector)


The following formula finds Mary’s age in the sample worksheet:

   =LOOKUP(E2,A2:A5,C2:C5)

The formula uses the value «Mary» in cell E2 and finds «Mary» in the lookup vector (column A). The formula then matches the value in the same row in the result vector (column C). Because «Mary» is in row 4, LOOKUP returns the value from row 4 in column C (22).

NOTE: The LOOKUP function requires that the table be sorted.

For more information about the LOOKUP function, click the following article number to view the article in the Microsoft Knowledge Base:
 

How to use the LOOKUP function in Excel

VLOOKUP()

The VLOOKUP or Vertical Lookup function is used when data is listed in columns. This function searches for a value in the left-most column and matches it with data in a specified column in the same row. You can use VLOOKUP to find data in a sorted or unsorted table. The following example uses a table with unsorted data.

The following is an example of VLOOKUP formula syntax:

    =VLOOKUP(Lookup_Value,Table_Array,Col_Index_Num,Range_Lookup)

The following formula finds Mary’s age in the sample worksheet:

   =VLOOKUP(E2,A2:C5,3,FALSE)

The formula uses the value «Mary» in cell E2 and finds «Mary» in the left-most column (column A). The formula then matches the value in the same row in Column_Index. This example uses «3» as the Column_Index (column C). Because «Mary» is in row 4, VLOOKUP returns the value from row 4 in column C (22).

For more information about the VLOOKUP function, click the following article number to view the article in the Microsoft Knowledge Base:
 

How to Use VLOOKUP or HLOOKUP to find an exact match

INDEX() and MATCH()

You can use the INDEX and MATCH functions together to get the same results as using LOOKUP or VLOOKUP.

The following is an example of the syntax that combines INDEX and MATCH to produce the same results as LOOKUP and VLOOKUP in the previous examples:

    =INDEX(Table_Array,MATCH(Lookup_Value,Lookup_Array,0),Col_Index_Num)

The following formula finds Mary’s age in the sample worksheet:


    =INDEX(A2:C5,MATCH(E2,A2:A5,0),3)

The formula uses the value «Mary» in cell E2 and finds «Mary» in column A. It then matches the value in the same row in column C. Because «Mary» is in row 4, the formula returns the value from row 4 in column C (22).

NOTE: If none of the cells in Lookup_Array match Lookup_Value («Mary»), this formula will return #N/A.
For more information about the INDEX function, click the following article number to view the article in the Microsoft Knowledge Base:

How to use the INDEX function to find data in a table

OFFSET() and MATCH()

You can use the OFFSET and MATCH functions together to produce the same results as the functions in the previous example.

The following is an example of syntax that combines OFFSET and MATCH to produce the same results as LOOKUP and VLOOKUP:

   =OFFSET(top_cell,MATCH(Lookup_Value,Lookup_Array,0),Offset_Col)

This formula finds Mary’s age in the sample worksheet:

   =OFFSET(A1,MATCH(E2,A2:A5,0),2)

The formula uses the value «Mary» in cell E2 and finds «Mary» in column A. The formula then matches the value in the same row but two columns to the right (column C). Because «Mary» is in column A, the formula returns the value in row 4 in column C (22).

For more information about the OFFSET function, click the following article number to view the article in the Microsoft Knowledge Base:
 

How to use the OFFSET function

Need more help?

Want more options?

Explore subscription benefits, browse training courses, learn how to secure your device, and more.

Communities help you ask and answer questions, give feedback, and hear from experts with rich knowledge.

Last week I had an email from Mike asking how he can lookup a suburb in a range of columns and return the post code from the header row.

I imagine his data was a bit like this:

Excel find column containing a value

And in cell B9 he wants to find the post code for Herston.

One way is with this array formula:

=INDEX(B1:F1,,MIN(IF(B2:F5=A9,COLUMN(A:E))))

Entered with CTRL+SHIFT+ENTER.

Enter your email address below to download the sample workbook.

By submitting your email address you agree that we can email you our Excel newsletter.

Before we dive in, here are the syntaxes for the INDEX and IF functions as a reminder (I’ve crossed out the arguments we’re not using):

INDEX(reference,row_num,[column_num],[area_num])
IF(logical_test, [value_if_true],[value_if_false])

The INDEX formula is returning a reference to the cell in the first row for the column containing ‘Herston’. For the column_num argument it uses a combination of IF, COLUMN and MIN.

Here it is again for reference:

=INDEX(B1:F1,,MIN(IF(B2:F5=A9,COLUMN(A:E))))

In English the above formula reads:

Check the cells in the range B2:F5 for ‘Herston’ and tell INDEX what column number it’s in. i.e. column 4. INDEX (look in) the range B1:F1 and return a reference to the 4th cell i.e. E1, which contains 4006.

So what’s MIN got to do with it….hold your horses, more on that in a moment.

Let’s step through the formula in the order it evaluates:

Step 1 — IF function’s logical_test: B2:F5=A9 i.e. B2:F5=Herston and it looks like this:

=INDEX(B1:F1,,MIN(IF({FALSE,FALSE,FALSE,FALSE,FALSE;  FALSE,FALSE,FALSE,FALSE,FALSE; FALSE,FALSE,FALSE,TRUE,FALSE;  FALSE,FALSE,FALSE,FALSE,FALSE},,COLUMN(A:E))))

Tip: did you notice in the formula above there is a semicolon after every 5th ‘FALSE’ instead of a comma. This semicolon represents a new row in the array.

Or if you imagine our formula is putting together a list of values representing each row in the table like this:

Excel find column containing a value

Step 2 — COLUMN function: This evaluates to return a horizontal array of numbers {1,2,3,4,5} for our IF function’s value_if_true argument. These numbers represent the 5 columns B:F in our table.

Our formula now looks like this:

=INDEX(B1:F1,,MIN(IF({FALSE,FALSE,FALSE,FALSE,FALSE;FALSE,FALSE,FALSE,FALSE,FALSE;FALSE,FALSE,FALSE,TRUE,FALSE;FALSE,FALSE,FALSE,FALSE,FALSE},{1,2,3,4,5})))

Tip: Instead of using the COLUMN function to generate the array of numbers we could simply type {1,2,3,4,5} into the formula. However, with large horizontal arrays it’s quicker (and dynamic) if we use the COLUMN function to generate the array, or for vertical arrays you can use the ROW function.

Note: if you don’t want your COLUMN function to be dynamic you can use the INDIRECT function to fix it, like this:

=INDEX(B1:F1,,MIN(IF(B2:F5=A9,COLUMN(INDIRECT("A:E")))))

Step 3 — IF function value_if_true: The IF function finishes evaluating by assigning the value_if_true numbers (generated by the COLUMN function) to the TRUE results in the logical_test.

To visualise this we can look at the 3rd horizontal array (i.e. the series of FALSE/TRUE after the second semicolon below). Remember this is the 3rd row of our table above.

Excel gives the TRUE results the corresponding number from the array generated from the COLUMN function {1,2,3,4,5} like so:

Excel find column containing a value

Note: In this step the FALSE values evaluate to nothing i.e. they are ignored. Remember we don’t have a value_if_false argument in our IF formula. Our formula now looks like this:

Excel find column containing a value

Step 4 – MIN: This simply evaluates to find the one and only number; 4.

=INDEX(B1:F1,, 4)

Tip: Since there is only one number remaining (the rest are all FALSE) we could have used MAX or SUM to get the same result as MIN.

Step 5 – INDEX: Finally INDEX can return a reference to the 4th column in the range B1:F1 which is cell E1 containing post code 4006.

Tip: Notice how our INDEX formula doesn’t have a row_num argument:

Excel find column containing a value

Since our reference is only one row high we don’t have to type a 1 in for the row_num argument, we simply enter a comma as a placeholder and continue on to the column_num argument.

What The?

Did you find that tricky?

When working with long or complex formulas I like to use the Evaluate Formula tool to understand what’s going on behind the scenes.

You can also evaluate parts of your formula by highlighting the section of the function in the formula bar and pressing the F9 key. Below I’ve evaluated the COLUMN(A:E) part of my formula:

Excel find column containing a value

To revert to the original formula either press the escape key or CTRL+Z.

Thanks

Thanks to Mike for inspiring this post.

If you liked this please share it with your friends and colleagues.

Simply use the icons below to share it on Google +1, LinkedIn, Facebook and Twitter, or leave me a comment and tell me what formula would you use to find the Post Code?

You can use this (it will return E:E, which you could then use INDIRECT with to return a workable range). This uses «words» as the word to match and stop at the first match:

=SUBSTITUTE(
    ADDRESS(1,MATCH("words",$A$1:$Z$1,0),4),"1",
    ":"&
    SUBSTITUTE(ADDRESS(1,MATCH("words",$A$1:$Z$1,0),4),"1",""))

enter image description here

This works by using MATCH to find the position of the occurrence in your range (in this case, 5), and then using ADDRESS, with row_num = 1 and column_num = the result of the match. Using 4 for the abs_num argument ensures that the returned value will be without the $‘s. You then substitute out the 1 in E1 with «:» and concatenate it with the same formula, giving you E:E as a string. You can then do look-ups based on that range (using INDIRECT), such as this:

enter image description here

We have the table in which the sales volumes of certain products are recorded in different months. It is necessary to find the data in the table, and the search criteria will be the headings of rows and columns. But the search must be performed separately by the range of the row or column. That is, only one of the criteria will be used. Therefore, you can`t apply the INDEX function here, but you need a special formula.



Finding values in the Excel table

To solve this problem, let us illustrate the example in the schematic table that corresponds to the conditions are described above.

The sheet with the table to search for values vertically and horizontally:

values vertically and horizontally.

Above this table we can see the row with results. In the cell B1 we introduce the criterion for the search query, that is, the column header or the ROW name. And in the cell D1, to a search formula should return to the result of the calculation of the corresponding value. Then the second formula will work in the cell F1. She will already use the values of the cells B1 and D1 as the criteria for searching of the corresponding month.



Search of the value in the Excel ROW

Now we are learning, in what maximum volume and in what month has been the maximum sale of the Product 4.

To search by columns:

  1. In the cell B1 you need to enter the value of the Product 4 — the name of the row, that will act as the criterion.
  2. In the cell D1 you need to enter the following:
  3. To confirm after entering the formula, you need to press the CTRL + SHIFT + Enter hotkey combination, because she must be executed in the array. If everything is done correctly, the curly braces will appear in the formula ROW.
  4. maximum volume.

  5. In the cell F1 you need to enter the second:
  6. For confirmation, to press the key combination CTRL + SHIFT + Enter again.

found.

So we have found, in what month and what was the largest sale of the Product 4 for two quarters.

The principle of the formula for finding the value in the Excel ROW:

In the first argument of the VLOOKUP function (Vertical Look Up), indicates to the reference to the cell, where the search criterion is located. In the second argument indicates to the range of the cells for viewing during in the process of searching.

In the third argument of the VLOOKUP function should be indicated the number of the column from which you should to take the value against of the row named the Item 4. But since we do not previously know this number, we use the COLUMN function for creating the array of column numbers for the range B4:G15.

This allows the VLOOKUP function to collect the whole array of values. As a result, all relevant values are stored in memory for each column in the row Product 4 (namely: 360; 958; 201; 605; 462; 832). After that, the MAX function will only take the maximum number from this array and return it as the value for the cell D1, as the result of calculating.

As you can see, the construction of the formula is simple and concise. On its basis, it is possible in a similar way to find other indicators for a certain product. For example, the minimum or an average value of sales volume you need to find using for this purpose MIN or AVERAGE functions. Nothing hinders you from applying this skeleton of the formula to apply with more complex functions for implementation the most comfortable analysis of the sales report.

How can I get to the column headings from a single cell value?

For example, how effectively we displayed the month with the maximum sale, using of the second. It’s not difficult to notice that in the second formula we used the skeleton of the first formula without the MAX function. The main structure of the function is: VLOOKUP. We replaced the MAX on the MATCH, which in the first argument uses the value obtained by the previous formula. It acts as the criterion for searching for the month now.

And as a result, the MATCH function returns the column number 2, where the maximum value of the sales volume for the product is located for the product 4. After that, the INDEX function is included in the work. This function returns the value by the number of terms and column from the range specified in its arguments. Because the we have the number of column 2, and the row number in the range where the names of months are stored in any cases will be the value 1. Then we have the INDEX function to get the corresponding value from the range of B4:G4 — February (the second month).

Search the value in the Excel column

The second version of the task will be searching in the table with using the month name as the criterion. In such cases, we have to change the skeleton of our formula: the VLOOKUP function is replaced by the HLOOKUP (Horizontal Look Up) one, and the COLUMN function is replaced by the row one.

This will allow us to know what volume and what of the product the maximum sale was in a certain month.

To find what kind of the product had the maximum sales in a certain month, you should:

  1. In the cell B2 to enter the name of the month June — this value will be used as the search criterion.
  2. In the cell D2, you should to enter the formula:
  3. To confirm after entering the formula you need to press the combination of keys CTRL + SHIFT + Enter, as this formula will be executed in the array. And the curly braces will appear in the function ROW.
  4. Formula.

  5. In the cell F1, you need to enter the second:
  6. You need to click CTRL + SHIFT + Enter for confirmation again.

Exaple.

The principle of the formula for finding the value in the Excel column

In the first argument of the HLOOKUP function, we indicate to the reference by the cell with the criterion for the search. In the second argument specifies the reference to the table argument being scanned. The third argument is generated by the ROW function, what creates in the array of ROW numbers of 10 elements in memory. So there are 10 rows in the table section.

Further the HLOOKUP function, alternately using to each number of the row, creates the array of corresponding sales values from the table for the certain month (June). Further, the MAX function is left only to select the maximum value from this array.

Then just a little modifying to the first formula by using the INDEX and MATCH functions, we created the second function to display the names of the table rows according to the cell value. The names of the corresponding rows (products) we output in F2.

ATTENTION! When using the formula skeleton for other tasks, you need always to pay attention to the second and the third argument of the search HLOOKUP function. The number of covered rows in the range is specified in the argument, must match with the number of rows in the table. And also the numbering should begin with the second ROW!

Download example search values in the columns and rows

Read also: The searching of the value in a range Excel table in columns and rows

Indeed, the content of the range generally we don’t care — we just need the row counter. That is, you need to change the arguments to: ROW(B2:B11) or ROW(C2:C11) — this does not affect in the quality of the formula. The main thing is that — there are 10 rows in these ranges, as well as in the table. And the numbering starts from the second row!

20 Step by Step Examples to Search and Find with MacrosIn this Excel VBA Tutorial, you learn how to search and find different items/information with macros.

This VBA Find Tutorial is accompanied by an Excel workbook containing the data and macros I use in the examples below. You can get free access to this example workbook by clicking the button below.

Get immediate free access to the Excel VBA Find workbook example

Use the following Table of Contents to navigate to the Section you’re interested in.

Related Excel VBA and Macro Training Materials

The following VBA and Macro training materials may help you better understand and implement the contents below:

  • Tutorials about general VBA constructs and structures:
    • Tutorials for Beginners:
      • Macros.
      • VBA.
    • Enable and disable macros.
    • The Visual Basic Editor (VBE).
    • Procedures:
      • Sub procedures.
      • Function procedures.
    • Work with:
      • Objects.
      • Properties.
      • Methods.
      • Variables.
      • Data types.
      • R1C1-style references.
      • Worksheet functions.
      • Loops.
      • Arrays.
    • Refer to:
      • Sheets and worksheets.
      • Cell ranges.
  • Tutorials with practical VBA applications and macro examples:
    • Find the last row or last column.
    • Set or get a cell’s or cell range’s value.
    • Check if a cell is empty.
    • Use the VLookup function.
  • The comprehensive and actionable Books at The Power Spreadsheets Library:
    • Excel Macros for Beginners Book Series.
    • VBA Fundamentals Book Series.

#1. Excel VBA Find (Cell with) Value in Cell Range

VBA Code to Find (Cell with) Value in Cell Range

To find a cell with a numeric value in a cell range, use the following structure/template in the applicable statement:

CellRangeObject.Find(What:=SearchedValue, After:=SingleCellRangeObject, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=XlSearchOrderConstant, SearchDirection:=XlSearchDirectionConstant)

The following Sections describe the main elements in this structure.

CellRangeObject

A Range object representing the cell range you search in.

Find

The Range.Find method:

  • Finds specific information (the numeric value you search for) in a cell range (CellRangeObject).
  • Returns a Range object representing the first cell where the information is found.
What:=SearchedValue

The What parameter of the Range.Find method specifies the data to search for.

To find a cell with a numeric value in a cell range, set the What parameter to the numeric value you search for (SearchedValue).

After:=SingleCellRangeObject

The After parameter of the Range.Find method specifies the cell after which the search begins. This must be a single cell in the cell range you search in (CellRangeObject).

If you omit specifying the After parameter, the search begins after the first cell (in the upper left corner) of the cell range you search in (CellRangeObject).

To find a cell with a numeric value in a cell range, set the After parameter to a Range object representing the cell after which the search begins.

LookIn:=xlValues

The LookIn parameter of the Range.Find method:

  • Specifies the type of data to search in.
  • Can take any of the built-in constants/values from the XlFindLookIn enumeration.

To find a cell with a numeric value in a cell range, set the LookIn parameter to xlValues. xlValues refers to values.

LookAt:=xlWhole

The LookAt parameter of the Range.Find method:

  • Specifies against which of the following the data you are searching for is matched:
    • The entire/whole searched cell contents.
    • Any part of the searched cell contents.
  • Can take any of the built-in constants/values from the XlLookAt enumeration.

To find a cell with a numeric value in a cell range, set the LookAt parameter to xlWhole. xlWhole matches the data you are searching for against the entire/whole searched cell contents.

SearchOrder:=XlSearchOrderConstant

The SearchOrder parameter of the Range.Find method:

  • Specifies the order in which the applicable cell range (CellRangeObject) is searched:
    • By rows.
    • By columns.
  • Can take any of the built-in constants/values from the XlSearchOrder enumeration.

To find a cell with a numeric value in a cell range, set the SearchOrder parameter to either of the following, as applicable:

  • xlByRows (SearchOrder:=xlByRows): To search by rows.
  • xlByColumns (SearchOrder:=xlByColumns): To search by columns.
SearchDirection:=XlSearchDirectionConstant

The SearchDirection parameter of the Range.Find method:

  • Specifies the search direction:
    • Search for the previous match.
    • Search for the next match.
  • Can take any of the built-in constants/values from the XlSearchDirection enumeration.

To find a cell with a numeric value in a cell range, set the SearchDirection parameter to either of the following, as applicable:

  • xlNext (SearchDirection:=xlNext): To search for the next match.
  • xlPrevious (SearchDirection:=xlPrevious): To search for the previous match.

Macro Example to Find (Cell with) Value in Cell Range

The following macro (User-Defined Function) example does the following:

  1. Accepts two arguments:
    1. MyRange: The cell range you search in.
    2. MyValue: The numeric value you search for.
  2. Finds MyValue in MyRange.
  3. Returns a string containing the address (as an A1-style relative reference) of the first cell in the cell range (MyRange) where the numeric value (MyValue) is found.
Function FindValueInCellRange(MyRange As Range, MyValue As Variant) As String
    'Source: https://powerspreadsheets.com/
    'For further information: https://powerspreadsheets.com/excel-vba-find/
    
    'This UDF:
        '(1) Accepts 2 arguments: MyRange and MyValue
        '(2) Finds a value passed as argument (MyValue) in a cell range passed as argument (MyRange)
        '(3) Returns the address (as an A1-style relative reference) of the first cell in the cell range (MyRange) where the value (MyValue) is found
    
    With MyRange
        FindValueInCellRange = .Find(What:=MyValue, After:=.Cells(.Cells.Count), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext).Address(RowAbsolute:=False, ColumnAbsolute:=False)
    End With
    
End Function

Effects of Executing Macro Example to Find (Cell with) Value in Cell Range

The following image illustrates the effects of using the macro (User-Defined Function) example. In this example:

  • Columns A through H (cells A6 to H30) contain randomly generated values.
  • Cell J7 contains the searched value (41).
  • Cell K7 contains the worksheet formula that works with the macro (User-Defined Function) example. This worksheet formula returns the address (as an A1-style relative reference) of the first cell in the cell range (MyRange) where the numeric value (MyValue) is found. This is cell B11.
  • Cell L7 displays the worksheet formula used in cell K7 (=FindValueInCellRange(A6:H30,J7)).
    • The cell range where the search is carried out contains cells A6 to H30 (A6:H30).
    • The searched value is stored in cell J7 (J7).
Example: Find value in cell range with VBA macros

Get immediate free access to the Excel VBA Find workbook example

#2. Excel VBA Find (Cell with) Value in Table

VBA Code to Find (Cell with) Value in Table

To find a cell with a numeric value in an Excel Table, use the following structure/template in the applicable statement:

ListObjectObject.DataBodyRange.Find(What:=SearchedValue, After:=SingleCellRangeObject, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=XlSearchOrderConstant, SearchDirection:=XlSearchDirectionConstant)

The following Sections describe the main elements in this structure.

ListObjectObject

A ListObject object representing the Excel Table you search in.

DataBodyRange

The ListObject.DataBodyRange property returns a Range object representing the cell range containing an Excel Table’s values (excluding the headers).

Find

The Range.Find method:

  • Finds specific information (the numeric value you search for) in a cell range (containing the applicable Excel Table’s values).
  • Returns a Range object representing the first cell where the information is found.
What:=SearchedValue

The What parameter of the Range.Find method specifies the data to search for.

To find a cell with a numeric value in an Excel Table, set the What parameter to the numeric value you search for (SearchedValue).

After:=SingleCellRangeObject

The After parameter of the Range.Find method specifies the cell after which the search begins. This must be a single cell in the cell range you search in (containing the applicable Excel Table’s values).

If you omit specifying the After parameter, the search begins after the first cell (in the upper left corner) of the cell range you search in (containing the applicable Excel Table’s values).

To find a cell with a numeric value in an Excel Table, set the After parameter to a Range object representing the cell after which the search begins.

LookIn:=xlValues

The LookIn parameter of the Range.Find method:

  • Specifies the type of data to search in.
  • Can take any of the built-in constants/values from the XlFindLookIn enumeration.

To find a cell with a numeric value in an Excel Table, set the LookIn parameter to xlValues. xlValues refers to values.

LookAt:=xlWhole

The LookAt parameter of the Range.Find method:

  • Specifies against which of the following the data you are searching for is matched:
    • The entire/whole searched cell contents.
    • Any part of the searched cell contents.
  • Can take any of the built-in constants/values from the XlLookAt enumeration.

To find a cell with a numeric value in an Excel Table, set the LookAt parameter to xlWhole. xlWhole matches the data you are searching for against the entire/whole searched cell contents.

SearchOrder:=XlSearchOrderConstant

The SearchOrder parameter of the Range.Find method:

  • Specifies the order in which the applicable cell range (containing the applicable Excel Table’s values) is searched:
    • By rows.
    • By columns.
  • Can take any of the built-in constants/values from the XlSearchOrder enumeration.

To find a cell with a numeric value in an Excel Table, set the SearchOrder parameter to either of the following, as applicable:

  • xlByRows (SearchOrder:=xlByRows): To search by rows.
  • xlByColumns (SearchOrder:=xlByColumns): To search by columns.
SearchDirection:=XlSearchDirectionConstant

The SearchDirection parameter of the Range.Find method:

  • Specifies the search direction:
    • Search for the previous match.
    • Search for the next match.
  • Can take any of the built-in constants/values from the XlSearchDirection enumeration.

To find a cell with a numeric value in an Excel Table, set the SearchDirection parameter to either of the following, as applicable:

  • xlNext (SearchDirection:=xlNext): To search for the next match.
  • xlPrevious (SearchDirection:=xlPrevious): To search for the previous match.

Macro Example to Find (Cell with) Value in Table

The following macro (User-Defined Function) example does the following:

  1. Accepts 3 arguments:
    1. MyWorksheetName: The name of the worksheet where the Excel Table you search in is stored.
    2. MyValue: The numeric value you search for.
    3. MyTableIndex: The index number of the Excel Table (stored in the worksheet named MyWorksheetName) you search in. MyTableIndex is an optional argument with a default value of 1.
  2. Finds MyValue in the applicable Excel Table’s values (excluding the headers).
  3. Returns a string containing the address (as an A1-style relative reference) of the first cell in the applicable Excel Table where the numeric value (MyValue) is found.
Function FindValueInTable(MyWorksheetName As String, MyValue As Variant, Optional MyTableIndex As Long = 1) As String
    'Source: https://powerspreadsheets.com/
    'For further information: https://powerspreadsheets.com/excel-vba-find/
    
    'This UDF:
        '(1) Accepts 3 arguments: MyWorksheetName, MyValue and MyTableIndex
        '(2) Finds a value passed as argument (MyValue) in an Excel Table stored in a worksheet whose name is passed as argument (MyWorksheetName). The index number of the Excel Table is either:
            '(1) Passed as an argument (MyTableIndex); or
            '(2) Assumed to be 1 (if MyTableIndex is omitted)
        '(3) Returns the address (as an A1-style relative reference) of the first cell in the Excel Table (stored in the MyWorksheetName worksheet and whose index is MyTableIndex) where the value (MyValue) is found
    
    With ThisWorkbook.Worksheets(MyWorksheetName).ListObjects(MyTableIndex).DataBodyRange
        FindValueInTable = .Find(What:=MyValue, After:=.Cells(.Cells.Count), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext).Address(RowAbsolute:=False, ColumnAbsolute:=False)
    End With
    
End Function

Effects of Executing Macro Example to Find (Cell with) Value in Table

The following image illustrates the effects of using the macro (User-Defined Function) example. In this example:

  • Columns A through H (cells A6 to H31) contain an Excel Table with randomly generated values.
  • Cell J7 contains the searched value (41).
  • Cell K7 contains the worksheet formula that works with the macro (User-Defined Function) example. This worksheet formula returns the address (as an A1-style relative reference) of the first cell in the Excel Table where the numeric value (MyValue) is found. This is cell B12.
  • Cell L7 displays the worksheet formula used in cell K7 (=FindValueInTable(“Find Value in Table”,J7)).
    • The name of the worksheet where the Excel Table is stored is “Find Value in Table” (“Find Value in Table”).
    • The searched value is stored in cell J7 (J7).
    • The index number of the Excel Table is 1 (by default).
Example: Find value in table with VBA macros

Get immediate free access to the Excel VBA Find workbook example

#3. Excel VBA Find (Cell with) Value in Column

VBA Code to Find (Cell with) Value in Column

To find a cell with a numeric value in a column, use the following structure/template in the applicable statement:

RangeObjectColumn.Find(What:=SearchedValue, After:=SingleCellRangeObject, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=XlSearchDirectionConstant)

The following Sections describe the main elements in this structure.

RangeObjectColumn

A Range object representing the column you search in.

Find

The Range.Find method:

  • Finds specific information (the numeric value you search for) in a cell range (RangeObjectColumn).
  • Returns a Range object representing the first cell where the information is found.
What:=SearchedValue

The What parameter of the Range.Find method specifies the data to search for.

To find a cell with a numeric value in a column, set the What parameter to the numeric value you search for (SearchedValue).

After:=SingleCellRangeObject

The After parameter of the Range.Find method specifies the cell after which the search begins. This must be a single cell in the column you search in (RangeObjectColumn).

If you omit specifying the After parameter, the search begins after the first cell of the column you search in (RangeObjectColumn).

To find a cell with a numeric value in a column, set the After parameter to a Range object representing the cell after which the search begins.

LookIn:=xlValues

The LookIn parameter of the Range.Find method:

  • Specifies the type of data to search in.
  • Can take any of the built-in constants/values from the XlFindLookIn enumeration.

To find a cell with a numeric value in a column, set the LookIn parameter to xlValues. xlValues refers to values.

LookAt:=xlWhole

The LookAt parameter of the Range.Find method:

  • Specifies against which of the following the data you are searching for is matched:
    • The entire/whole searched cell contents.
    • Any part of the searched cell contents.
  • Can take any of the built-in constants/values from the XlLookAt enumeration.

To find a cell with a numeric value in a column, set the LookAt parameter to xlWhole. xlWhole matches the data you are searching for against the entire/whole searched cell contents.

SearchOrder:=xlByRows

The SearchOrder parameter of the Range.Find method:

  • Specifies the order in which the applicable column (RangeObjectColumn) is searched:
    • By rows.
    • By columns.
  • Can take any of the built-in constants/values from the XlSearchOrder enumeration.

To find a cell with a numeric value in a column, set the SearchOrder parameter to xlByRows. xlByRows results in the Range.Find method searching by rows.

SearchDirection:=XlSearchDirectionConstant

The SearchDirection parameter of the Range.Find method:

  • Specifies the search direction:
    • Search for the previous match.
    • Search for the next match.
  • Can take any of the built-in constants/values from the XlSearchDirection enumeration.

To find a cell with a numeric value in a column, set the SearchDirection parameter to either of the following, as applicable:

  • xlNext (SearchDirection:=xlNext): To search for the next match.
  • xlPrevious (SearchDirection:=xlPrevious): To search for the previous match.

Macro Example to Find (Cell with) Value in Column

The following macro (User-Defined Function) example does the following:

  1. Accepts two arguments:
    1. MyColumn: The column you search in.
    2. MyValue: The numeric value you search for.
  2. Finds MyValue in MyColumn.
  3. Returns a string containing the address (as an A1-style relative reference) of the first cell in the column (MyColumn) where the numeric value (MyValue) is found.
Function FindValueInColumn(MyColumn As Range, MyValue As Variant) As String
    'Source: https://powerspreadsheets.com/
    'For further information: https://powerspreadsheets.com/excel-vba-find/
    
    'This UDF:
        '(1) Accepts 2 arguments: MyColumn and MyValue
        '(2) Finds a value passed as argument (MyValue) in a column passed as argument (MyColumn)
        '(3) Returns the address (as an A1-style relative reference) of the first cell in the column (MyColumn) where the value (MyValue) is found
    
    With MyColumn
        FindValueInColumn = .Find(What:=MyValue, After:=.Cells(.Cells.Count), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext).Address(RowAbsolute:=False, ColumnAbsolute:=False)
    End With
    
End Function

Effects of Executing Macro Example to Find (Cell with) Value in Column

The following image illustrates the effects of using the macro (User-Defined Function) example. In this example:

  • Column A (cells A6 to A31) contains randomly generated values.
  • Cell C7 contains the searched value (90).
  • Cell D7 contains the worksheet formula that works with the macro (User-Defined Function) example. This worksheet formula returns the address (as an A1-style relative reference) of the first cell in the column (MyColumn) where the numeric value (MyValue) is found. This is cell A13.
  • Cell E7 displays the worksheet formula used in cell D7 (=FindValueInColumn(A:A,C7)).
    • The column where the search is carried out is column A (A:A).
    • The searched value is stored in cell C7 (C7).

Get immediate free access to the Excel VBA Find workbook example

#4. Excel VBA Find (Cell with) Value in Table Column

VBA Code to Find (Cell with) Value in Table Column

To find a cell with a numeric value in an Excel Table column, use the following structure/template in the applicable statement:

ListColumnObject.DataBodyRange.Find(What:=SearchedValue, After:=SingleCellRangeObject, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=XlSearchOrderConstant, SearchDirection:=XlSearchDirectionConstant)

The following Sections describe the main elements in this structure.

ListColumnObject

A ListColumn object representing the Excel Table column you search in.

DataBodyRange

The ListColumn.DataBodyRange property returns a Range object representing the cell range containing an Excel Table column’s values (excluding the header).

Find

The Range.Find method:

  • Finds specific information (the numeric value you search for) in a cell range (containing the applicable Excel Table column’s values).
  • Returns a Range object representing the first cell where the information is found.
What:=SearchedValue

The What parameter of the Range.Find method specifies the data to search for.

To find a cell with a numeric value in an Excel Table column, set the What parameter to the numeric value you search for (SearchedValue).

After:=SingleCellRangeObject

The After parameter of the Range.Find method specifies the cell after which the search begins. This must be a single cell in the cell range you search in (containing the applicable Excel Table column’s values).

If you omit specifying the After parameter, the search begins after the first cell of the cell range you search in (containing the applicable Excel Table column’s values).

To find a cell with a numeric value in an Excel Table column, set the After parameter to a Range object representing the cell after which the search begins.

LookIn:=xlValues

The LookIn parameter of the Range.Find method:

  • Specifies the type of data to search in.
  • Can take any of the built-in constants/values from the XlFindLookIn enumeration.

To find a cell with a numeric value in an Excel Table column, set the LookIn parameter to xlValues. xlValues refers to values.

LookAt:=xlWhole

The LookAt parameter of the Range.Find method:

  • Specifies against which of the following the data you are searching for is matched:
    • The entire/whole searched cell contents.
    • Any part of the searched cell contents.
  • Can take any of the built-in constants/values from the XlLookAt enumeration.

To find a cell with a numeric value in an Excel Table column, set the LookAt parameter to xlWhole. xlWhole matches the data you are searching for against the entire/whole searched cell contents.

SearchOrder:=XlSearchOrderConstant

The SearchOrder parameter of the Range.Find method:

  • Specifies the order in which the applicable cell range (containing the applicable Excel Table column’s values) is searched:
    • By rows.
    • By columns.
  • Can take any of the built-in constants/values from the XlSearchOrder enumeration.

To find a cell with a numeric value in an Excel Table column, set the SearchOrder parameter to either of the following, as applicable:

  • xlByRows (SearchOrder:=xlByRows): To search by rows.
  • xlByColumns (SearchOrder:=xlByColumns): To search by columns.
SearchDirection:=XlSearchDirectionConstant

The SearchDirection parameter of the Range.Find method:

  • Specifies the search direction:
    • Search for the previous match.
    • Search for the next match.
  • Can take any of the built-in constants/values from the XlSearchDirection enumeration.

To find a cell with a numeric value in an Excel Table column, set the SearchDirection parameter to either of the following, as applicable:

  • xlNext (SearchDirection:=xlNext): To search for the next match.
  • xlPrevious (SearchDirection:=xlPrevious): To search for the previous match.

Macro Example to Find (Cell with) Value in Table Column

The following macro (User-Defined Function) example does the following:

  1. Accepts 4 arguments:
    1. MyWorksheetName: The name of the worksheet where the Excel Table (containing the column you search in) is stored.
    2. MyColumnIndex: The index/column number of the column you search in (in the applicable Excel Table).
    3. MyValue: The numeric value you search for.
    4. MyTableIndex: The index number of the Excel Table (stored in the worksheet named MyWorksheetName) containing the column you search in. MyTableIndex is an optional argument with a default value of 1.
  2. Finds MyValue in the applicable Excel Table column’s values (excluding the header).
  3. Returns a string containing the address (as an A1-style relative reference) of the first cell in the applicable Excel Table column where the numeric value (MyValue) is found.
Function FindValueInTableColumn(MyWorksheetName As String, MyColumnIndex As Long, MyValue As Variant, Optional MyTableIndex As Long = 1) As String
    'Source: https://powerspreadsheets.com/
    'For further information: https://powerspreadsheets.com/excel-vba-find/
    
    'This UDF:
        '(1) Accepts 4 arguments: MyWorksheetName, MyColumnIndex, MyValue and MyTableIndex
        '(2) Finds a value passed as argument (MyValue) in an Excel Table column, where:
            '(1) The table column's index is passed as argument (MyColumnIndex); and
            '(2) The Excel Table is stored in a worksheet whose name is passed as argument (MyWorksheetName). The index number of the Excel Table is either:
                '(1) Passed as an argument (MyTableIndex); or
                '(2) Assumed to be 1 (if MyTableIndex is omitted)
        '(3) Returns the address (as an A1-style relative reference) of the first cell in the applicable Excel Table column where the value (MyValue) is found
    
    With ThisWorkbook.Worksheets(MyWorksheetName).ListObjects(MyTableIndex).ListColumns(MyColumnIndex).DataBodyRange
        FindValueInTableColumn = .Find(What:=MyValue, After:=.Cells(.Cells.Count), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext).Address(RowAbsolute:=False, ColumnAbsolute:=False)
    End With
    
End Function

Effects of Executing Macro Example to Find (Cell with) Value in Table Column

The following image illustrates the effects of using the macro (User-Defined Function) example. In this example:

  • Columns A through H (cells A6 to H31) contain an Excel Table with randomly generated values. Cells in the first row (row 7) contain the searched value (90), except for the cell in the searched column (Column 3).
  • Cell J7 contains the searched value (90).
  • Cell K7 contains the worksheet formula that works with the macro (User-Defined Function) example. This worksheet formula returns the address (as an A1-style relative reference) of the first cell in the applicable Excel Table column (Column 3) where the numeric value (MyValue) is found. This is cell C14.
  • Cell L7 displays the worksheet formula used in cell K7 (=FindValueInTableColumn(“Find Value in Table Column”,3, J7)).
    • The name of the worksheet where the Excel Table is stored is “Find Value in Table Column” (“Find Value in Table Column”).
    • The index number of the Excel Table column is 3 (3).
    • The searched value is stored in cell J7 (J7).
    • The index number of the Excel Table is 1 (by default).
Example: Find value in table column with VBA macros

Get immediate free access to the Excel VBA Find workbook example

#5. Excel VBA Find Minimum Value in Cell Range

VBA Code to Find Minimum Value in Cell Range

To find the minimum value in a cell range, use the following structure/template in the applicable statement:

Application.Min(CellRangeObject)

The following Sections describe the main elements in this structure.

Application.Min

The WorksheetFunction.Min method returns the minimum value in a set of values.

CellRangeObject

The WorksheetFunction.Min method accepts up to thirty parameters (Arg1 to Arg30). These are the values for which you want to find the minimum value.

To find the minimum value in a cell range, pass a Range object (CellRangeObject) representing the cell range whose minimum value you want to find as method parameter.

Macro Example to Find Minimum Value in Cell Range

The following macro (User-Defined Function) example does the following:

  1. Accepts 1 argument (MyRange): The cell range whose minimum value you search for.
  2. Finds and returns the minimum value in the cell range (MyRange).
Function FindMinimumValueInCellRange(MyRange As Range) As Double
    'Source: https://powerspreadsheets.com/
    'For further information: https://powerspreadsheets.com/excel-vba-find/
    
    'This UDF:
        '(1) Accepts 1 argument: MyRange
        '(2) Finds the minimum value in the cell range passed as argument (MyRange)
    
    FindMinimumValueInCellRange = Application.Min(MyRange)

End Function

Effects of Executing Macro Example to Find Minimum Value in Cell Range

The following image illustrates the effects of using the macro (User-Defined Function) example. In this example:

  • Columns A through H (cells A6 to H30) contain randomly generated values.
  • Cell J7 contains the worksheet formula that works with the macro (User-Defined Function) example. This worksheet formula returns the minimum value in the cell range (MyRange). This is the number 1.
  • Cell K7 displays the worksheet formula used in cell J7 (=FindMinimumValueInCellRange(A6:H30)). The cell range where the search is carried out contains cells A6 to H30 (A6:H30).
Example: Find minimum value in cell range with VBA macros

Get immediate free access to the Excel VBA Find workbook example

#6. Excel VBA Find (Cell with) String (or Text) in Cell Range

VBA Code to Find (Cell with) String (or Text) in Cell Range

To find a cell with a string (or text) in a cell range, use the following structure/template in the applicable statement:

CellRangeObject.Find(What:=SearchedString, After:=SingleCellRangeObject, LookIn:=XlFindLookInConstant, LookAt:=XlLookAtConstant, SearchOrder:=XlSearchOrderConstant, SearchDirection:=XlSearchDirectionConstant, MatchCase:=BooleanValue)

The following Sections describe the main elements in this structure.

CellRangeObject

A Range object representing the cell range you search in.

Find

The Range.Find method:

  • Finds specific information (the string or text you search for) in a cell range (CellRangeObject).
  • Returns a Range object representing the first cell where the information is found.
What:=SearchedString

The What parameter of the Range.Find method specifies the data to search for.

To find a cell with a string (or text) in a cell range, set the What parameter to the string (or text) you search for (SearchedString).

After:=SingleCellRangeObject

The After parameter of the Range.Find method specifies the cell after which the search begins. This must be a single cell in the cell range you search in (CellRangeObject).

If you omit specifying the After parameter, the search begins after the first cell (in the upper left corner) of the cell range you search in (CellRangeObject).

To find a cell with a string (or text) in a cell range, set the After parameter to a Range object representing the cell after which the search begins.

LookIn:=XlFindLookInConstant

The LookIn parameter of the Range.Find method:

  • Specifies the type of data to search in.
  • Can take any of the built-in constants/values from the XlFindLookIn enumeration.

To find a cell with a string (or text) in a cell range, set the LookIn parameter to either of the following, as applicable:

  • xlFormulas (LookIn:=xlFormulas): To search in the applicable cell range’s formulas.
  • xlValues (LookIn:=xlValues): To search in the applicable cell range’s values.
LookAt:=XlLookAtConstant

The LookAt parameter of the Range.Find method:

  • Specifies against which of the following the data you are searching for is matched:
    • The entire/whole searched cell contents.
    • Any part of the searched cell contents.
  • Can take any of the built-in constants/values from the XlLookAt enumeration.

To find a cell with a string (or text) in a cell range, set the LookAt parameter to either of the following, as applicable:

  • xlWhole (LookAt:=xlWhole): To match against the entire/whole searched cell contents.
  • xlPart (LookAt:=xlPart): To match against any part of the searched cell contents.
SearchOrder:=XlSearchOrderConstant

The SearchOrder parameter of the Range.Find method:

  • Specifies the order in which the applicable cell range (CellRangeObject) is searched:
    • By rows.
    • By columns.
  • Can take any of the built-in constants/values from the XlSearchOrder enumeration.

To find a cell with a string (or text) in a cell range, set the SearchOrder parameter to either of the following, as applicable:

  • xlByRows (SearchOrder:=xlByRows): To search by rows.
  • xlByColumns (SearchOrder:=xlByColumns): To search by columns.
SearchDirection:=XlSearchDirectionConstant

The SearchDirection parameter of the Range.Find method:

  • Specifies the search direction:
    • Search for the previous match.
    • Search for the next match.
  • Can take any of the built-in constants/values from the XlSearchDirection enumeration.

To find a cell with a string (or text) in a cell range, set the SearchDirection parameter to either of the following, as applicable:

  • xlNext (SearchDirection:=xlNext): To search for the next match.
  • xlPrevious (SearchDirection:=xlPrevious): To search for the previous match.
MatchCase:=BooleanValue

The MatchCase parameter of the Range.Find method specifies whether the search is:

  • Case-sensitive; or
  • Case-insensitive.

The default value of the MatchCase parameter is False.

To find a cell with a string (or text) in a cell range, set the MatchCase parameter to either of the following, as applicable:

  • True (MatchCase:=True): To carry out a case-sensitive search.
  • False (MatchCase:=False): To carry out a case-insensitive search.

Macro Example to Find (Cell with) String (or Text) in Cell Range

The following macro (User-Defined Function) example does the following:

  1. Accepts two arguments:
    1. MyRange: The cell range you search in.
    2. MyString: The string (or text) you search for.
  2. Finds MyString in MyRange. The search is case-insensitive.
  3. Returns a string containing the address (as an A1-style relative reference) of the first cell in the cell range (MyRange) where the string or text (MyString) is found.
Function FindStringInCellRange(MyRange As Range, MyString As Variant) As String
    'Source: https://powerspreadsheets.com/
    'For further information: https://powerspreadsheets.com/excel-vba-find/
    
    'This UDF:
        '(1) Accepts 2 arguments: MyRange and MyString
        '(2) Finds a string passed as argument (MyString) in a cell range passed as argument (MyRange). The search is case-insensitive
        '(3) Returns the address (as an A1-style relative reference) of the first cell in the cell range (MyRange) where the string (MyString) is found
    
    With MyRange
        FindStringInCellRange = .Find(What:=MyString, After:=.Cells(.Cells.Count), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Address(RowAbsolute:=False, ColumnAbsolute:=False)
    End With
    
End Function

Effects of Executing Macro Example to Find (Cell with) String (or Text) in Cell Range

The following image illustrates the effects of using the macro (User-Defined Function) example. In this example:

  • Columns A through H (cells A6 to H30) contain randomly generated words.
  • Cell J7 contains the searched string or text (Excel).
  • Cell K7 contains the worksheet formula that works with the macro (User-Defined Function) example. This worksheet formula returns the address (as an A1-style relative reference) of the first cell in the cell range (MyRange) where the string or text (MyString) is found. This is cell F20.
  • Cell L7 displays the worksheet formula used in cell K7 (=FindStringInCellRange(A6:H30,J7)).
    • The cell range where the search is carried out contains cells A6 to H30 (A6:H30).
    • The searched string or text is stored in cell J7 (J7).
Example: Find String in cell range with VBA macros

Get immediate free access to the Excel VBA Find workbook example

#7. Excel VBA Find (Cell with) String (or Text) in Column

VBA Code to Find (Cell with) String (or Text) in Column

To find a cell with a string (or text) in a column, use the following structure/template in the applicable statement:

RangeObjectColumn.Find(What:=SearchedString, After:=SingleCellRangeObject, LookIn:=XlFindLookInConstant, LookAt:=XlLookAtConstant, SearchOrder:=xlByRows, SearchDirection:=XlSearchDirectionConstant, MatchCase:=BooleanValue)

The following Sections describe the main elements in this structure.

RangeObjectColumn

A Range object representing the column you search in.

Find

The Range.Find method:

  • Finds specific information (the string or text you search for) in a cell range (RangeObjectColumn).
  • Returns a Range object representing the first cell where the information is found.
What:=SearchedString

The What parameter of the Range.Find method specifies the data to search for.

To find a cell with a string (or text) in a column, set the What parameter to the string (or text) you search for (SearchedString).

After:=SingleCellRangeObject

The After parameter of the Range.Find method specifies the cell after which the search begins. This must be a single cell in the column you search in (RangeObjectColumn).

If you omit specifying the After parameter, the search begins after the first cell of the column you search in (RangeObjectColumn).

To find a cell with a string (or text) in a column, set the After parameter to a Range object representing the cell after which the search begins.

LookIn:=XlFindLookInConstant

The LookIn parameter of the Range.Find method:

  • Specifies the type of data to search in.
  • Can take any of the built-in constants/values from the XlFindLookIn enumeration.

To find a cell with a string (or text) in a column, set the LookIn parameter to either of the following, as applicable:

  • xlFormulas (LookIn:=xlFormulas): To search in the applicable column’s formulas.
  • xlValues (LookIn:=xlValues): To search in the applicable column’s values.
LookAt:=XlLookAtConstant

The LookAt parameter of the Range.Find method:

  • Specifies against which of the following the data you are searching for is matched:
    • The entire/whole searched cell contents.
    • Any part of the searched cell contents.
  • Can take any of the built-in constants/values from the XlLookAt enumeration.

To find a cell with a string (or text) in a column, set the LookAt parameter to either of the following, as applicable:

  • xlWhole (LookAt:=xlWhole): To match against the entire/whole searched cell contents.
  • xlPart (LookAt:=xlPart): To match against any part of the searched cell contents.
SearchOrder:=xlByRows

The SearchOrder parameter of the Range.Find method:

  • Specifies the order in which the applicable column (RangeObjectColumn) is searched:
    • By rows.
    • By columns.
  • Can take any of the built-in constants/values from the XlSearchOrder enumeration.

To find a cell with a string (or text) in a column, set the SearchOrder parameter to xlByRows. xlByRows results in the Range.Find method searching by rows.

SearchDirection:=XlSearchDirectionConstant

The SearchDirection parameter of the Range.Find method:

  • Specifies the search direction:
    • Search for the previous match.
    • Search for the next match.
  • Can take any of the built-in constants/values from the XlSearchDirection enumeration.

To find a cell with a string (or text) in a column, set the SearchDirection parameter to either of the following, as applicable:

  • xlNext (SearchDirection:=xlNext): To search for the next match.
  • xlPrevious (SearchDirection:=xlPrevious): To search for the previous match.
MatchCase:=BooleanValue

The MatchCase parameter of the Range.Find method specifies whether the search is:

  • Case-sensitive; or
  • Case-insensitive.

The default value of the MatchCase parameter is False.

To find a cell with a string (or text) in a column, set the MatchCase parameter to either of the following, as applicable:

  • True (MatchCase:=True): To carry out a case-sensitive search.
  • False (MatchCase:=False): To carry out a case-insensitive search.

Macro Example to Find (Cell with) String (or Text) in Column

The following macro (User-Defined Function) example does the following:

  1. Accepts two arguments:
    1. MyColumn: The column you search in.
    2. MyString: The string (or text) you search for.
  2. Finds MyString in MyColumn. The search is case-insensitive.
  3. Returns a string containing the address (as an A1-style relative reference) of the first cell in the column (MyColumn) where the string or text (MyString) is found.
Function FindStringInColumn(MyColumn As Range, MyString As Variant) As String
    'Source: https://powerspreadsheets.com/
    'For further information: https://powerspreadsheets.com/excel-vba-find/
    
    'This UDF:
        '(1) Accepts 2 arguments: MyColumn and MyString
        '(2) Finds a string passed as argument (MyString) in a column passed as argument (MyColumn). The search is case-insensitive
        '(3) Returns the address (as an A1-style relative reference) of the first cell in the column (MyColumn) where the string (MyString) is found
    
    With MyColumn
        FindStringInColumn = .Find(What:=MyString, After:=.Cells(.Cells.Count), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Address(RowAbsolute:=False, ColumnAbsolute:=False)
    End With
    
End Function

Effects of Executing Macro Example to Find (Cell with) String (or Text) in Column

The following image illustrates the effects of using the macro (User-Defined Function) example. In this example:

  • Column A (cells A6 to A30) contains randomly generated words.
  • Cell C7 contains the searched string or text (Excel).
  • Cell D7 contains the worksheet formula that works with the macro (User-Defined Function) example. This worksheet formula returns the address (as an A1-style relative reference) of the first cell in the column (MyColumn) where the string or text (MyString) is found. This is cell A21.
  • Cell E7 displays the worksheet formula used in cell D7 (=FindStringInColumn(A:A,C7)).
    • The column where the search is carried out is column A (A:A).
    • The searched string or text is stored in cell C7 (C7).
Example: Find string in column with VBA macros

Get immediate free access to the Excel VBA Find workbook example

#8. Excel VBA Find String (or Text) in Cell

VBA Code to Find String (or Text) in Cell

To find a string (or text) in a cell, use the following structure/template in the applicable statement:

InStr(StartingPosition, SearchedCell.Value, SearchedString, VbCompareMethodConstant)

The following Sections describe the main elements in this structure.

InStr

The InStr function returns a number. This number specifies the position of the first occurrence of a string or text (SearchedString) in another string (the string stored in SearchedCell).

StartingPosition

The Start argument of the InStr function is:

  • An optional argument.
  • A numeric expression specifying the starting position for the string (or text) search.

If you omit specifying the Start argument, the search begins at the first character of the searched string (the string stored in SearchedCell).

To find a string (or text) in a cell, set the Start argument to the position (in the string stored in SearchedCell) where the string (or text) search starts.

SearchedCell.Value

The String1 argument of the InStr function represents the string expression the InStr function searches in.

To find a string (or text) in a cell, set the String1 argument to the value/string stored in the searched cell. For these purposes:

  • “SearchedCell” is a Range object representing the searched cell.
  • “Value” refers to the Range.Value property. The Range.Value property returns the value/string stored in the searched cell (SearchedCell).
SearchedString

The String2 argument of the InStr function represents the string expression (or text) the InStr function searches for.

To find a string (or text) in a cell, set the String2 argument to the string (or text) you search for.

VbCompareMethodConstant

The Compare argument of the InStr function:

  • Is an optional argument.
  • Specifies the type of string comparison carried out by the InStr function.
  • Can take any of the built-in constants/values from the vbCompareMethod enumeration.

If you omit specifying the Compare argument, the type of string comparison is determined by the Option Compare statement. The Option Compare statement declares the default string comparison method at a module level. The default string comparison method is binary (vbBinaryCompare).

To find a string (or text) in a cell, set the Compare argument to either of the following, as applicable:

  • vbBinaryCompare: Performs a binary comparison. vbBinaryCompare:
    • Results in a case-sensitive search.
    • May be (slightly) faster than vbTextCompare.
  • vbTextCompare: Performs a textual comparison. vbTextCompare:
    • Results in a case-insensitive search.
    • May be (slightly) slower than vbBinaryCompare.
    • Is more prone to errors/bugs than vbBinaryCompare.

Macro Example to Find String (or Text) in Cell

The following macro (User-Defined Function) example does the following:

  1. Accepts three arguments:
    1. MyCell: The cell you search in.
    2. MyString: The string (or text) you search for.
    3. MyStartingPosition: The starting position for the string (or text) search. MyStartingPosition is an optional argument with a default value of 1.
  2. Finds MyString in the value/string stored in MyCell.
  3. Returns the following:
    1. If MyString is not found in the value/string stored in MyCell, the string “String not found in cell”.
    2. If MyString is found in the value/string stored in MyCell, the position of the first occurrence of MyString in the value/string stored in MyCell.
Function FindStringInCell(MyCell As Range, MyString As Variant, Optional MyStartingPosition As Variant = 1) As Variant
    'Source: https://powerspreadsheets.com/
    'For further information: https://powerspreadsheets.com/excel-vba-find/
    
    'This UDF:
        '(1) Accepts three arguments: MyCell, MyString and MyStartingPosition (optional argument with a default value of 1)
        '(2) Finds a string passed as argument (MyString) in the value/string stored in a cell passed as argument (MyCell)
        '(3) Returns the following (as applicable):
            'If MyString is not found in the value/string stored in MyCell: The string "String not found in cell"
            'If MyString is found in the value/string stored in MyCell: The position of the first occurrence of MyString in the value/string stored in MyCell
    
    'Obtain position of first occurrence of MyString in the value/string stored in MyCell
    FindStringInCell = InStr(MyStartingPosition, MyCell.Value, MyString, vbBinaryCompare)
    
    'If MyString is not found in the value/string stored in MyCell, return the string "String not found in cell"
    If FindStringInCell = 0 Then FindStringInCell = "String not found in cell"

End Function

Effects of Executing Macro Example to Find String (or Text) in Cell

The following image illustrates the effects of using the macro (User-Defined Function) example. In this example:

  • Column A (cells A7 to A31) contains randomly generated words.
  • Column B (cells B7 to B31) contains a 2-character string (ar).
  • Column C (cells C7 to C31) contains worksheet formulas working with the macro (User-Defined Function) example. These worksheet formulas return either of the following (as applicable):
    • The string “String not found in cell”, if the string (or text) specified in the applicable cell of column B is not found in the applicable cell of column A.
    • The position of the first occurrence of the string (or text) specified in the applicable cell of column B in the applicable cell of column A, if the string (or text) specified in the applicable cell of column B is found in the applicable cell of column A.
  • Column D (cells D7 to D31) displays the worksheet formulas used in column C (=FindStringInCell(CellInColumnA,CellInColumnB)).
    • The cell where the search is carried out is in column A (CellInColumnA).
    • The searched string or text is stored in column B (CellInColumnB).
Example: Find string in cell with VBA macros

Get immediate free access to the Excel VBA Find workbook example

#9. Excel VBA Find String (or Text) in String

VBA Code to Find String (or Text) in String

To find a string (or text) in a string, use the following structure/template in the applicable statement:

InStr(StartingPosition, SearchedString, SearchedText, VbCompareMethodConstant)

The following Sections describe the main elements in this structure.

InStr

The InStr function returns a number. This number specifies the position of the first occurrence of a string or text (SearchedText) in another string (SearchedString).

StartingPosition

The Start argument of the InStr function is:

  • An optional argument.
  • A numeric expression specifying the starting position for the string (or text) search.

If you omit specifying the Start argument, the search begins at the first character of the searched string (SearchedString).

To find a string (or text) in a string, set the Start argument to the position (in SearchedString) where the string (or text) search starts.

SearchedString

The String1 argument of the InStr function represents the string expression the InStr function searches in.

To find a string (or text) in a string, set the String1 argument to the searched string.

SearchedText

The String2 argument of the InStr function represents the string expression (or text) the InStr function searches for.

To find a string (or text) in a string, set the String2 argument to the string (or text) you search for.

VbCompareMethodConstant

The Compare argument of the InStr function:

  • Is an optional argument.
  • Specifies the type of string comparison carried out by the InStr function.
  • Can take any of the built-in constants/values from the vbCompareMethod enumeration.

If you omit specifying the Compare argument, the type of string comparison is determined by the Option Compare statement. The Option Compare statement declares the default string comparison method at a module level. The default string comparison method is binary (vbBinaryCompare).

To find a string (or text) in a string, set the Compare argument to either of the following, as applicable:

  • vbBinaryCompare: Performs a binary comparison. vbBinaryCompare:
    • Results in a case-sensitive search.
    • May be (slightly) faster than vbTextCompare.
  • vbTextCompare: Performs a textual comparison. vbTextCompare:
    • Results in a case-insensitive search.
    • May be (slightly) slower than vbBinaryCompare.
    • Is more prone to errors/bugs than vbBinaryCompare.

Macro Example to Find String (or Text) in String

The following macro (User-Defined Function) example does the following:

  1. Accepts three arguments:
    1. MyString: The string you search in.
    2. MyText: The string (or text) you search for.
    3. MyStartingPosition: The starting position for the string (or text) search. MyStartingPosition is an optional argument with a default value of 1.
  2. Finds MyText in MyString.
  3. Returns the following:
    1. If MyText is not found in MyString, the string “Text not found in string”.
    2. If MyText is found in MyString, the position of the first occurrence of MyText in MyString.
Function FindTextInString(MyString As Variant, MyText As Variant, Optional MyStartingPosition As Variant = 1) As Variant
    'Source: https://powerspreadsheets.com/
    'For further information: https://powerspreadsheets.com/excel-vba-find/
    
    'This UDF:
        '(1) Accepts three arguments: MyString, MyText and MyStartingPosition (optional argument with a default value of 1)
        '(2) Finds text (a string) passed as argument (MyText) in a string passed as argument (MyString)
        '(3) Returns the following (as applicable):
            'If MyText is not found in MyString: The string "Text not found in string"
            'If MyText is found in MyString: The position of the first occurrence of MyText in MyString
    
    'Obtain position of first occurrence of MyText in MyString
    FindTextInString = InStr(MyStartingPosition, MyString, MyText, vbBinaryCompare)
    
    'If MyText is not found in MyString, return the string "Text not found in string"
    If FindTextInString = 0 Then FindTextInString = "Text not found in string"

End Function

Effects of Executing Macro Example to Find String (or Text) in String

The following image illustrates the effects of using the macro (User-Defined Function) example. In this example:

  • Column A (cells A7 to A31) contains randomly generated words.
  • Column B (cells B7 to B31) contains text (at).
  • Column C (cells C7 to C31) contains worksheet formulas working with the macro (User-Defined Function) example. These worksheet formulas return either of the following (as applicable):
    • The string “Text not found in string”, if the string (or text) specified in the applicable cell of column B is not found in the string specified in the applicable cell of column A.
    • The position of the first occurrence of the string (or text) specified in the applicable cell of column B in the string specified in the applicable cell of column A, if the string (or text) specified in the applicable cell of column B is found in the string specified in the applicable cell of column A.
  • Column D (cells D7 to D31) displays the worksheet formulas used in column C (=FindTextInString(CellInColumnA,CellInColumnB)).
    • The string where the search is carried out is stored in column A (CellInColumnA).
    • The searched string (or text) is stored in column B (CellInColumnB).
Example: Find text in string with VBA macros

Get immediate free access to the Excel VBA Find workbook example

#10. Excel VBA Find Character in String

VBA Code to Find Character in String

To find a character in a string, use the following structure/template in the applicable statement:

InStr(StartingPosition, SearchedString, SearchedCharacter, VbCompareMethodConstant)

The following Sections describe the main elements in this structure.

InStr

The InStr function returns a number. This number specifies the position of the first occurrence of a string or text (SearchedCharacter) in another string (SearchedString).

StartingPosition

The Start argument of the InStr function is:

  • An optional argument.
  • A numeric expression specifying the starting position for the character search.

If you omit specifying the Start argument, the search begins at the first character of the searched string (SearchedString).

To find a character in a string, set the Start argument to the position (in SearchedString) where the character search starts.

SearchedString

The String1 argument of the InStr function represents the string expression the InStr function searches in.

To find a character in a string, set the String1 argument to the searched string.

SearchedCharacter

The String2 argument of the InStr function represents the string expression (or text) the InStr function searches for.

To find a character in a string, set the String2 argument to the character you search for.

VbCompareMethodConstant

The Compare argument of the InStr function:

  • Is an optional argument.
  • Specifies the type of string comparison carried out by the InStr function.
  • Can take any of the built-in constants/values from the vbCompareMethod enumeration.

If you omit specifying the Compare argument, the type of string comparison is determined by the Option Compare statement. The Option Compare statement declares the default string comparison method at a module level. The default string comparison method is binary (vbBinaryCompare).

To find a character in a string, set the Compare argument to either of the following, as applicable:

  • vbBinaryCompare: Performs a binary comparison. vbBinaryCompare:
    • Results in a case-sensitive search.
    • May be (slightly) faster than vbTextCompare.
  • vbTextCompare: Performs a textual comparison. vbTextCompare:
    • Results in a case-insensitive search.
    • May be (slightly) slower than vbBinaryCompare.
    • Is more prone to errors/bugs than vbBinaryCompare.

Macro Example to Find Character in String

The following macro (User-Defined Function) example does the following:

  1. Accepts three arguments:
    1. MyString: The string you search in.
    2. MyCharacter: The character you search for.
    3. MyStartingPosition: The starting position for the character search. MyStartingPosition is an optional argument with a default value of 1.
  2. Finds MyCharacter in MyString.
  3. Returns the following:
    1. If MyCharacter is not found in MyString, the string “Character not found in string”.
    2. If MyCharacter is found in MyString, the position of the first occurrence of MyCharacter in MyString.
Function FindCharacterInString(MyString As Variant, MyCharacter As Variant, Optional MyStartingPosition As Variant = 1) As Variant
    'Source: https://powerspreadsheets.com/
    'For further information: https://powerspreadsheets.com/excel-vba-find/
    
    'This UDF:
        '(1) Accepts three arguments: MyString, MyCharacter and MyStartingPosition (optional argument with a default value of 1)
        '(2) Finds a character passed as argument (MyCharacter) in a string passed as argument (MyString)
        '(3) Returns the following (as applicable):
            'If MyCharacter is not found in MyString: The string "Character not found in string"
            'If MyCharacter is found in MyString: The position of the first occurrence of MyCharacter in MyString
    
    'Obtain position of first occurrence of MyCharacter in MyString
    FindCharacterInString = InStr(MyStartingPosition, MyString, MyCharacter, vbBinaryCompare)
    
    'If MyCharacter is not found in MyString, return the string "Character not found in string"
    If FindCharacterInString = 0 Then FindCharacterInString = "Character not found in string"

End Function

Effects of Executing Macro Example to Find Character in String

The following image illustrates the effects of using the macro (User-Defined Function) example. In this example:

  • Column A (cells A7 to A31) contains randomly generated words.
  • Column B (cells B7 to B31) contains a character (a).
  • Column C (cells C7 to C31) contains worksheet formulas working with the macro (User-Defined Function) example. These worksheet formulas return either of the following (as applicable):
    • The string “Character not found in string”, if the character specified in the applicable cell of column B is not found in the string specified in the applicable cell of column A.
    • The position of the first occurrence of the character specified in the applicable cell of column B in the string specified in the applicable cell of column A, if the character specified in the applicable cell of column B is found in the string specified in the applicable cell of column A.
  • Column D (cells D7 to D31) displays the worksheet formulas used in column C (=FindCharacterInString(CellInColumnA,CellInColumnB)).
    • The string where the search is carried out is stored in column A (CellInColumnA).
    • The searched character is stored in column B (CellInColumnB).
Example: Find character in string with VBA macros

Get immediate free access to the Excel VBA Find workbook example

#11. Excel VBA Find Column with Specific Header

VBA Code to Find Column with Specific Header

To find a column with a specific header, use the following structure/template in the applicable statement:

HeaderRowRangeObject.Find(What:=SearchedHeader, After:=SingleCellRangeObject, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=XlSearchDirectionConstant, MatchCase:=BooleanValue)

The following Sections describe the main elements in this structure.

HeaderRowRangeObject

A Range object representing the cell range containing the headers you search in.

Find

The Range.Find method:

  • Finds specific information (the header you search for) in a cell range (HeaderRowRangeObject).
  • Returns a Range object representing the first cell where the information is found.
What:=SearchedHeader

The What parameter of the Range.Find method specifies the data to search for.

To find a column with a specific header, set the What parameter to the header you search for (SearchedHeader).

After:=SingleCellRangeObject

The After parameter of the Range.Find method specifies the cell after which the search begins. This must be a single cell in the cell range containing the headers you search in (HeaderRowRangeObject).

If you omit specifying the After parameter, the search begins after the first cell of the cell range you search in (HeaderRowRangeObject).

To find a column with a specific header, set the After parameter to a Range object representing the cell after which the search begins.

LookIn:=xlValues

The LookIn parameter of the Range.Find method:

  • Specifies the type of data to search in.
  • Can take any of the built-in constants/values from the XlFindLookIn enumeration.

To find a column with a specific header, set the LookIn parameter to xlValues. xlValues refers to values.

LookAt:=xlWhole

The LookAt parameter of the Range.Find method:

  • Specifies against which of the following the data you are searching for is matched:
    • The entire/whole searched cell contents.
    • Any part of the searched cell contents.
  • Can take any of the built-in constants/values from the XlLookAt enumeration.

To find a column with a specific header, set the LookAt parameter to xlWhole. xlWhole matches the data you are searching for against the entire/whole searched cell contents.

SearchOrder:=xlByColumns

The SearchOrder parameter of the Range.Find method:

  • Specifies the order in which the applicable cell range (HeaderRowRangeObject) is searched:
    • By rows.
    • By columns.
  • Can take any of the built-in constants/values from the XlSearchOrder enumeration.

To find a column with a specific header, set the SearchOrder parameter to xlByColumns. xlByColumns searches by columns.

SearchDirection:=XlSearchDirectionConstant

The SearchDirection parameter of the Range.Find method:

  • Specifies the search direction:
    • Search for the previous match.
    • Search for the next match.
  • Can take any of the built-in constants/values from the XlSearchDirection enumeration.

To find a column with a specific header, set the SearchDirection parameter to either of the following, as applicable:

  • xlNext (SearchDirection:=xlNext): To search for the next match.
  • xlPrevious (SearchDirection:=xlPrevious): To search for the previous match.
MatchCase:=BooleanValue

The MatchCase parameter of the Range.Find method specifies whether the search is:

  • Case-sensitive; or
  • Case-insensitive.

The default value of the MatchCase parameter is False.

To find a column with a specific header, set the MatchCase parameter to either of the following, as applicable:

  • True (MatchCase:=True): To carry out a case-sensitive search.
  • False (MatchCase:=False): To carry out a case-insensitive search.

Macro Example to Find Column with Specific Header

The following macro (User-Defined Function) example does the following:

  1. Accepts two arguments:
    1. MyRange: The cell range whose first row contains the headers you search in.
    2. MyHeader: The header you search for.
  2. Finds MyHeader in the first row of MyRange.
  3. Returns the number of the column containing the first cell in the header row where the header (MyHeader) is found.
Function FindColumnWithSpecificHeader(MyRange As Range, MyHeader As Variant) As Long
    'Source: https://powerspreadsheets.com/
    'For further information: https://powerspreadsheets.com/excel-vba-find/
    
    'This UDF:
        '(1) Accepts 2 arguments: MyRange and MyHeader
        '(2) Finds a header passed as argument (MyHeader) in the first row (the header row) of a cell range passed as argument (MyRange). The search is case-insensitive
        '(3) Returns the number of the column containing the first cell in the header row where the header (MyHeader) is found
    
    With MyRange.Rows(1)
        FindColumnWithSpecificHeader = .Find(What:=MyHeader, After:=.Cells(.Cells.Count), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Column
    End With

End Function

Effects of Executing Macro Example to Find Column with Specific Header

The following image illustrates the effects of using the macro (User-Defined Function) example. In this example:

  • Columns A through H (cells A6 to H31) contain data with the following characteristics:
    • Headers in its first row (row 6).
    • Randomly generated values.
  • Cell J7 contains the searched header (Column 3).
  • Cell K7 contains the worksheet formula that works with the macro (User-Defined Function) example. This worksheet formula returns the column number of the first cell in the header row (cells A6 to H6) of the cell range (MyRange) where the header (MyHeader) is found. This is column 3 (C).
  • Cell L7 displays the worksheet formula used in cell K7 (=FindColumnWithSpecificHeader(A6:H31,J7)).
    • The cell range whose first row contains the headers where the search is carried out contains cells A6 to H31 (A6:H31).
    • The searched header is stored in cell J7 (J7).
Example: Find column with specific header with VBA macros

Get immediate free access to the Excel VBA Find workbook example

#12. Excel VBA Find Next or Find All

VBA Code to Find Next or Find All

To (i) find the next appearance of specific information or (ii) find all appearances of specific information, use the following structure/template in the applicable procedure:

Dim FoundCell As Range
Dim FirstFoundCellAddress As String
Set FoundCell = SearchedRangeObject.Find(What:=SearchedData, After:=SingleCellRangeObject, LookIn:=XlFindLookInConstant, LookAt:=XlLookAtConstant, SearchOrder:=XlSearchDirectionConstant, SearchDirection:=xlNext, MatchCase:=BooleanValue)
If Not FoundCell Is Nothing Then
    FirstFoundCellAddress = FoundCell.Address
    Do
        Statements
        Set FoundCell = SearchedRangeObject.FindNext(After:=FoundCell)
    Loop Until FoundCell.Address = FirstFoundCellAddress
End If

The following Sections describe the main elements in this structure.

Lines #1 and #2: Dim FoundCell As Range | Dim FirstFoundCellAddress As String

Dim

The Dim statement:

  1. Declares variables.
  2. Allocates storage space.
FoundCell | FirstFoundCellAddress

The names of the variables declared with the Dim statement.

  • FoundCell holds/represents the cell where the searched data is found.
  • FirstFoundCellAddress holds/represents the address of the first cell where the searched data is found.
As Range | As String

The data type of the variables declared with the Dim statement.

  • FoundCell is of the Range object data type. The Range object represents a cell or cell range.
  • FirstFoundCellAddress is of the String data type. The String data type (generally) holds textual data.

Line #3: Set FoundCell = SearchedRangeObject.Find(What:=SearchedData, After:=SingleCellRangeObject, LookIn:=XlFindLookInConstant, LookAt:=XlLookAtConstant, SearchOrder:=XlSearchDirectionConstant, SearchDirection:=xlNext, MatchCase:=BooleanValue)

Set

The Set statement assigns an object reference to an object variable.

FoundCell

Object variable holding/representing the cell where the searched data is found.

=

The assignment operator assigns an object reference (returned by the Range.Find method) to an object variable (FoundCell).

SearchedRangeObject

A Range object representing the cell range you search in.

Find

The Range.Find method:

  • Finds specific information (the data you search for) in a cell range (SearchedRangeObject).
  • Returns a Range object representing the first cell where the information is found.
What:=SearchedData

The What parameter of the Range.Find method specifies the data to search for.

To (i) find the next appearance of specific information or (ii) find all appearances of specific information, set the What parameter to the data you search for (SearchedData).

After:=SingleCellRangeObject

The After parameter of the Range.Find method specifies the cell after which the search begins. This must be a single cell in the cell range you search in (SearchedRangeObject).

If you omit specifying the After parameter, the search begins after the first cell (in the upper left corner) of the cell range you search in (SearchedRangeObject).

To (i) find the next appearance of specific information or (ii) find all appearances of specific information, set the After parameter to a Range object representing the cell after which the search begins.

LookIn:=XlFindLookInConstant

The LookIn parameter of the Range.Find method:

  • Specifies the type of data to search in.
  • Can take any of the built-in constants/values from the XlFindLookIn enumeration.

To (i) find the next appearance of specific information or (ii) find all appearances of specific information, set the LookIn parameter to any of the following, as applicable:

  • xlCommentsThreaded (LookIn:=xlCommentsThreaded): To search in the applicable cell range’s threaded comments.
  • xlValues (LookIn:=xlValues): To search in the applicable cell range’s values.
  • xlComments (LookIn:=xlComments): To search in the applicable cell range’s comments/notes.
  • xlFormulas (LookIn:=xlFormulas): To search in the applicable cell range’s formulas.
LookAt:=XlLookAtConstant

The LookAt parameter of the Range.Find method:

  • Specifies against which of the following the data you are searching for is matched:
    • The entire/whole searched cell contents.
    • Any part of the searched cell contents.
  • Can take any of the built-in constants/values from the XlLookAt enumeration.

To (i) find the next appearance of specific information or (ii) find all appearances of specific information, set the LookAt parameter to either of the following, as applicable:

  • xlWhole (LookAt:=xlWhole): To match against the entire/whole searched cell contents.
  • xlPart (LookAt:=xlPart): To match against any part of the searched cell contents.
SearchOrder:=XlSearchDirectionConstant

The SearchOrder parameter of the Range.Find method:

  • Specifies the order in which the applicable cell range (SearchedRangeObject) is searched:
    • By rows.
    • By columns.
  • Can take any of the built-in constants/values from the XlSearchOrder enumeration.

To (i) find the next appearance of specific information or (ii) find all appearances of specific information, set the SearchOrder parameter to either of the following, as applicable:

  • xlByRows (SearchOrder:=xlByRows): To search by rows.
  • xlByColumns (SearchOrder:=xlByColumns): To search by columns.
SearchDirection:=xlNext

The SearchDirection parameter of the Range.Find method:

  • Specifies the search direction:
    • Search for the previous match.
    • Search for the next match.
  • Can take any of the built-in constants/values from the XlSearchDirection enumeration.

To (i) find the next appearance of specific information or (ii) find all appearances of specific information, set the SearchDirection parameter to xlNext. xlNext results in the Range.Find method searching for the next match.

MatchCase:=BooleanValue

The MatchCase parameter of the Range.Find method specifies whether the search is:

  • Case-sensitive; or
  • Case-insensitive.

The default value of the MatchCase parameter is False.

To (i) find the next appearance of specific information or (ii) find all appearances of specific information, set the MatchCase parameter to either of the following, as applicable:

  • True (MatchCase:=True): To carry out a case-sensitive search.
  • False (MatchCase:=False): To carry out a case-insensitive search.

Lines #4 and 10: If Not FoundCell Is Nothing Then | End If

If … Then | End If

The If… Then… Else statement:

  • Conditionally executes a set of statements (lines #5 to #9);
  • Depending on an expression’s value (Not FoundCell Is Nothing).
Not FoundCell Is Nothing

The condition of an If… Then… Else statement is an expression evaluating to True or False. If the expression returns True, the applicable set of statements (lines #5 to #9) is executed.

In this expression:

  • Not FoundCell:
    • The Not operator performs a logical negation on an expression.
    • FoundCell is the object variable holding/representing the cell where the searched data is found.
    • The Range.Find method (in line #3) returns Nothing if no match is found. Therefore:
      • If the Range.Find method finds no match:
        • FoundCell is Nothing.
        • Not FoundCell is not Nothing.
      • If the Range.Find method finds a match:
        • FoundCell is not Nothing.
        • Not FoundCell is Nothing.
  • Is: The Is operator is an object reference comparison operator.
  • Nothing: Nothing allows you to disassociate a variable from the data it previously represented. The Range.Find method (in line #3) returns Nothing if no match is found.

Line #5: FirstFoundCellAddress = FoundCell.Address

FirstFoundCellAddress

Variable holding/representing the address of the first cell where the searched data is found.

=

The assignment operator assigns the result returned by an expression (FoundCell.Address) to a variable (FirstFoundCellAddress).

FoundCell

Object variable holding/representing the cell where the searched data is found.

At this point, FoundCell holds/represents the first cell where the searched data is found (by line #3).

Address

The Range.Address property returns a String representing the applicable cell range’s (FoundCell’s) reference.

Lines #6 and #9: Do | Loop Until FoundCell.Address = FirstFoundCellAddress

Do | Loop Until…

The Do… Loop Until statement repeats a set of statements until a condition becomes True.

FoundCell.Address = FirstFoundCellAddress

The condition of a Do… Loop Until statement is an expression evaluating to True or False. The applicable set of statements (lines #7 and #8) are:

  1. (Always) executed once, even if the condition is never met; and
  2. Repeatedly executed until the condition returns True.

In this expression:

  • FoundCell.Address:
    • FoundCell is an object variable holding/representing the cell where the searched data is found.
    • The Range.Address property returns a String representing the applicable cell range’s (FoundCell’s) reference.
  • =: The equal to comparison operator returns True or False as follows:
    • True if both expressions (FoundCell.Address and FirstFoundCellAddress) are equal.
    • False if the expressions (FoundCell.Address and FirstFoundCellAddress) are not equal.
  • FirstFoundCellAddress: Variable holding/representing the address of the first cell where the searched data is found.

This condition is tested (only) after the procedure finds (and works with) the first cell where the searched data is found. Therefore, the condition (only) returns True after the Range.FindNext method (line #8) wraps around to the first cell where the searched data is found (after finding all other cells where the searched data is found).

Line #7: Statements

Set of statements to be repeatedly executed for each cell where the searched data is found.

Line #8: Set FoundCell = SearchedRangeObject.FindNext(After:=FoundCell)

Set

The Set statement assigns an object reference to an object variable.

FoundCell

Object variable holding/representing the cell where the searched data is found.

=

The assignment operator assigns an object reference (returned by the Range.FindNext method) to an object variable (FoundCell).

SearchedRangeObject

A Range object representing the cell range you search in.

FindNext

The Range.FindNext method:

  • Continues the search that was begun by the Range.Find method (line #3).
  • Finds the next cell matching the conditions specified by the Range.Find method (line #3).
  • Returns a Range object representing the next cell where the information is found.
After:=FoundCell

The After parameter of the Range.FindNext method specifies the cell after which the search restarts.

To (i) find the next appearance of specific information or (ii) find all appearances of specific information, set the After parameter to the object variable holding/representing the cell where the searched data is found.

Macro Example to Find Next or Find All

The following macro example does the following:

  1. Find:
    1. All cells whose value is 10;
    2. In the cell range containing cells A6 to H30 in the “Find Next All” worksheet in the workbook where the procedure is stored.
  2. Set the interior/fill color of all found cells to light green.
Sub FindNextAll()
    'Source: https://powerspreadsheets.com/
    'For further information: https://powerspreadsheets.com/excel-vba-find/
    
    'This procedure:
        '(1) Finds all cells whose value is 10 in cells A6 to H30 of the "Find Next All" worksheet in this workbook
        '(2) Sets the found cells' interior/fill color to light green
    
    'Declare variable to hold/represent searched value
    Dim MyValue As Long
    
    'Declare variable to hold/represent address of first cell where searched value is found
    Dim FirstFoundCellAddress As String
    
    'Declare object variable to hold/represent cell range where search takes place
    Dim MyRange As Range
    
    'Declare object variable to hold/represent cell where searched value is found
    Dim FoundCell As Range
    
    'Specify searched value
    MyValue = 10
    
    'Identify cell range where search takes place
    Set MyRange = ThisWorkbook.Worksheets("Find Next All").Range("A6:H30")
    
    'Find first cell where searched value is found
    With MyRange
        Set FoundCell = .Find(What:=MyValue, After:=.Cells(.Cells.Count), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext)
    End With
    
    'Test whether searched value is found in cell range where search takes place
    If Not FoundCell Is Nothing Then
        
        'Store address of first cell where searched value is found
        FirstFoundCellAddress = FoundCell.Address
        
        Do
            
            'Set interior/fill color of applicable cell where searched value is found to light green
            FoundCell.Interior.Color = RGB(63, 189, 133)
            
            'Find next cell where searched value is found
            Set FoundCell = MyRange.FindNext(After:=FoundCell)
        
        'Loop until address of current cell where searched value is found is equal to address of first cell where searched value was found
        Loop Until FoundCell.Address = FirstFoundCellAddress
    
    End If
    
End Sub

Effects of Executing Macro Example to Find Next or Find All

The following image illustrates the effects of executing the macro example. In this example:

  • Cells A6 to H30 contain randomly generated values.
  • A text box (Find all cells where value = 10) executes the macro example when clicked.

After the macro is executed, Excel sets the interior/fill color of all cells whose value is 10 to light green.

Example: Find next appearance or find all appearances with VBA macros

Get immediate free access to the Excel VBA Find workbook example

#13. Excel VBA Find Last Row with Data in Cell Range

VBA Code to Find Last Row with Data in Cell Range

To find the last row with data in a cell range, use the following structure/template in the applicable procedure:

If Application.CountA(SearchedCellRangeObject) = 0 Then
    LastRowVariable = ValueIfCellRangeEmpty
Else
    LastRowVariable = SearchedCellRangeObject.Find(What:="*", LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
End If

The following Sections describe the main elements in this structure.

Lines #1, #3 and #5: If Application.CountA(SearchedCellRangeObject) = 0 Then | Else | End If

If… Then … Else… End If

The If… Then… Else statement:

  1. Conditionally executes a set of statements (line #2 or line #4);
  2. Depending on an expression’s value (Application.CountA(SearchedCellRangeObject) = 0).
Application.CountA(SearchedCellRangeObject) = 0

The condition of an If… Then… Else statement is an expression evaluating to True or False.

  • If the expression returns True, a set of statements (line #2) is executed.
  • If the expression returns False, a different set of statements (line #4) is executed.

In this expression:

  • Application.CountA(…): The WorksheetFunction.CountA method counts the number of cells in a cell range (SearchedCellRangeObject) that are not empty.
  • SearchedCellRangeObject: A Range object representing the cell range whose last row you search.
  • =: The equal to comparison operator returns True or False as follows:
    • True if both expressions (Application.CountA(SearchedCellRangeObject) and 0) are equal.
    • False if the expressions (Application.CountA(SearchedCellRangeObject) and 0) are not equal.
  • 0: The number 0. The WorksheetFunction.CountA method returns 0 if all cells in SearchedCellRangeObject are empty.

Line #2: LastRowVariable = ValueIfCellRangeEmpty

Assignment statement assigning:

  • ValueIfCellRangeEmpty; to
  • LastRowVariable.

In this expression:

  • LastRowVariable: Variable of (usually) the Long data type holding/representing the number of the last row with data in the cell range whose last row you search (SearchedCellRangeObject).
  • =: Assignment operator. Assigns a value (ValueIfCellRangeEmpty) to a variable (LastRowVariable).
  • ValueIfCellRangeEmpty: Value assigned to LastRowVariable when SearchedCellRangeObject is empty and the WorksheetFunction.CountA method returns 0.

Line #4: LastRowVariable = SearchedCellRangeObject.Find(What:=”*”, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

LastRowVariable

Variable of (usually) the Long data type holding/representing the number of the last row with data in the cell range whose last row you search (SearchedCellRangeObject).

=

The assignment operator assigns a value (SearchedCellRangeObject.Find(What:=”*”, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row) to a variable (LastRowVariable).

SearchedCellRangeObject

A Range object representing the cell range whose last row you search.

Find

The Range.Find method:

  • Finds specific information in a cell range (SearchedCellRangeObject).
  • Returns a Range object representing the first cell where the information is found.
What:=”*”

The What parameter of the Range.Find method specifies the data to search for.

To find the last row with data in a cell range, set the What parameter to any character sequence. The asterisk (*) acts as a wildcard and results in Range.Find searching for any character sequence.

LookIn:=xlFormulas

The LookIn parameter of the Range.Find method:

  • Specifies the type of data to search in.
  • Can take any of the built-in constants/values from the XlFindLookIn enumeration.

To find the last row with data in a cell range, set the LookIn parameter to xlFormulas. xlFormulas refers to formulas.

LookAt:=xlPart

The LookAt parameter of the Range.Find method:

  • Specifies against which of the following the data you are searching for is matched:
    • The entire/whole searched cell contents.
    • Any part of the searched cell contents.
  • Can take any of the built-in constants/values from the XlLookAt enumeration.

To find the last row with data in a cell range, set the LookAt parameter to xlPart. xlPart matches the data you are searching for (any character sequence as specified by the What parameter) against any part of the searched cell contents.

SearchOrder:=xlByRows

The SearchOrder parameter of the Range.Find method:

  • Specifies the order in which the applicable cell range (SearchedCellRangeObject) is searched:
    • By rows.
    • By columns.
  • Can take any of the built-in constants/values from the XlSearchOrder enumeration.

To find the last row with data in a cell range, set the SearchOrder parameter to xlByRows. xlByRows results in the Range.Find method searching by rows.

SearchDirection:=xlPrevious

The SearchDirection parameter of the Range.Find method:

  • Specifies the search direction:
    • Search for the previous match.
    • Search for the next match.
  • Can take any of the built-in constants/values from the XlSearchDirection enumeration.

To find the last row with data in a cell range, set the SearchDirection parameter to xlPrevious. xlPrevious results in the Range.Find method searching for the previous match.

Row

The Range.Row property returns the number of the first row of the first area in a cell range.

When searching for the last row with data in a cell range, the Range.Row property returns the row number of the cell represented by the Range object returned by the Range.Find method (Find(What:=”*”, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious)).

Macro Example to Find Last Row with Data in Cell Range

The following macro (User-Defined Function) example does the following:

  1. Accepts one argument (MyRange). This is the cell range whose last row you search.
  2. Tests whether MyRange is empty and proceeds accordingly:
    1. If MyRange is empty, returns the number 0 as the number of the last row with data in MyRange.
    2. If MyRange isn’t empty:
      1. Finds the last row with data in MyRange; and
      2. Returns the number of the last row with data in MyRange.
Function FindLastRow(MyRange As Range) As Long
    'Source: https://powerspreadsheets.com/
    'For further information: https://powerspreadsheets.com/excel-vba-find/
    
    'This UDF:
        '(1) Accepts 1 argument: MyRange
        '(2) Tests whether MyRange is empty
        '(3) If MyRange is empty, returns 0 as the number of the last row with data in MyRange
        '(4) If MyRange is not empty:
            '(1) Finds the last row with data in MyRange by searching for the last cell with any character sequence
            '(2) Returns the number of the last row with data in MyRange
        
    'Test if MyRange is empty
    If Application.CountA(MyRange) = 0 Then

        'If MyRange is empty, assign 0 to FindLastRow
        FindLastRow = 0

    Else
    
        'If MyRange isn't empty, find the last cell with any character sequence by:
            '(1) Searching for the previous match;
            '(2) Across rows
        FindLastRow = MyRange.Find(What:="*", LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

    End If
    
End Function

Effects of Executing Macro Example to Find Last Row with Data in Cell Range

The following image illustrates the effects of using the macro (User-Defined Function) example. In this example:

  • Columns A through H (cells A6 to H30) contain data.
  • Cell J7 contains the worksheet formula that works with the macro (User-Defined Function) example. This worksheet formula returns the number of the last row with data in the cell range (MyRange). This is row 30.
  • Cell K7 displays the worksheet formula used in cell J7 (=FindLastRow(A:H)). The cell range whose last row is searched contains columns A through H (A:H).
Example: Find last row with VBA macros

Get immediate free access to the Excel VBA Find workbook example

#14. Excel VBA Find Last Column with Data in Cell Range

VBA Code to Find Last Column with Data in Cell Range

To find the last column with data in a cell range, use the following structure/template in the applicable procedure:

If Application.CountA(SearchedCellRangeObject) = 0 Then
    LastColumnVariable = ValueIfCellRangeEmpty
Else
    LastColumnVariable = SearchedCellRangeObject.Find(What:="*", LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
End If

The following Sections describe the main elements in this structure.

Lines #1, #3 and #5: If Application.CountA(SearchedCellRangeObject) = 0 Then | Else | End If

If… Then … Else… End If

The If… Then… Else statement:

  1. Conditionally executes a set of statements (line #2 or line #4);
  2. Depending on an expression’s value (Application.CountA(SearchedCellRangeObject) = 0).
Application.CountA(SearchedCellRangeObject) = 0

The condition of an If… Then… Else statement is an expression evaluating to True or False.

  • If the expression returns True, a set of statements (line #2) is executed.
  • If the expression returns False, a different set of statements (line #4) is executed.

In this expression:

  • Application.CountA(…): The WorksheetFunction.CountA method counts the number of cells in a cell range (SearchedCellRangeObject) that are not empty.
  • SearchedCellRangeObject: A Range object representing the cell range whose last column you search.
  • =: The equal to comparison operator returns True or False as follows:
    • True if both expressions (Application.CountA(SearchedCellRangeObject) and 0) are equal.
    • False if the expressions (Application.CountA(SearchedCellRangeObject) and 0) are not equal.
  • 0: The number 0. The WorksheetFunction.CountA method returns 0 if all cells in SearchedCellRangeObject are empty.

Line #2: LastColumnVariable = ValueIfCellRangeEmpty

Assignment statement assigning:

  • ValueIfCellRangeEmpty; to
  • LastColumnVariable.

In this expression:

  • LastColumnVariable: Variable of (usually) the Long data type holding/representing the number of the last column with data in the cell range whose last column you search (SearchedCellRangeObject).
  • =: Assignment operator. Assigns a value (ValueIfCellRangeEmpty) to a variable (LastColumnVariable).
  • ValueIfCellRangeEmpty: Value assigned to LastColumnVariable when SearchedCellRangeObject is empty and the WorksheetFunction.CountA method returns 0.

Line #4: LastColumnVariable = SearchedCellRangeObject.Find(What:=”*”, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column

LastColumnVariable

Variable of (usually) the Long data type holding/representing the number of the last column with data in the cell range whose last column you search (SearchedCellRangeObject).

=

The assignment operator assigns a value (SearchedCellRangeObject.Find(What:=”*”, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column) to a variable (LastColumnVariable).

SearchedCellRangeObject

A Range object representing the cell range whose last column you search.

Find

The Range.Find method:

  • Finds specific information in a cell range (SearchedCellRangeObject).
  • Returns a Range object representing the first cell where the information is found.
What:=”*”

The What parameter of the Range.Find method specifies the data to search for.

To find the last column with data in a cell range, set the What parameter to any character sequence. The asterisk (*) acts as a wildcard and results in Range.Find searching for any character sequence.

LookIn:=xlFormulas

The LookIn parameter of the Range.Find method:

  • Specifies the type of data to search in.
  • Can take any of the built-in constants/values from the XlFindLookIn enumeration.

To find the last column with data in a cell range, set the LookIn parameter to xlFormulas. xlFormulas refers to formulas.

LookAt:=xlPart

The LookAt parameter of the Range.Find method:

  • Specifies against which of the following the data you are searching for is matched:
    • The entire/whole searched cell contents.
    • Any part of the searched cell contents.
  • Can take any of the built-in constants/values from the XlLookAt enumeration.

To find the last column with data in a cell range, set the LookAt parameter to xlPart. xlPart matches the data you are searching for (any character sequence as specified by the What parameter) against any part of the searched cell contents.

SearchOrder:=xlByColumns

The SearchOrder parameter of the Range.Find method:

  • Specifies the order in which the applicable cell range (SearchedCellRangeObject) is searched:
    • By rows.
    • By columns.
  • Can take any of the built-in constants/values from the XlSearchOrder enumeration.

To find the last column with data in a cell range, set the SearchOrder parameter to xlByColumns. xlByColumns results in the Range.Find method searching by columns.

SearchDirection:=xlPrevious

The SearchDirection parameter of the Range.Find method:

  • Specifies the search direction:
    • Search for the previous match.
    • Search for the next match.
  • Can take any of the built-in constants/values from the XlSearchDirection enumeration.

To find the last column with data in a cell range, set the SearchDirection parameter to xlPrevious. xlPrevious results in the Range.Find method searching for the previous match.

Column

The Range.Column property returns the number of the first column of the first area in a cell range.

When searching for the last column with data in a cell range, the Range.Column property returns the column number of the cell represented by the Range object returned by the Range.Find method (Find(What:=”*”, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious)).

Macro Example to Find Last Column with Data in Cell Range

The following macro (User-Defined Function) example does the following:

  1. Accepts one argument (MyWorksheetName). This is the name of the worksheet whose last column you search.
  2. Tests whether the worksheet named MyWorksheetName is empty and proceeds accordingly:
    1. If the worksheet named MyWorksheetName is empty, returns the number 0 as the number of the last column with data in the worksheet.
    2. If the worksheet named MyWorksheetName isn’t empty:
      1. Finds the last column with data in the worksheet; and
      2. Returns the number of the last column with data in the worksheet.
Function FindLastColumn(MyWorksheetName As String) As Long
    'Source: https://powerspreadsheets.com/
    'For further information: https://powerspreadsheets.com/excel-vba-find/
    
    'This UDF:
        '(1) Accepts 1 argument: MyWorksheetName
        '(2) Tests whether the worksheet named MyWorksheetName is empty
        '(3) If the worksheet named MyWorksheetName is empty, returns 0 as the number of the last column with data in the worksheet
        '(4) If the worksheet named MyWorksheetName is not empty:
            '(1) Finds the last column with data in the worksheet by searching for the last cell with any character sequence
            '(2) Returns the number of the last column with data in the worksheet
    
    'Declare object variable to hold/represent all cells in the worksheet named MyWorksheetName
    Dim MyRange As Range
    
    'Identify all cells in the worksheet named MyWorksheetName
    Set MyRange = ThisWorkbook.Worksheets(MyWorksheetName).Cells
    
    'Test if MyRange is empty
    If Application.CountA(MyRange) = 0 Then

        'If MyRange is empty, assign 0 to FindLastColumn
        FindLastColumn = 0

    Else
    
        'If MyRange isn't empty, find the last cell with any character sequence by:
            '(1) Searching for the previous match;
            '(2) Across columns
        FindLastColumn = MyRange.Find(What:="*", LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column

    End If
    
End Function

Effects of Executing Macro Example to Find Last Column with Data in Cell Range

The following image illustrates the effects of using the macro (User-Defined Function) example. In this example:

  • Columns A through H (cells A6 to H30) in the worksheet named “Find Last Column Data” contain data.
    • Columns A through H of the “Find Last Column Data” worksheet contain exactly the same data as that in the “Find Last Column Formula” worksheet (displayed in the image below).
    • The “Find Last Column Data” worksheet contains no data in columns J or K whereas the “Find Last Column Formula” worksheet (displayed in the image below) does.
  • Cell J7 contains the worksheet formula that works with the macro (User-Defined Function) example. This worksheet formula returns the number of the last column with data in the worksheet (named “Find Last Column Data”). This is column H or 8.
  • Cell K7 displays the worksheet formula used in cell J7 (=FindLastColumn(“Find Last Column Data”)). The name of the worksheet whose last column is searched is “Find Last Column Data”.

Get immediate free access to the Excel VBA Find workbook example

#15. Excel VBA Find Last Non Empty Cell in Column

VBA Code to Find Last Non Empty Cell in Column

To find the last non empty cell in a column, use the following structure/template in the applicable statement:

WorksheetObject.Range(ColumnLetter & WorksheetObject.Rows.Count).End(xlUp)

The following Sections describe the main elements in this structure.

WorksheetObject

A Worksheet object representing the worksheet containing the column whose last non empty cell you want to find.

Range

The Worksheet.Range property returns a Range object representing a cell or cell range.

ColumnLetter

The letter of the column whose last non empty cell you want to find.

&

The concatenation operator joins two strings and creates a new string.

WorksheetObject

A Worksheet object representing the worksheet containing the column whose last non empty cell you want to find.

Rows

The Worksheet.Rows property returns a Range object representing all rows in the applicable worksheet (containing the column whose last non empty cell you want to find).

Count

The Range.Count property returns the number of objects in a collection (the number of rows in the worksheet containing the column whose last non empty cell you want to find).

End(xlUp)

The Range.End property returns a Range object representing the cell at the end of the region containing the source range. In other words: The Range.End property is the rough equivalent of using the “Ctrl + Arrow Key” or “End, Arrow Key” keyboard shortcuts.

The Range.End property accepts one parameter: Direction. Direction:

  • Specifies the direction in which to move.
  • Can take the any of the built-in constants/values from the XlDirection enumeration.

To find the last non empty cell in a column, set the Direction parameter to xlUp. xlUp:

  • Results in moving up, to the top of the data region.
  • Is the rough equivalent of the “Ctrl + Up Arrow” or “End, Up Arrow” keyboard shortcuts.

Macro Example to Find Last Non Empty Cell in Column

The following macro (User-Defined Function) example does the following:

  1. Accepts one argument (MyColumn): The letter of the column (in the worksheet where the UDF is used) whose last non empty cell you want to find.
  2. Finds the last non empty cell in the applicable column.
  3. Returns a string containing the address (as an A1-style relative reference) of the last non empty cell in the applicable column.
Function FindLastNonEmptyCellColumn(MyColumn As String) As String
    'Source: https://powerspreadsheets.com/
    'For further information: https://powerspreadsheets.com/excel-vba-find/
    
    'This UDF:
        '(1) Accepts 1 argument: MyColumn
        '(2) Finds the last non empty cell in the column whose letter is passed as argument (MyColumn) in the worksheet where the UDF is used
        '(3) Returns the address (as an A1-style relative reference) of the last non empty cell found in the column whose letter is passed as argument (MyColumn) in the worksheet where the UDF is used
    With Application.Caller.Parent
        FindLastNonEmptyCellColumn = .Range(MyColumn & .Rows.Count).End(xlUp).Address(RowAbsolute:=False, ColumnAbsolute:=False)
    End With

End Function

Effects of Executing Macro Example to Find Last Non Empty Cell in Column

The following image illustrates the effects of using the macro (User-Defined Function) example. In this example:

  • Column A (cells A6 to A30) contains data.
  • Cell C7 contains the letter of the column whose last non empty cell is sought (A).
  • Cell D7 contains the worksheet formula that works with the macro (User-Defined Function) example. This worksheet formula returns the address (as an A1-style relative reference) of the last non empty cell in column A (MyColumn). This is cell A30
  • Cell E7 displays the worksheet formula used in cell D7 (=FindLastNonEmptyCellColumn(C7)). The letter of the column whose last non empty cell is sought is stored in cell C7 (C7).
Example: Find last non empty cell in column with VBA macros

Get immediate free access to the Excel VBA Find workbook example

#16. Excel VBA Find Empty (or Blank) Cells

VBA Code to Find Empty (or Blank) Cells

To find all empty (or blank) cells in a cell range, use the following structure/template in the applicable procedure:

Dim BlankCellsObjectVariable As Range
On Error Resume Next
Set BlankCellsObjectVariable = RangeObjectWithBlankCells.SpecialCells(xlCellTypeBlanks)
On Error GoTo 0
If Not BlankCellsObjectVariable Is Nothing Then
    StatementsIfBlankCells
End If

The following Sections describe the main elements in this structure.

Line #1: Dim BlankCellsObjectVariable As Range

Dim

The Dim statement:

  1. Declares variables.
  2. Allocates storage space.
BlankCellsObjectVariable

The name of the variable declared with the Dim statement.

BlankCellsObjectVariable holds/represents the empty (or blank) cells found.

As Range

The data type of the variable declared with the Dim statement.

BlankCellsObjectVariable is declared as of the Range object data type. The Range object represents a cell or cell range.

Line #2: On Error Resume Next

The On Error Resume Next statement specifies that, when a run-time error occurs, control passes to the statement immediately following that statement where the error occurred. Therefore, procedure execution continues.

Line #3 returns an error (Run time error ‘1004′: No cells were found) if the cell range where you search for empty (or blank) cells doesn’t contain any empty (or blank) cells.

Line #3: Set BlankCellsObjectVariable = RangeObjectWithBlankCells.SpecialCells(xlCellTypeBlanks)

Set

The Set statement assigns an object reference to an object variable.

BlankCellsObjectVariable

Object variable holding/representing the empty (or blank) cells found.

=

The assignment operator assigns an object reference (returned by the Range.SpecialCells method) to an object variable (BlankCellsObjectVariable).

RangeObjectWithBlankCells

A Range object representing the cell range you search in for empty (or blank) cells.

SpecialCells

The Range.SpecialCells method returns a Range object representing all cells matching a specified:

  • Type; and
  • Value.
xlCellTypeBlanks

The Type parameter of the Range.SpecialCells method:

  • Specifies the cells to include in the Range object returned by the Range.SpecialCells method.
  • Can take any of the built-in constants/values from the XlCellType enumeration.

To find all empty (or blank) cells in a cell range, set the Type parameter to xlCellTypeBlanks. xlCellTypeBlanks results in the Range.SpecialCells method including blank/empty cells in the Range object it returns.

Line #4: On Error GoTo 0

The On Error GoTo 0 statement disables error handling (originally enabled by line #2).

Line #5 and #7: If Not BlankCellsObjectVariable Is Nothing Then | End If

If… Then | End If

The If… Then… Else statement:

  • Conditionally executes a set of statements (line #6);
  • Depending on an expression’s value (Not BlankCellsObjectVariable Is Nothing).
Not BlankCellsObjectVariable Is Nothing

The condition of an If… Then… Else statement is an expression evaluating to True or False. If the expression returns True, the applicable set of statements (line #6) is executed.

In this expression:

  • Not BlankCellsObjectVariable:
    • The Not operator performs a logical negation on an expression.
    • BlankCellsObjectVariable is the object variable holding/representing the empty (or blank) cells found.
    • BlankCellsObjectVariable holds/represents Nothing if no empty (or blank) cells are found. Therefore:
      • If the Range.SpecialCells method finds no empty (or blank) cells:
        • BlankCellsObjectVariable is Nothing.
        • Not BlankCellsObjectVariable is not Nothing.
      • If the Range.SpecialCells method finds empty (or blank) cells:
        • BlankCellsObjectVariable is not Nothing.
        • Not BlankCellsObjectVariable is Nothing.
  • Is: The Is operator is an object reference comparison operator.
  • Nothing: Nothing allows you to disassociate a variable from the data it previously represented. BlankCellsObjectVariable holds/represents Nothing if no empty (or blank) cells are found.

Line #6: StatementsIfBlankCells

Statements conditionally executed by the If… Then… Else statement if the applicable condition (Not BlankCellsObjectVariable Is Nothing) returns True (the Range.SpecialCells method finds empty or blank cells).

Macro Example to Find Empty (or Blank) Cells

The following macro example does the following:

  1. Find all empty (or blank) cells in the cell range containing cells A6 to H30 in the “Find Blank Cells” worksheet in the workbook where the procedure is stored.
  2. Set the interior/fill color of all found empty (or blank) cells to light green.
Sub FindBlankCells()
    'Source: https://powerspreadsheets.com/
    'For further information: https://powerspreadsheets.com/excel-vba-find/
    
    'This procedure:
        '(1) Finds all blank cells in cells A6 to H30 of the "Find Blank Cells" worksheet in this workbook
        '(2) Sets the found cells' interior/fill color to light green
    
    'Declare object variable to hold/represent blank cells
    Dim MyBlankCells As Range
        
    'Enable error-handling
    On Error Resume Next
    
    'Identify blank cells in searched cell range
    Set MyBlankCells = ThisWorkbook.Worksheets("Find Blank Cells").Range("A6:H30").SpecialCells(xlCellTypeBlanks)
    
    'Disable error-handling
    On Error GoTo 0
    
    'Test whether blank cells were found in searched cell range
    If Not MyBlankCells Is Nothing Then
        
        'Set interior/fill color of blank cells found to light green
        MyBlankCells.Interior.Color = RGB(63, 189, 133)
        
    End If

End Sub

Effects of Executing Macro Example to Find Empty (or Blank) Cells

The following image illustrates the effects of using the macro example. In this example:

  • Columns A through H (cells A6 to H30) contain:
    • Data; and
    • A few empty cells (with light green interior/fill).
  • A text box (Find all blank cells) executes the macro example when clicked.

After the macro is executed, Excel sets the interior/fill color of all empty (or blank) cells to light green.

Example: Find blank cells with VBA macros

Get immediate free access to the Excel VBA Find workbook example

#17. Excel VBA Find First Empty (or Blank) Cell in Cell Range

VBA Code to Find First Empty (or Blank) Cell in Cell Range

To find the first empty (or blank) cell in a cell range, use the following structure/template in the applicable procedure:

For Each iCell In CellRangeObject
    If IsEmpty(iCell) Then
        StatementsForFirstEmptyCell
        Exit For
    End If
Next iCell

The following Sections describe the main elements in this structure.

Lines #1 and #6: For Each iCell In CellRangeObject | Next iCell

For Each … In … | Next …

The For Each… Next statement repeats a series of statements for each element (iCell, an individual cell) in a collection (CellRangeObject).

iCell

Object variable (of the Range data type) used to iterate/loop through each element of the collection (CellRangeObject).

CellRangeObject

A Range object representing the cell range you search in.

The series of statements repeated by the For Each… Next statement are repeated for each individual element (iCell) in CellRangeObject.

Lines #2 and #5: If IsEmpty(iCell) Then | End If

If… Then | End If

The If… Then Else statement:

  • Conditionally executes a set of statements (lines #3 and #4);
  • Depending on an expression’s value (IsEmpty(iCell)).
IsEmpty(iCell)

The condition of an If… Then… Else statement is an expression evaluating to True or False. If the expression returns True, the applicable set of statements (lines #3 and #4) are executed.

In this expression:

  • As a general rule, the IsEmpty function returns a Boolean value (True or False) indicating whether a variable is initialized. You can (also) use the IsEmpty function to test whether a cell is empty (or blank).
  • iCell is an object variable representing the individual cell the For Each… Next statement is currently working with (iterating through).
  • IsEmpty(iCell) returns True or False as follows:
    • True if the cell (currently) represented by iCell is empty (or blank).
    • False if the cell (currently) represented by iCell isn’t empty. A cell isn’t considered to be empty if, for example, it contains a worksheet formula returning a zero-length string (“”).

Line #3: StatementsForFirstEmptyCell

Statements conditionally executed by the If… Then… Else statement if the applicable condition (IsEmpty(iCell)) returns True (the cell currently represented by iCell is empty or blank).

Line #4: Exit For

The Exit For statement:

  • Exits a For Each… Next loop.
  • Transfers control to the statement following the Next statement (line #6).

Macro Example to Find First Empty (or Blank) Cell in Cell Range

The following macro (User-Defined Function) example does the following:

  1. Accepts one argument (MyRange): The cell range whose first empty cell you search for.
  2. Loops through each individual cell in the cell range (MyRange) and tests whether the applicable cell is empty (or blank).
  3. Returns the following:
    1. If MyRange contains empty cells, the address (as an A1-style relative reference) of the first empty cell in the cell range (MyRange).
    2. If MyRange doesn’t contain empty cells, the string “No empty cells found in cell range”.
Function FindNextEmptyCellRange(MyRange As Range) As String
    'Source: https://powerspreadsheets.com/
    'For further information: https://powerspreadsheets.com/excel-vba-find/
    
    'This UDF:
        '(1) Accepts 1 argument: MyRange
        '(2) Loops through each individual cell in the cell range (MyRange) and tests whether the applicable cell is empty
        '(3) Returns the following (as applicable):
            'If there are empty cells in MyRange: The address (as an A1-style relative reference) of the first empty cell
            'If there are no empty cells in MyRange: The string "No empty cells found in cell range"
    
    'Declare object variable to iterate/loop through all cells in the cell range (MyRange)
    Dim iCell As Range
    
    'Loop through each cell in the cell range (MyRange)
    For Each iCell In MyRange
        
        'If the current cell is empty:
            '(1) Return the current cell's address (as an A1-style relative reference)
            '(2) Exit the For Each... Next loop
        If IsEmpty(iCell) Then
            FindNextEmptyCellRange = iCell.Address(RowAbsolute:=False, ColumnAbsolute:=False)
            Exit For
        End If
        
    Next iCell
    
    'If no empty cells are found in the cell range (MyRange), return the string "No empty cells found in cell range"
    If FindNextEmptyCellRange = "" Then FindNextEmptyCellRange = "No empty cells found in cell range"

End Function

Effects of Executing Macro Example to Find First Empty (or Blank) Cell in Cell Range

The following image illustrates the effects of using the macro (User-Defined Function) example. In this example:

  • Columns A through H (cells A6 to H30) contain:
    • Data; and
    • A few empty cells (with light green interior/fill).
  • Cell J7 contains the worksheet formula that works with the macro (User-Defined Function) example. This worksheet formula returns the address (as an A1-style relative reference) of the first empty cell in the cell range (MyRange). This is cell B7.
  • Cell K7 displays the worksheet formula used in cell J7 (=FindNextEmptyCellRange(A6:H30)). The cell range where the search is carried out contains cells A6 to H30 (A6:H30).
Example: Find first empty cell in cell range with VBA macros

Get immediate free access to the Excel VBA Find workbook example

#18. Excel VBA Find First Empty (or Blank) Cell in Column

VBA Code to Find First Empty (or Blank) Cell in Column

To find the first empty (or blank) cell in a column, use the following structure/template in the applicable statement:

ColumnRangeObject.Find(What:="", After:=ColumnRangeObject.Cells(ColumnRangeObject.Cells.Count), LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext)

The following Sections describe the main elements in this structure.

ColumnRangeObject

A Range object representing the column whose first empty (or blank) cell you search for.

Find

The Range.Find method:

  • Finds specific information (the first empty or blank cell) in a cell range (ColumnRangeObject).
  • Returns a Range object representing the first cell where the information is found.
What:=””

The What parameter of the Range.Find method specifies the data to search for.

To find the first empty (or blank) cell in a column, set the What parameter to a zero-length string (“”).

After:=ColumnRangeObject.Cells(ColumnRangeObject.Cells.Count)

The After parameter of the Range.Find method specifies the cell after which the search begins.

To find the first empty (or blank) cell in a column, set the After parameter to “ColumnRangeObject.Cells(ColumnRangeObject.Cells.Count)”. For these purposes:

  • “ColumnRangeObject” is a Range object representing the column whose first empty (or blank) cell you search for.
  • The Range.Cells property (Cells) returns a Range object representing all cells in the column whose first empty (or blank) cell you search for.
  • The Range.Item property returns a Range object representing the last cell in the column whose first empty (or blank) cell you search for. For these purposes, the RowIndex parameter of the Range.Item property is set to the value returned by the Range.Count property (Count).
  • The Range.Count property (Count) returns the number of cells in the Range object returned by the Range.Cells property (ColumnRangeObject.Cells).
LookIn:=xlFormulas

The LookIn parameter of the Range.Find method:

  • Specifies the type of data to search in.
  • Can take any of the built-in constants/values from the XlFindLookIn enumeration.

To find the first empty (or blank) cell in a column, set the LookIn parameter to xlFormulas. xlFormulas refers to formulas.

LookAt:=xlWhole

The LookAt parameter of the Range.Find method:

  • Specifies against which of the following the data you are searching for is matched:
    • The entire/whole searched cell contents.
    • Any part of the searched cell contents.
  • The LookAt parameter can take any of the built-in constants/values from the XlLookAt enumeration.

To find the first empty (or blank) cell in a column, set the LookAt parameter to xlWhole. xlWhole matches the data you are searching for against the entire/whole searched cell contents.

SearchOrder:=xlByRows

The SearchOrder parameter of the Range.Find method:

  • Specifies the order in which the applicable cell range (ColumnRangeObject) is searched:
    • By rows.
    • By columns.
  • Can take any of the built-in constants/values from the XlSearchOrder enumeration.

To find the first empty (or blank) cell in a column, set the SearchOrder parameter to xlByRows. xlByRows searches by rows.

SearchDirection:=xlNext

The SearchDirection parameter of the Range.Find method:

  • Specifies the search direction:
    • Search for the previous match.
    • Search for the next match.
  • Can take any of the built-in constants/values from the XlSearchDirection enumeration.

To find the first empty (or blank) cell in a column, set the SearchDirection parameter to xlNext. xlNext searches for the next match.

Macro Example to Find First Empty (or Blank) Cell in Column

The following macro (User-Defined Function) example does the following:

  1. Accepts one argument (MyColumn): The letter of the column (in the worksheet where the UDF is used) whose first empty (or blank) cell you search for.
  2. Finds the first empty (or blank) cell in the applicable column.
  3. Returns a string containing the address (as an A1-style relative reference) of the first empty (or blank) cell in the applicable column.
Function FindFirstEmptyCellColumn(MyColumn As String) As String
    'Source: https://powerspreadsheets.com/
    'For further information: https://powerspreadsheets.com/excel-vba-find/
    
    'This UDF:
        '(1) Accepts 1 argument: MyColumn
        '(2) Finds the first empty (blank) cell in the column whose letter is passed as argument (MyColumn) in the worksheet where the UDF is used
        '(3) Returns the address (as an A1-style relative reference) of the first empty (blank) cell in the column whose letter is passed as argument (MyColumn) in the worksheet where the UDF is used
    With Application.Caller.Parent.Columns(MyColumn)
        FindFirstEmptyCellColumn = .Find(What:="", After:=.Cells(.Cells.Count), LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext).Address(RowAbsolute:=False, ColumnAbsolute:=False)
    End With

End Function

Effects of Executing Macro Example to Find First Empty (or Blank) Cell in Column

The following image illustrates the effects of using the macro (User-Defined Function) example. In this example:

  • Column I (cells I1 to I25) contains:
    • Data; and
    • A few empty cells (with light green interior/fill).
  • Cell A7 contains the letter of the column whose first empty (or blank) cell is sought (I).
  • Cell B7 contains the worksheet formula that works with the macro (User-Defined Function) example. This worksheet formula returns the address (as an A1-style relative reference) of the first empty (or blank) cell in column I (MyColumn). This is cell I10.
  • Cell C7 displays the worksheet formula used in cell B7 (=FindFirstEmptyCellColumn(A7)). The letter of the column whose first empty (or blank) cell is sought is stored in cell A7 (A7).

Example: Find first empty (or blank) cell in column with VBA macros

Get immediate free access to the Excel VBA Find workbook example

#19. Excel VBA Find Next Empty (or Blank) Cell in Column

VBA Code to Find Next Empty (or Blank) Cell in Column

To find the next empty (or blank) cell in a column, use the following structure/template in the applicable procedure:

If IsEmpty(RangeObjectSourceCell) Then
    RangeObjectSourceCell.ActionForNextEmptyCell
ElseIf IsEmpty(RangeObjectSourceCell.Offset(1, 0)) Then
    RangeObjectSourceCell.Offset(1, 0).ActionForNextEmptyCell
Else
    RangeObjectSourceCell.End(xlDown).Offset(1, 0).ActionForNextEmptyCell
End If

The following Sections describe the main elements in this structure.

Lines #1, #3, #5 and #7: If IsEmpty(RangeObjectSourceCell) Then | ElseIf IsEmpty(RangeObjectSourceCell.Offset(1, 0)) Then | Else | End If

If… Then | ElseIf… Then | Else | End If

The If… Then… Else statement:

  • Conditionally executes a set of statements (lines #2, #4 or #6);
  • Depending on an expression’s value (IsEmpty(RangeObjectSourceCell) or IsEmpty(RangeObjectSourceCell.Offset(1, 0))).
IsEmpty(RangeObjectSourceCell)

The condition of an If… Then… Else statement is an expression evaluating to True or False. If the expression returns True, the applicable set of statements (line #2) is executed.

In this expression:

  • As a general rule, the IsEmpty function returns a Boolean value (True or False) indicating whether a variable is initialized. You can (also) use the IsEmpty function to test whether a cell is empty (or blank).
  • RangeObjectSourceCell is a Range object representing the cell where the search (for the next empty or blank cell in the column) begins.
  • IsEmpty(RangeObjectSourceCell) returns True or False as follows:
    • True if the cell represented by RangeObjectSourceCell is empty (or blank).
    • False if the cell represented by RangeObjectSourceCell isn’t empty. A cell isn’t considered to be empty if, for example, it contains a worksheet formula returning a zero-length string (“”).
IsEmpty(RangeObjectSourceCell.Offset(1, 0))

The condition of an If… Then… Else statement is an expression evaluating to True or False. If the expression returns True, the applicable set of statements (line #4) is executed.

In this expression:

  • As a general rule, the IsEmpty function returns a Boolean value (True or False) indicating whether a variable is initialized. You can (also) use the IsEmpty function to test whether a cell is empty (or blank).
  • RangeObjectSourceCell is a Range object representing the cell where the search (for the next empty or blank cell in the column) begins.
  • The Range.Offset property (Offset(1, 0)) returns a Range object representing a cell range at an offset from the source cell range. In this expression:
    • The source cell range is the cell where the search (for the next empty or blank cell in the column) begins (RangeObjectSourceCell).
    • The offset from the source cell range is as follows:
      • 1 row downwards, as specified by the RowOffset parameter of the Range.Offset property (1).
      • No column offsetting, as specified by the ColumnOffset parameter of the Range.Offset property (0).
  • IsEmpty(RangeObjectSourceCell.Offset(1, 0)) returns True or False as follows:
    • True if the cell represented by the Range object returned by the Range.Offset property is empty.
    • False if the cell represented by the Range object returned by the Range.Offset property isn’t empty. A cell isn’t considered to be empty if, for example, it contains a worksheet formula returning a zero-length string (“”).

Line #2: RangeObjectSourceCell.ActionForNextEmptyCell

Statement conditionally executed by the If… Then… Else statement if the applicable condition (IsEmpty(RangeObjectSourceCell)) returns True.

RangeObjectSourceCell

A Range object representing the cell where the search (for the next empty or blank cell in the column) begins.

ActionForNextEmptyCell

VBA construct (usually a property or method) working with the applicable cell (represented by RangeObjectSourceCell).

Line #4: RangeObjectSourceCell.Offset(1, 0).ActionForNextEmptyCell

Statement conditionally executed by the If… Then… Else statement if the applicable condition (IsEmpty(RangeObjectSourceCell.Offset(1, 0))) returns True.

RangeObjectSourceCell

A Range object representing the cell where the search (for the next empty or blank cell in the column) begins.

Offset(1, 0)

The Range.Offset property (Offset(1, 0)) returns a Range object representing a cell range at an offset from the source cell range. For these purposes:

  • The source cell range is the cell where the search (for the next empty or blank cell in the column) begins (RangeObjectSourceCell).
  • The offset from the source cell range is as follows:
    • 1 row downwards, as specified by the RowOffset parameter of the Range.Offset property (1).
    • No column offsetting, as specified by the ColumnOffset parameter of the Range.Offset property (0).
ActionForNextEmptyCell

VBA construct (usually a property or method) working with the applicable cell (returned by the Range.Offset property).

Line #6: RangeObjectSourceCell.End(xlDown).Offset(1, 0).ActionForNextEmptyCell

Statement conditionally executed by the If… Then… Else statement if no previous condition (in lines #1 or #3) returns True.

RangeObjectSourceCell

A Range object representing the cell where the search (for the next empty or blank cell in the column) begins.

End(xlDown)

The Range.End property returns a Range object representing the cell at the end of the region containing the source range. In other words: The Range.End property is the rough equivalent of using the “Ctrl + Arrow Key” or “End, Arrow Key” keyboard shortcuts.

The Range.End property accepts one parameter: Direction. Direction:

  • Specifies the direction in which to move.
  • Can take the any of the built-in constants/values from the XlDirection enumeration.

To find the next empty (or blank) cell in a column, set the Direction parameter to xlDown. xlDown:

  • Results in moving down, to the bottom of the data region.
  • Is the rough equivalent of the “Ctrl + Down Arrow” or “End, Down Arrow” keyboard shortcuts.
Offset(1, 0)

The Range.Offset property (Offset(1, 0)) returns a Range object representing a cell range at an offset from the source cell range. For these purposes:

  • The source cell range is the cell represented by the Range object returned by the Range.End property (End(xlDown)).
  • The offset from the source cell range is as follows:
    • 1 row downwards, as specified by the RowOffset parameter of the Range.Offset property (1).
    • No column offsetting, as specified by the ColumnOffset parameter of the Range.Offset property (0).
ActionForNextEmptyCell

VBA construct (usually a property or method) working with the applicable cell (returned by the Range.Offset property).

Macro Example to Find Next Empty (or Blank) Cell in Column

The following macro (User-Defined Function) example does the following:

  1. Accepts one argument (MySourceCell): The cell where the search (for the next empty or blank cell in the column) begins.
  2. Finds the next empty (or blank) cell in the applicable column after MySourceCell.
  3. Returns a string containing the address (as an R1C1-style absolute reference) of the next empty (or blank) cell in the applicable column.
Function FindNextEmptyCellColumn(MySourceCell As Range) As String
    'Source: https://powerspreadsheets.com/
    'For further information: https://powerspreadsheets.com/excel-vba-find/
    
    'This UDF:
        '(1) Accepts 1 argument: MySourceCell
        '(2) Finds the next empty/blank cell in the applicable column and after the cell passed as argument (MySourceCell)
        '(3) Returns the address (as an R1C1-style absolute reference) of the next empty/blank cell in the applicable column and after the cell passed as argument (MySourceCell)
    
    With MySourceCell(1)
        
        'If first cell in cell range passed as argument (MySourceCell) is empty, obtain/return that cell's address
        If IsEmpty(MySourceCell(1)) Then
            FindNextEmptyCellColumn = .Address(ReferenceStyle:=xlR1C1)
        
        'If cell below first cell in cell range passed as argument (MySourceCell) is empty, obtain/return that cell's address
        ElseIf IsEmpty(.Offset(1, 0)) Then
            FindNextEmptyCellColumn = .Offset(1, 0).Address(ReferenceStyle:=xlR1C1)
        
        'Otherwise:
            '(1) Find the next empty/blank cell in the applicable column and after the first cell in cell range passed as argument (MySourceCell)
            '(2) Obtain/return the applicable cell's address
        Else
            FindNextEmptyCellColumn = .End(xlDown).Offset(1, 0).Address(ReferenceStyle:=xlR1C1)
        End If
        
    End With

End Function

Effects of Executing Macro Example to Find Next Empty (or Blank) Cell in Column

The following image illustrates the effects of using the macro (User-Defined Function) example. In this example:

  • Column A (cells A6 to A30) contains:
    • Data; and
    • A few empty cells.
  • Cell C7 contains the worksheet formula that works with the macro (User-Defined Function) example. This worksheet formula returns the address (as an R1C1-style absolute reference) of the next empty (or blank) cell in column A after cell A13 (MySourceCell). This cell R21C1 (or A21 as an A1-style relative reference).
  • Cell D7 displays the worksheet formula used in cell C7 (=FindNextEmptyCellColumn(A13)). The cell where the search (for the next empty or blank cell in the column) begins is A13 (A13).
Example: Find next empty cell in column with VBA macros

Get immediate free access to the Excel VBA Find workbook example

#20. Excel VBA Find Value in Array

VBA Code to Find Value in Array

To find a numeric value in a one-dimensional array, use the following structure/template in the applicable procedure:

Dim PositionOfValueInArray As Variant
Dim LoopCounter As Long
PositionOfValueInArray = OutputIfValueNotInArray
For LoopCounter = LBound(SearchedArray) To UBound(SearchedArray)
    If SearchedArray(LoopCounter) = SearchedValue Then
        PositionOfValueInArray = LoopCounter
        Exit For
    End If
Next LoopCounter

The following Sections describe the main elements in this structure.

Lines #1 and #2: Dim PositionOfValueInArray As Variant | Dim LoopCounter As Long

The Dim statement:

  1. Declares variables.
  2. Allocates storage space.
PositionOfValueInArray | LoopCounter

The names of the variables declared with the Dim statement.

  • PositionOfValueInArray holds/represents:
    • The position of the searched numeric value in the array, if the searched numeric value is found in the array.
    • The default/fallback output/data, if the searched numeric value isn’t found in the array.
  • LoopCounter represents the loop counter used in the For… Next loop in lines #4 to #9.
As Variant | As Long

The data type of the variables declared with the Dim statement.

  • PositionOfValueInArray is of the Variant data type. The Variant data type can (generally) contain any type of data, except for fixed-length Strings.
  • LoopCounter is of the Long data type. The Long data type can contain integers between -2,147,483,648 and 2,147,483,647.

Line #3: PositionOfValueInArray = OutputIfValueNotInArray

PositionOfValueInArray

Variable holding/representing:

  • The position of the searched numeric value in the array, if the searched numeric value is found in the array.
  • The default/fallback output/data, if the searched numeric value isn’t found in the array.
=

The assignment operator assigns the result returned by an expression (OutputIfValueNotInArray) to a variable (PositionOfValueInArray).

OutputIfValueNotInArray

Expression specifying the default/fallback output/data to be assigned to the PositionOfValueInArray variable if the searched numeric value isn’t found in the array.

Lines #4 and #9: For LoopCounter = LBound(SearchedArray) To UBound(SearchedArray) | Next LoopCounter

For… = … To … | Next…

The For… Next statement repeats a series of statements a specific number of times.

LoopCounter

Variable holding/representing the loop counter.

LBound(…) | UBound(…)

The LBound/UBound functions return the lower/upper bound of an array’s dimension. The LBound/UBound functions accept two arguments:

  • ArrayName: The name of the array whose lower/upper bound you want to obtain.
  • Dimension:
    • An optional argument.
    • The dimension (of the applicable array) whose lower/upper bound you want to obtain.

If you omit specifying the Dimension argument, the LBound/UBound functions return the lower/upper bound of the array’s first dimension.

The initial/final values of LoopCounter are set to the following:

  • Initial value (Start) of LoopCounter: The output returned by the LBound function (LBound(SearchedArray)).
  • Final value (End) of LoopCounter: The output returned by the UBound function (UBound(SearchedArray)).
SearchedArray

The array you search in.

SearchedArray is passed as the ArrayName argument of the LBound/UBound functions.

Lines #5 and #8: If SearchedArray(LoopCounter) = SearchedValue Then | End If

If… Then… End If

The If… Then… Else statement:

  1. Conditionally executes a set of statements (lines #6 and #7);
  2. Depending on an expression’s value (SearchedArray(LoopCounter) = SearchedValue).
SearchedArray(LoopCounter) = SearchedValue

The condition of an If… Then… Else statement is an expression evaluating to True or False.

  • If the expression returns True, a set of statements (lines #6 and #7) is executed.
  • If the expression returns False:
    • No statements are executed.
    • Execution continues with the statement following End If (line #8).

In this expression:

  • SearchedArray: The array you search in.
  • LoopCounter: Variable holding/representing the loop counter.
  • SearchedArray(LoopCounter): Array element whose index number is LoopCounter.
  • =: The equal to comparison operator returns True or False as follows:
    • True if both expressions (SearchedArray(LoopCounter) and SearchedValue) are equal.
    • False if the expressions (SearchedArray(LoopCounter) and SearchedValue) are not equal.
  • SearchedValue: Numeric value you search for in the array (SearchedArray).

Line #6: PositionOfValueInArray = LoopCounter

PositionOfValueInArray

Variable holding/representing:

  • The position of the searched numeric value in the array, if the searched numeric value is found in the array.
  • The default/fallback output/data, if the searched numeric value isn’t found in the array.
=

The assignment operator assigns the result returned by an expression (LoopCounter) to a variable (PositionOfValueInArray).

LoopCounter

Variable holding/representing the loop counter.

Line #7: Exit For

The Exit For statement:

  • Exits a For… Next loop.
  • Transfers control to the statement following the Next statement (line #8).

Macro Example to Find Value in Array

The following macro (User-Defined Function) example does the following:

  1. Accepts two arguments:
    1. MyArray: The array you search in.
    2. MyValue: The numeric value you search for.
  2. Loops through each element in MyArray and tests whether the applicable element is equal to MyValue.
  3. Returns the following:
    1. If MyValue is not found in MyArray, the string “Value not found in array”.
    2. If MyValue is found in MyArray, the position/index number of MyValue in MyArray.
Function FindValueInArray(MyArray As Variant, MyValue As Variant) As Variant
    'Source: https://powerspreadsheets.com/
    'For further information: https://powerspreadsheets.com/excel-vba-find/
    
    'This UDF:
        '(1) Accepts 2 arguments: MyArray and MyValue
        '(2) Loops through each element in the array (MyArray) and tests whether the applicable element is equal to the searched value (MyValue)
        '(3) Returns the following (as applicable):
            'If MyValue is not found in MyArray: The string "Value not found in array"
            'If MyValue is found in MyArray: The position (index number) of MyValue in MyArray
    
    'Declare variable to represent loop counter
    Dim iCounter As Long

    'Specify default/fallback value/string to be returned ("Value not found in array") if MyValue is not found in MyArray
    FindValueInArray = "Value not found in array"

    'Loop through each element in the array (MyArray)
    For iCounter = LBound(MyArray) To UBound(MyArray)
    
        'Test if the current array element is equal to MyValue
        If MyArray(iCounter) = MyValue Then
            
            'If the current array element is equal to MyValue:
                '(1) Return the position (index number) of the current array element
                '(2) Exit the For... Next loop
            FindValueInArray = iCounter
            Exit For
            
        End If
        
    Next iCounter

End Function

Effects of Executing Macro Example to Find Value in Array

To illustrate the effects of using the macro (User-Defined Function) example, I use the following macro. The following macro does the following:

  1. Declare an array (MySearchedArray).
  2. Fill the array with values (0, 10, 20, …, 90).
  3. Call the FindValueInArray macro (User-Defined Function) example to find the number “50” in the array.
  4. Display a message box with the value/string returned by the FindValueInArray macro (User-Defined Function) example.
Sub FindValueInArrayCaller()
    'Source: https://powerspreadsheets.com/
    'For further information: https://powerspreadsheets.com/excel-vba-find/
    
    'This procedure:
        '(1) Declares an array and fills it with values (0, 10, 20, ..., 90)
        '(2) Calls the FindValueInArray UDF to find the number "50" in the array
        '(3) Displays a message box with the value/string returned by the FindValueInArray UDF
    
    'Declare a zero-based array with 10 elements of the Long data type
    Dim MySearchedArray(0 To 9) As Long
    
    'Assign values to array elements
    MySearchedArray(0) = 0
    MySearchedArray(1) = 10
    MySearchedArray(2) = 20
    MySearchedArray(3) = 30
    MySearchedArray(4) = 40
    MySearchedArray(5) = 50
    MySearchedArray(6) = 60
    MySearchedArray(7) = 70
    MySearchedArray(8) = 80
    MySearchedArray(9) = 90
    
    'Do the following:
        '(1) Call the FindValueInArray UDF and search for the number "50" in MySearchedArray
        '(2) Display a message box with the value/string returned by the FindValueInArray UDF
    MsgBox FindValueInArray(MySearchedArray, 50)
    

End Sub

The following GIF illustrates the effects of using the macro above to call the macro (User-Defined Function) example.

As expected, this results in Excel displaying a message box with the number 5. This is the position of the searched numeric value (50) in the array.

Example: Find value in array with VBA macros

Get immediate free access to the Excel VBA Find workbook example

Learn More About Excel VBA Find

Workbook Example Used in this Excel VBA Find Tutorial

This VBA Find Tutorial is accompanied by an Excel workbook containing the data and macros I use in the examples above. You can get free access to this example workbook by clicking the button below.

Get immediate free access to the Excel VBA Find workbook example

The Power Spreadsheets Library

The Books at The Power Spreadsheets Library are comprehensive and actionable guides for professionals who want to:

  • Automate Excel;
  • Save time for the things that really matter; and
  • Open new career opportunities.

Learn more about The Power Spreadsheets Library here.

Понравилась статья? Поделить с друзьями:
  • Excel find cell with value
  • Excel find any text in cell
  • Excel find and replace not in formulas
  • Excel find and replace in one column
  • Excel find and remove