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?
 
aikimarkConnect With a Mentor Commented:
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
 
Martin LissOlder than dirtCommented:
Please describe exactly what the function needs to do.
0
 
NorieVBA ExpertCommented:
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
ouestqueAuthor Commented:
Hey guys. I  am trying to create a User Defined Function that is faster than Match(). Is that possible?
0
 
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
 
NorieVBA ExpertCommented:
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:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.