Leading the way in Microsoft Office Development
 Home|Excel|Word|PowerPoint|Consultancy|Feedback|Contact 
 Microsoft Excel > Functions User Defined > FINDMATCH< Previous | Next > 

 

FINDMATCH(sLookupValue, rgeLookupArray)

 
 

Returns the position of an item in a list.

 

 
sLookupValueThe value you want to find.
rgeLookupArrayThe contiguous range of cells containing possible lookup values.
 

 

REMARKS

 
 
  • The "rgeLookupArray" must be a single block of cells, either in a row or a column.

     
     
  • This function uses a binary search and is very similar to the MATCH() function.

     
     
  • Thanks to Chad Langhans for this contribution.

     

     
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    Option Explicit

    Public Function FINDMATCH(ByVal sLookupValue As String, _
                              ByVal rgeLookupArray As Range) As Integer
                               
    Dim ilow As Integer
    Dim ihigh As Integer
    Dim imiddle As Integer

       ilow = 0
       ihigh = rgeLookupArray.Cells.Count
                               
       Do While ilow <= ihigh
          imiddle = (ilow + ihigh) / 2
          
          If VBA.StrComp(sLookupValue, rgeLookupArray.Cells(imiddle, 1).Value, _
                         VbCompareMethod.vbTextCompare) = 0 Then
             FINDMATCH = imiddle
             Exit Function
          End If
       
          If VBA.StrComp(sLookupValue, rgeLookupArray.Cells(imiddle, 1).Value, _
                         VbCompareMethod.vbTextCompare) = -1 Then
             ihigh = imiddle - 1
          Else
             ilow = imiddle + 1
          End If
       Loop
       
       Call MsgBox("This entry has not been found")
       FINDMATCH = 0
    End Function
       

     

    Example

     
       

     Copyright © 2004-2007 Better Solutions Limited. All Rights Reserved.< Previous | Top | Next >