Vba nth occurance

I am trying to find the nth occurrence in a column of a value.

I found this function but it is not meeting my needs because I need it to search a column and return the position so that I can incorporate it in other vba code

Function Nth_Occurrence(range_look As Range, find_it As String, occurrence As Long, offset_row As Long, offset_col As Long)

Dim lCount As Long

Dim rFound As Range



    Set rFound = range_look.Cells(1, 1)

        For lCount = 1 To occurrence

            Set rFound = range_look.Find(find_it, rFound, xlValues, xlWhole)

        Next lCount

    Nth_Occurrence = rFound.Offset(offset_row, offset_col)

End Function

Open in new window


I am trying to pass it this way

Sub test()


   myrange = Column(1)

     'Trend
    beg = Application.Match("S Order", myrange, 0)
   end = Nth_Occurrence(myrange, "S Order", 2, 0, 0)

    Range("B" & beg, "B" & end - 1).Select
    Range(Selection, Selection.End(xlToRight)).Select

End Sub

Open in new window

LVL 1
montrofAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
Is this what you need?

Option Explicit

Sub test()
    ' Report the row number for the 3rd occurrence of "aa" in column A.
    MsgBox "Found in row " & Occurrence("aa", "A", 3).Row
End Sub
Private Function Occurrence(strValue As String, strCol As String, intOccurrence As Integer) As Range

Dim lngLastRow As Long
Dim lngRow As Long
Dim intCount As Long

lngLastRow = Range(strCol & "1048576").End(xlUp).Row

For lngRow = 1 To lngLastRow
    If Cells(lngRow, strCol) = strValue Then
        intCount = intCount + 1
        If intCount = intOccurrence Then
            Set Occurrence = Range(strCol & lngRow)
            Exit Function
        End If
    End If
Next
Set Occurrence = Nothing
End Function

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
montrofAuthor Commented:
That's it.   Thank you
Martin LissOlder than dirtCommented:
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you
including these two new ones.
Creating your own Excel Formulas and doing the impossible
A Guide to Writing Understandable and Maintainable VBA Code
Marty - MVP 2009 to 2015, Experts-Exchange Top Expert Visual Basic Classic 2012 to 2014
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.