Background:
As part of a project I am working on which involves fuzzy string matching, I have implemented the Levenshtein Distance algorithm in VBA to calculate the "similarity" between two strings (see this question for part of the code/more insight into my project).
So, I created a table in Sheet1
in Excel with row and column headers which are strings (located in cells A2:A2146
and B1:TU1
, respectively), and I am comparing these strings with the LevenshteinDistance
function. The function populates the empty cells in the table (in my case, B2:TU2146
) with what I call the matchScore
. The idea is this: the more similar two strings are, the lower their matchScore
. It follows that if two strings match exactly, we would have matchScore = 0
.
(1) More specifically, suppose the value of S1
(one of my column headers) is "recursion" and the value of cell A532
(one of my row headers) is "recursion". After executing my "similarity" function, the value returned in cell S532
of the table is 0
.
What I want to achieve:
For the purposes of my problem and the heuristic I've defined to measure string similarity, I am particularly interested in string pairs for which matchScore <= 1
is true (this includes the example (1) above).
The data table is huge, and it is difficult for me to get visibility into the "good data" (matchScore <= 1
). Thus, I want Excel to find each value in the table which is <=1 and output them in Sheet2
along with the strings that were paired off as "good matches." Therefore, there should be three columns of data in Sheet2
. To reference again the example above (1), when my code finishes running, I should see 0
, "recursion", and "recursion" in cells A1
through C1
(assuming this was the only "good match" I found in the table).
What I have tried to implement as a solution:
Sub FindMatches()
Dim r As Long, c As Range
r = 1
For Each c In Range("B2:BY2146").Cells
If c.Value <= 1 Then Sheets("Sheet2").Cells(r, 1).Resize(1, 3).Value = Array(c.Value, Cells(c.Row, 1), Cells(1, c.Column))
r = r + 1
Next c
End Sub
Effectively, this sub does nothing. Where am I wrong in the way I am attempting to implement my solution, and what can I do to solve this issue?