我觉得这是愚蠢的简单,但我已经搜索和实验了一段时间,似乎是空手而归(对我来说也很晚了)。我猜我找错词了。不管怎样,让我解释一下。
在Excel中,使用Xlookup功能时可以有多个条件。
“普通”Xlookup看起来如下所示:
=xlookup("ThingToLookFor", "Search Range", "Return Range")多个条件Xlookup看起来如下所示:
=xlookup("ThingToLookFor" & "OtherThingToLookFor", "Search Range 1" & "Search Range 2", "Return Range")我试图在VBA中做一个多标准的Xlookup,但在我的一生中,我无法想出如何写出它。使用"&“将两个字符串组合在一起,所以这是没有好处的。我试过一些不同的东西,但它似乎不喜欢我扔给它的任何东西。
那么,在VBA中,正确的版本是什么:
WorksheetFunction.Xlookup("ThingToLookFor" & OtherThingToLookFor", "Search Range 1" & "Search Range 2", "Return Range")? 注意:在搜索时,我确实发现了“评估”,这是相当整洁的。我能让它起作用,但我不确定我喜欢它。我真的希望能找到一种让WorksheetFunction工作的方法。
编辑--我在这里尝试做的一个更具体的例子:
Sub xlookup_test()
Dim Lookup_Value_1 As String
Lookup_Value_1 = "My Document"
Dim Lookup_Value_2 As String
Lookup_Value_2 = "Sales"
Dim Search_List_1 As Range
Set Search_List_1 = Document_Control.Range("DC_Document_Type")
Dim Search_List_2 As Range
Set Search_List_2 = Document_Control.Range("DC_Document_Name")
Dim Return_List As Range
Set Return_List = Document_Control.Range("DC_Document_ID")
Dim Return_Value As String
' this is the problem line
Return_Value = WorksheetFunction.XLookup(Lookup_Value_1 & Lookup_Value_2, Search_List_1 & Search_List_2, Return_List)
Debug.Print (Return_Value)
End Sub但是,正如前面提到的,使用"&“只是将两个字符串组合在一起形成一个字符串,而不是告诉它它需要寻找两种不同的东西。
发布于 2021-09-07 08:19:16
不幸的是,我无法在我的XLOOKUP版本中访问它,所以我无法进行测试。但我认为问题在于你在搜索范围内使用引号(")。
你说:“使用"&”将两个字符串组合在一起“。这正是正在发生的事情,只不过您正在组合字符串OF地址,而不是将上的字符串组合成这些地址。
尝试这样做(您将需要调整搜索范围和标准以适应):
WorksheetFunction.Xlookup("ThingToLookFor" & "OtherThingToLookFor", A1:A3&B1:B3, C1:C3)如果"ThingToLookFor“和"OtherThingToLookFor”也是单元格引用,请记住也要从这些引用中删除"。
https://stackoverflow.com/questions/69082327
复制相似问题