简单的Excel VBAsearch和查找function赋予#VALUE! 错误

这是我十年来第二次使用VBA / Excel,并不是一个有趣的回报。 我试图做一些非常简单的事情,但是我不确定我的数据types是错误的还是什么,debugging器并没有给我太多的帮助。 执行失败的单元格中使用我的自定义函数与#VALUE! 错误。 这使我认为我的数据types是无效的,但我引用了几个不同的来源,找不到问题是什么。

该函数应该search描述单元格文本,以查找存储在Lookup1范围内的任何子string的任何匹配项,然后使用lookup2范围进行散列表样式转换(如果find的话)。

Function ExtractCategory(Description As String, Lookup1 As Range, _ Lookup2 As Range) As String Dim txt As String Dim SubjCell As Range For Each rRange In Lookup1 SubjCell = rRange txt = SubjCell.Value If InStr(txt, Description) <> 0 Then ExtractCategory = Application.WorksheetFunction.Lookup(txt, _ Lookup1, Lookup2) Exit For Else ExtractCategory = "Not Found" End If Next rRange End Function 

我仍然不确定的唯一types问题是txt = SubjCell.Value 。 当我尝试使用SubjCell.Text ,IDE将其去除大写text …不知道为什么。

试试这个(两个工作 – 试用和testing

 Function ExtractCategory(Description As String, Lookup1 As Range, _ Lookup2 As Range) As String Dim rRange As Range For Each rRange In Lookup1 If InStr(1, rRange.Value, Description) Then ExtractCategory = Evaluate("=lookup(" & rRange.Value & "," & _ Lookup1.Address & "," & Lookup2.Address & ")") Exit For Else ExtractCategory = "Not Found" End If Next rRange End Function 

这是一样的

 Function ExtractCategory(Description As String, Lookup1 As Range, _ Lookup2 As Range) As String Dim rRange As Range For Each rRange In Lookup1 If InStr(1, rRange.Value, Description) Then ExtractCategory = Application.WorksheetFunction.Lookup(rRange.Value, _ Lookup1, Lookup2) Exit For Else ExtractCategory = "Not Found" End If Next rRange End Function 

编辑

快照

在这里输入图像说明