When Should You Use the VBA Range.Find Method?
The Range.Find method is basically used to find specific information within a range. It has various types of attributes that can be used depending on the required values. It will return a Range object that represents the first cell where the information is found.
expression .Find(What, After, LookIn, LookAt, SearchOrder, SearchDirection, MatchCase, MatchByte, SearchFormat)
expression A variable that represents a Range object.
|What||Required||Variant||The data to search for. Can be a string or any Microsoft Excel data type.|
|After||Optional||Variant||The cell after which you want the search to begin. This corresponds to the position of the active cell when a search is done from the user interface. Notice that After must be a single cell in the range. Remember that the search begins after this cell; the specified cell isn?t searched until the method wraps back around to this cell. If you do no specify this argument, the search starts after the cell in the upper-left corner of the range.|
|LookIn||Optional||Variant||Can be one of the following XlFindLookIn constants: xlFormulas, xlValues, or xlNotes.|
|LookAt||Optional||Variant||Can be one of the following XlLookAt constants: xlWhole or xlPart.|
|SearchOrder||Optional||Variant||Can be one of the following XlSearchOrder constants: xlByRows or xlByColumns.|
|SearchDirection||Optional||XlSearchDirection||The search direction.|
|MatchCase||Optional||Variant||True to make the search case sensitive. The default value is False.|
|MatchByte||Optional||Variant||Used only if you have selected or installed double-byte language support. True to have double-byte characters match only double-byte characters. False to have double-byte characters match their single-byte equivalents.|
|SearchFormat||Optional||Variant||The search format.|
The Find method returns Nothing if there is no match found. It does not affect the selection or the active cell.
The settings for LookIn, LookAt, SearchOrder, and MatchByte are saved each time you use the Find method. If you do not specify values for these parameters the next time you call the method, the saved values are used. Setting these parameters changes the settings in the Find dialog box; and changing the settings in the Find dialog box changes the saved values that are used if you omit the parameters. To avoid problems, set these parameters explicitly whenever you use this method.
When the search reaches the end of the specified range, it wraps around to the beginning of the range. To stop a search when this wraparound occurs, save the address of the first found cell, and then test each successive found-cell address against this saved address.
This example finds all cells in the range B1:B20 on worksheet one that contain the value “Reviewed” and changes them to “Accepted”.
Sub FindMethod() With Worksheets("031217").Range("B1:B20") Set c = .Find("Reviewed", LookIn:=xlValues) If Not c Is Nothing Then firstAddress = c.Address Do c.Value = "Accepted" Set c = .FindNext(c) Loop While Not c Is Nothing And c.Address <> firstAddress End If End With End Sub