David Kearns Central RSS 2.0
# Friday, June 19, 2009
Algorithm implementation/Strings/Levenshtein distance - Wikibooks, collection of open-content textbooks:

Visual Basic for Applications (no Damerau extension)

This version is identical to JavaScript and PHP implementations in this article. I had problems when I tried to use the other VBA implementation in this article, so I had to adopt the version below.

Application.WorksheetFunction.Min() method is Excel-specific. If you implement it with other VBA-enabled applications, uncomment the conditional block and comment out the Application.WorksheetFunction.Min() line. 

Function levenshtein(a As String, b As String) As Integer

Dim i As Integer
Dim j As Integer
Dim cost As Integer
Dim d() As Integer
Dim min1 As Integer
Dim min2 As Integer
Dim min3 As Integer

If Len(a) = 0 Then
levenshtein = Len(b)
Exit Function
End If

If Len(b) = 0 Then
levenshtein = Len(a)
Exit Function
End If

ReDim d(Len(a), Len(b))

For i = 0 To Len(a)
d(i, 0) = i
Next

For j = 0 To Len(b)
d(0, j) = j
Next

For i = 1 To Len(a)
For j = 1 To Len(b)
If Mid(a, i, 1) = Mid(b, j, 1) Then
cost = 0
Else
cost = 1
End If

' Since Min() function is not a part of VBA, we'll "emulate" it below
min1 = (d(i - 1, j) + 1)
min2 = (d(i, j - 1) + 1)
min3 = (d(i - 1, j - 1) + cost)

' If min1 <= min2 And min1 <= min3 Then
' d(i, j) = min1
' ElseIf min2 <= min1 And min2 <= min3 Then
' d(i, j) = min2
' Else
' d(i, j) = min3
' End If

' In Excel we can use Min() function that is included
' as a method of WorksheetFunction object
d(i, j) = Application.WorksheetFunction.Min(min1, min2, min3)
Next
Next

levenshtein = d(Len(a), Len(b))

End Function

I'm no VBA dev, and it confuses me how all of it is called, so a quick google for:

VBA Tips: Writing Your First VBA Function:

Writing Your First VBA Function in Excel

About User Defined Functions

Excel provides the user with a large collection of ready-made functions, more than enough to satisfy the average user. Many more can be added by installing the various add-ins that are available.

Most calculations can be achieved with what is provided, but it isn't long before you find yourself wishing that there was a function that did a particular job, and you can't find anything suitable in the list. You need a UDF.

A UDF (User Defined Function) is simply a function that you create yourself with VBA. UDFs are often called "Custom Functions". A UDF can remain in a code module attached to a workbook, in which case it will always be available when that workbook is open. Alternatively you can create your own add-in containing one or more functions that you can install into Excel just like a commercial add-in.

UDFs can be accessed by code modules too. Often UDFs are created by developers to work solely within the code of a VBA procedure and the user is never aware of their existence.

Like any function, the UDF can be as simple or as complex as you want. Let's start with an easy one...

And I see I have to "Insert" a "Module" to paste the code in. And then it just works. Nice.

Friday, June 19, 2009 11:52:16 AM (Eastern Daylight Time, UTC-04:00)  #    Comments [3] -
work
Friday, June 19, 2009 12:37:41 PM (Eastern Daylight Time, UTC-04:00)
Funny, I had to add a custom function to Excel a few months ago and had to learn this too. I can't for the life of me remember what the function was for though. I think it was a modified concat function.
Nate
Friday, June 26, 2009 2:35:50 PM (Eastern Daylight Time, UTC-04:00)
Just had to do the modified concatenation again. Turns out I wanted to concat a range. http://excel.tips.net/Pages/T003062_Concatenating_Ranges_of_Cells.html
Nate
Friday, June 26, 2009 2:53:05 PM (Eastern Daylight Time, UTC-04:00)
But... it appears that the example I linked to didn't work. I had to fix it (which I remember having to do last time as well) so that the function name is the name of the variable that gets returned by the function. So... instead of a function Concat2 with a variable named Concat, I renamed it to something useful like ConcatRange and made sure the resultant variable matched.
Nate
Comments are closed.
Archive
<June 2009>
SunMonTueWedThuFriSat
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011
Blogroll
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions.

© Copyright 2010
David Kearns
Sign In
Statistics
Total Posts: 1300
This Year: 1
This Month: 0
This Week: 0
Comments: 1761
Themes
Pick a theme:
All Content © 2010, David Kearns
DasBlog theme 'Business' created by Christoph De Baene (delarou)