Excel 2007: VBA function faster than Match function

What is the VBA for a function that is faster than the Match function?
ouestqueAsked:
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:
Please describe exactly what the function needs to do.
0
NorieAnalyst Assistant Commented:
I don't think there is a function that's faster than MATCH, in fact a combination of INDEX/MATCH is often used instead of VLOOKUP.

What exactly are you doing?
0
ouestqueAuthor Commented:
Hey guys. I  am trying to create a User Defined Function that is faster than Match(). Is that possible?
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

ouestqueAuthor Commented:
So the UDF would need to basically do what the Match function does, but faster. Find the row number of a value in a range.
0
NorieAnalyst Assistant Commented:
Using a UDF instead of native Excel functionality is more than likely going to be slower.
0
aikimarkCommented:
You can do this.  However, you need to understand that the speed will be offset by any update activity to the range you are matching against.  Before we begin, a few questions.

1. How static is the match range?
2. How big is the match range?
3. What is the data type of the cell values in the match range?  (numbers, dates, string, other, mixed)
4. Do you need the exact functionality of the Match() function or just searching for exact matches?
5. Is your current match range sorted?  If not, you should test the Match() function performance with sorted data.
0
ouestqueAuthor Commented:
1. Match range on most sheets do not change. Nonetheless, on some sheets new records are added every week.
2. 50k rows.
3. String.
4. Just searching for exact matches.
5. Yes.
0
aikimarkCommented:
Firstly, there needs to be workbook/worksheet to initialize the dictionary and rebuild the dictionary if anything changes in the matching data range.
Option Explicit

Private Sub Workbook_Open()
    Set clsFastMatch = New FastMatch
    clsFastMatch.BuildDic Application.Names("ICD10codes").RefersToRange
End Sub


Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    If Sh.Name = "ICD10codes" Then
        If Intersect(Target, Application.Names("ICD10codes").RefersToRange) Is Nothing Then
        Else
            clsFastMatch.BuildDic Application.Names("ICD10codes").RefersToRange
        End If
    End If
End Sub

Open in new window


The module is pretty simple.  We only need a public variable for our class and a function we can use in a worksheet formula
Option Explicit

Public clsFastMatch As FastMatch

Public Function FasterMatch(parmValue As String)
    FasterMatch = clsFastMatch.GetMatch(parmValue)
End Function

Open in new window


And the class isn't overly complicated.  It has a private dictionary and both Get and Build methods.
Option Explicit

Private dicMatches As Object

Private Sub Class_Initialize()
    Set dicMatches = CreateObject("scripting.dictionary")
End Sub

Private Sub Class_Terminate()
    dicMatches.RemoveAll
    Set dicMatches = Nothing
End Sub

Public Sub BuildDic(parmRange As Range)
    Dim rng As Range
    dicMatches.RemoveAll
    For Each rng In parmRange
        dicMatches(rng.Value) = rng.Row
    Next
End Sub

Public Function GetMatch(parmValue As String)
    If dicMatches.exists(parmValue) Then
        GetMatch = dicMatches(parmValue)
    Else
        GetMatch = Null
    End If
End Function

Open in new window

I've put all this code and test data (ICD10 codes) into the uploaded workbook.  You will see the tests I've run on the second worksheet.
Note: The data does not need to be sorted in order to achieve fast performance.
Q_29078244.xlsm
0

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
aikimarkCommented:
If we need to simulate the Match() function more exactly, the FasterMatch() function should probably return #N/A error instead of -1 in the not-found scenario.
0
ouestqueAuthor Commented:
Awesome solution aikimark!! Thank You!!
0
aikimarkCommented:
It is possible to expand this solution to multiple ranges you want to do FasterMatch() upon.
0
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
VBA

From novice to tech pro — start learning today.