Lookup function

finnrichard

New Member
Joined
Sep 12, 2014
Messages
2
Dear fellow forum users,

I am trying to make a lookup function in VBA that returns the nth occurence in a table. So far I have written the following code, but it doesn't work. I am new to VBA, can anyone please help me?

Function FindNthOccurence(lookup_value As String, mytable As Range, Nth As Integer) As String
Dim i As Integer
Dim currentdindex As Integer
currentindex = 0
For i = 1 To mytable.Rows.Count
If mytable(i, 1) = lookup_value Then
currentindex = currentindex + 1
If currentindex = Nth Then
FindNthOccurence = currentindex
Next i
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
This tweaked version is working for me to return the "position" in the table of the Nth occurrence:

Code:
Function FindNthOccurence(lookup_value As String, mytable As Range, Nth As Integer) As String
Dim i As Long, currentindex As Long

For i = 1 To mytable.Rows.Count
    If mytable(i, 1) = lookup_value Then
        currentindex = currentindex + 1
        If currentindex = Nth Then
            FindNthOccurence = i
            Exit Function
        End If
    End If
Next i

End Function
 
Upvote 0
.
.

I prefer this function, which returns the cell address of the n-th occurrence:

Code:
Function LOCATION(lookup_value As String, lookup_array As Range, Optional index_num As Long = 1)

    Dim lkp As Range
    Dim ind As Long
    Dim add() As String
    
    ind = 0
    For Each lkp In lookup_array
        If lkp.Value = lookup_value Then
            ind = ind + 1
            ReDim Preserve add(1 To ind)
            add(ind) = lkp.Address
        End If
    Next lkp
    
    If ind = 0 Then
        LOCATION = CVErr(xlErrNA)
    Else
        LOCATION = add(index_num)
    End If
        
End Function
 
Last edited:
Upvote 0
Thank you. I have made some arrangements but I still get "#NAME?"

I have a list of clients' telephone numbers (column 4) from a certain country (first column in MyTable). I would like to look up the telephone number of the fifth american in the list.
So lookup value is usa, occurrence=5, columnnumber is 4



Code:
Function FindNthOccurence(lookup_value As String, mytable As Range, Nth As Integer, columnnumber As Integer) As String
Dim i As Long
Dim currentdindex As Long
currentindex = 0


For i = 1 To mytable.Rows.Count
    If mytable(i, 1).Text = lookup_value Then
    currentindex = currentindex + 1
    If currentindex = Nth Then
    FindNthOccurence = mytable(i, columnnumber)
    Exit Function
End If
End If
Next i
End Function
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,392
Members
449,081
Latest member
JAMES KECULAH

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top