Hi,
I am new to vba and this is my first function attempt. I can't get this function to work. As a sub, it returns the value I want but when I changed it to a function, I get #VALUE! error.
I want users to be able to type in a formula and get the correct value and fill the formula down across all rows.
Purpose: lookup and return a value from range in table 2 that most closely matches the value in the specified cell in table 1. Its essentially a VLookup but with special code for when the strings being compared aren't exact.
Code summary: lets say Brian M Smith is in A2 in the left table. Names of people are in column B of the right table but the names are different (ie: in the right table, the name is Smith, Brian). The code splits the name Brian M Smith into discrete elements and stores in an array. For each cell in the range of table 2, the name is also split and stored in a separate array. The code then compares each array element individually and gets a score when they match. The function should return the name from the right table that has the highest score.
Here is the code:
Function JDELookup(ByVal lookupValue As Range, _
ByVal lookupArray As Range)
Dim LRarray As Integer 'last row of right table
Dim lookupCell As Range 'each cell in the lookupArray
Dim strLen1 As Integer 'length of string in cell from left table
Dim strLen2 As Integer 'length of string in cell from right table
Dim replLen1 As Integer 'length of string in cell from left table after formatting the string by replacing commas and spaces with single space
Dim replLen2 As Integer 'length of string in cell from right table after formatting the string by replacing commas and spaces with single space
Dim spaceCnt1 As Integer 'number of space found in the formatted string of cell in left table
Dim spaceCnt2 As Integer 'number of space found in the formatted string of cell in right table
Dim componCnt1 As Integer 'component count: counts the number of components in the cell from left table separated by spaces
Dim componCnt2 As Integer 'component count: counts the number of components in the cell from right table separated by spaces
Dim componString1() As String 'the string of each component from the left table
Dim componString2() As String 'the string of each component from the right table
Dim imax As Single 'the highest count of matches between the two strings
Dim ieval As Single 'the count of matches of the two strings currently being evaluated
Dim delValue As String 'the value of the sucessful lookup
lookupValue = LCase(Application.Trim(Replace(Replace(Replace(lookupValue, ",", " "), " ", " "), " ", " ")))
strLen1 = Len(lookupValue)
replLen1 = Len(Replace(lookupValue, " ", ""))
spaceCnt1 = strLen1 - replLen1
componCnt1 = spaceCnt1 + 1
componString1 = Split(lookupValue, " ")
LRarray = lookupArray.Cells(lookupArray.Rows.Count, 1).End(xlUp).Row
imax = 0
For Each lookupCell In Range(lookupArray.Cells(1, 1), lookupArray.Cells(LRarray, 1))
lookupCell = LCase(Application.Trim(Replace(Replace(Replace(lookupCell.Value, ",", " "), " ", " "), " ", " ")))
strLen2 = Len(lookupCell)
replLen2 = Len(Replace(lookupCell, " ", ""))
spaceCnt2 = strLen2 - replLen2
componCnt2 = spaceCnt2 + 1
componString2 = Split(lookupCell, " ")
ieval = 0
If lookupValue <> lookupCell Then
If InStr(1, lookupCell, componString1(0)) Then
For spaceCnt1 = 0 To UBound(componString1)
For spaceCnt2 = 0 To UBound(componString2)
If InStr(1, componString1(spaceCnt1), componString2(spaceCnt2)) Then
If Len(componString1(spaceCnt1)) = 1 Then
ieval = ieval + 0.1
Else
ieval = ieval + 1
End If
End If
Next
Next
If ieval > imax Then
imax = ieval
delValue = lookupCell.Value
End If
End If
Else
delValue = lookupCell.Value
Exit For
End If
Next lookupCell
JDELookup = delValue
End Function
This is stored in module 2 of the project (not in "Sheet1" module).
Can someone help me trouble shoot this?
Thanks,
Jason
I am new to vba and this is my first function attempt. I can't get this function to work. As a sub, it returns the value I want but when I changed it to a function, I get #VALUE! error.
I want users to be able to type in a formula and get the correct value and fill the formula down across all rows.
Purpose: lookup and return a value from range in table 2 that most closely matches the value in the specified cell in table 1. Its essentially a VLookup but with special code for when the strings being compared aren't exact.
Code summary: lets say Brian M Smith is in A2 in the left table. Names of people are in column B of the right table but the names are different (ie: in the right table, the name is Smith, Brian). The code splits the name Brian M Smith into discrete elements and stores in an array. For each cell in the range of table 2, the name is also split and stored in a separate array. The code then compares each array element individually and gets a score when they match. The function should return the name from the right table that has the highest score.
Here is the code:
Function JDELookup(ByVal lookupValue As Range, _
ByVal lookupArray As Range)
Dim LRarray As Integer 'last row of right table
Dim lookupCell As Range 'each cell in the lookupArray
Dim strLen1 As Integer 'length of string in cell from left table
Dim strLen2 As Integer 'length of string in cell from right table
Dim replLen1 As Integer 'length of string in cell from left table after formatting the string by replacing commas and spaces with single space
Dim replLen2 As Integer 'length of string in cell from right table after formatting the string by replacing commas and spaces with single space
Dim spaceCnt1 As Integer 'number of space found in the formatted string of cell in left table
Dim spaceCnt2 As Integer 'number of space found in the formatted string of cell in right table
Dim componCnt1 As Integer 'component count: counts the number of components in the cell from left table separated by spaces
Dim componCnt2 As Integer 'component count: counts the number of components in the cell from right table separated by spaces
Dim componString1() As String 'the string of each component from the left table
Dim componString2() As String 'the string of each component from the right table
Dim imax As Single 'the highest count of matches between the two strings
Dim ieval As Single 'the count of matches of the two strings currently being evaluated
Dim delValue As String 'the value of the sucessful lookup
lookupValue = LCase(Application.Trim(Replace(Replace(Replace(lookupValue, ",", " "), " ", " "), " ", " ")))
strLen1 = Len(lookupValue)
replLen1 = Len(Replace(lookupValue, " ", ""))
spaceCnt1 = strLen1 - replLen1
componCnt1 = spaceCnt1 + 1
componString1 = Split(lookupValue, " ")
LRarray = lookupArray.Cells(lookupArray.Rows.Count, 1).End(xlUp).Row
imax = 0
For Each lookupCell In Range(lookupArray.Cells(1, 1), lookupArray.Cells(LRarray, 1))
lookupCell = LCase(Application.Trim(Replace(Replace(Replace(lookupCell.Value, ",", " "), " ", " "), " ", " ")))
strLen2 = Len(lookupCell)
replLen2 = Len(Replace(lookupCell, " ", ""))
spaceCnt2 = strLen2 - replLen2
componCnt2 = spaceCnt2 + 1
componString2 = Split(lookupCell, " ")
ieval = 0
If lookupValue <> lookupCell Then
If InStr(1, lookupCell, componString1(0)) Then
For spaceCnt1 = 0 To UBound(componString1)
For spaceCnt2 = 0 To UBound(componString2)
If InStr(1, componString1(spaceCnt1), componString2(spaceCnt2)) Then
If Len(componString1(spaceCnt1)) = 1 Then
ieval = ieval + 0.1
Else
ieval = ieval + 1
End If
End If
Next
Next
If ieval > imax Then
imax = ieval
delValue = lookupCell.Value
End If
End If
Else
delValue = lookupCell.Value
Exit For
End If
Next lookupCell
JDELookup = delValue
End Function
This is stored in module 2 of the project (not in "Sheet1" module).
Can someone help me trouble shoot this?
Thanks,
Jason
Last edited: