The Find function is one of the most common functions used in VBA. The method allows users to locate
the first occurrence of a piece of information within a range. You can use this method to search in a sheet,
or part of a Microsoft Excel spreadsheet. This tutorial will explain how to find a value in an Excel column
using the Find function in VBA.
MyRange: This expression represents the Range object, which designates in which cells you'd like to
search for the value contained in What parameter. This can be your full sheet (Sheets(1).Cells.Find(...)), a
column (Sheets(1).Columns(3).Find(...)), a row (Sheets(1).Rows(7).Find(....)), or a range of cells
(Sheets(1).Range("D12:F56").Find(....)). What: This parameter is required. This parameter allows you to
stipulate which value you want to find in your range. It can be of any data type supported by Excel. After:
This parameter is optional. This parameter indicates the starting cell for the search (note that the cell must
be unique). If After is not specified, the search will begin in the upper left corner of the range. LookIn: This
parameter is optional. The parameter is used to locate a value in a range. Other variations of LookIn
include xlValues, xlFormulas, and xlComments. LookAt: This parameter is optional. LookAt indicates
whether or not the value must be exactly equal to the value sought, or partially equal. For example, if one
were to search for the value "10" in a matrix including: 6210, 4105, 540, 163, 154, 132, 10, there are
various methods you can use. To tell VBA that you only want the number 10, you can use the parameter
LookAt:=XlWhole. In contrast, to indicate you are searching for values that contain 10 (in this case, 6210 or
4105), you can use LookAt:=XlPart. SearchOrder: This parameter is optional. The search order is subject
to two constants: xlByRows (for rows), or xlByColumns (for columns). SearchDirection: This parameter is
optional. This parameter indicates the direction of search in a particular range. The two constants are
xlNext, which is used to search a subsequent value in the range, and xlPrevious, which is used to search
a value stated previously. MatchCase: This parameter is optional. The two values for this argument are
True and False. Case-sensitive searches should be set to True. SearchFormat: This parameter is
optional. The parameter can be True or False depending on whether or not a format is assigned (e.g.
monetary standard, number, border, fill, alignment, etc.).
Sub Cherche()
'déclaration des variables :
Dim Trouve As Range, PlageDeRecherche As Range
Dim Valeur_Cherchee As String, AdresseTrouvee As String
N.B. If we had made a search for the 1024 number instead of the word "Find", we should have been
declared Valeur_Cherchee as an integer. The What variant allows you to search for any type of data.
In the example coded here we will look for the word "mot" in the A1:A20 range:
Sub Principale()
Dim Plage As RangeDim Lignes(), i As LongDim Texte As StringDim Flag As BooleanSet Plage =
Sheets("Feuil1").Range("A1:A20") 'plage de rechercheTexte = "mot" 'expression cherchéeFlag = Find_Next(Plage, Texte,
Lignes()) 'appel de la fonctionIf Flag Then 'si fonction retourne Vrai = expression trouvée dans la plage For i =
LBound(Lignes) To UBound(Lignes) 'restitution des lignes correspondantes Debug.Print Lignes(i) Next iElse
MsgBox "L'expression : " & Texte & " n'a pas été trouvée dans la plage : " & Plage.AddressEnd IfEnd Sub'Sources :
Michel_m'http://www.commentcamarche.net/forum/affich-31432413-importation-de-donnees-sans-doublons#9Function
Find_Next(Rng As Range, Texte As String, Tbl()) As BooleanDim Nbre As Integer, Lig As Long, Cptr As Long, Adresse As
String Nbre = Application.CountIf(Rng, Texte) If Nbre > 0 Then ReDim Tbl(Nbre - 1) Lig = 1 For Cptr = 0 To
Nbre - 1 Lig = Rng.Find(Texte, Cells(Lig, Rng.Column), xlValues).Row Tbl(Cptr) = Lig Next Else GoTo
Absent End If Find_Next = True Exit FunctionAbsent: Find_Next = FalseEnd Function
N.B. For an entire column, simply replace:
By:
N.B. To return the addresses of the cells rather than the line number in the Find_Next function, replace:
By:
FindAll
This custom function returns Find and FindNext results as an array of values. It will find all the instances of
a string (sText As String) and returns an array containing the line numbers. The parameters of this function
are as follows: ByVal sText As String represents the target value. ByRef oSht As Worksheet represents
the target sheet. ByRef sRange As String represents the range. ByRef arMatches() As String
represents the array that will store the returned values. The code:
Function FindAll(ByVal sText As String, ByRef oSht As Worksheet, ByRef sRange As String, ByRef arMatches() As String) As
Boolean
' --------------------------------------------------------------------------------------------------------------
' FindAll - To find all instances of the1 given string and return the row numbers.
' If there are not any matches the function will return false
' --------------------------------------------------------------------------------------------------------------
On Error GoTo Err_Trap
Dim rFnd As Range ' Range Object
Dim iArr As Integer ' Counter for Array
Dim rFirstAddress ' Address of the First Find
' -----------------
' Clear the Array
' -----------------
Erase arMatches
Set rFnd = oSht.Range(sRange).Find(what:=sText, LookIn:=xlValues, lookAt:=xlPart)
This document entitled « Search and Find Using VBA » from CCM (ccm.net) is made available under the Creative Commons license.
You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.