简体   繁体   English

使用 VBA,有没有办法根据两列的条件查找单元格(类似于索引匹配公式)

[英]Using VBA, is there a way to look up a cell based on criteria for two columns (similar to Index Match formula)

I'm trying to figure out the best way to lookup cell information based on criteria for two columns.我正在尝试根据两列的条件找出查找单元格信息的最佳方法。 For example, I have column A with one set of numbers, column B with another set of numbers, and column C with the cell information I want to extract.例如,我的 A 列包含一组数字,B 列包含另一组数字,列 C 包含我要提取的单元格信息。 The cell info must match the Column A & Column B info I give in a user form, and then when I click 'Search' I want the userform to populate with Column C's info.单元格信息必须与我在用户表单中提供的 A 列和 B 列信息匹配,然后当我单击“搜索”时,我希望用户表单填充 C 列的信息。 The userform coding is fine-I'm just having trouble with the 'lookup' aspect.用户窗体编码很好——我只是在“查找”方面遇到了麻烦。 If I were to write this code not in vba and just as an array, it would look like so:如果我不在 vba 中编写此代码,而只是将其作为一个数组,它看起来会像这样:

={INDEX(A1:C20,MATCH(1,(A:A=ColumnAItem)*(B:B=ColumnBItem),0),3)}

And this is basically what I've figured out on my own so far in VBA:到目前为止,这基本上是我自己在 VBA 中得出的结论:

Private Sub SearchButton_Click()

Dim SAP_A As Variant, SAP_B As Variant
Dim ws As Worksheet, mA, mB


Set ws = Sheets("Database Entry Sheet")


SAP_A = Trim(textbox5.Value)
SAP_B = Trim(textbox8.Value)

    mA = Application.Match(CLng(SAP_A), ws.Range("A:A"), 0)
    mB = Application.Match(CLng(SAP_B), ws.Range("B:B"), 0)

    If Not IsError(mA) And IsError(mB) Then
        textbox1.Text = ws.Cells(mA, "C")

End Sub

Try this试试这个

Private Sub SearchButton_Click()

Dim SAP_A As Variant, SAP_B As Variant
Dim ws As Worksheet, mA, mB


Set ws = Sheets("Database Entry Sheet")


SAP_A = Trim(textbox5.Value)
SAP_B = Trim(textbox8.Value)

    mA = Application.Match(CLng(SAP_A), ws.Range("A:A"), 0)
    mB = Application.Match(CLng(SAP_B), ws.Range("B:B"), 0)
    
    '* Added Not before IsError(mB)
    If Not IsError(mA) And Not IsError(mB) Then
        '* Make sure they correspond to the same row
        If mA = mB Then   
            textbox1.Text = ws.Cells(mA, "C")
        End If
    End If '* End the if statement
End Sub

Note my comments starting with '*请注意我以'*开头的评论

There's however a subtle problem with the logic here if values are repeated on the same column: Take this example然而,如果值在同一列上重复,则这里的逻辑存在一个微妙的问题:以这个例子为例

A  B  C
1  2  V1
5  1  V2
9  4  V3
3  1  V4
2  7  V5

Now if the user searches for SAP_A = 3 and SAP_B = 1 then mA = 4 and mB = 2 and the code therefore, is not going to give you V4 as you would expect.现在,如果用户搜索SAP_A = 3SAP_B = 1 ,则mA = 4mB = 2 ,因此代码不会像您期望的那样为您提供V4 This is because Match gives you the index of the first matched value.这是因为Match为您提供了第一个匹配值的索引。 Therefore, if you have repeated values in either column, then it is better to loop down columns A and B and check if both values match and return the value in column C. ( Edit: or even better, read the whole range into a variant array and therefore, loop in memory rather a range: as @BigBen suggested in the comments)因此,如果您在任一列中有重复值,那么最好向下循环 A 列和 B 列并检查两个值是否匹配并返回列 C 中的值。(编辑:或者甚至更好,将整个范围读入一个变体数组,因此,在 memory 中循环而不是一个范围:正如@BigBen 在评论中建议的那样)

Another approach:另一种方法:

Private Sub SearchButton_Click()

    Dim A As Variant, B As Variant
    Dim ws As Worksheet, f, m

    Set ws = ActiveSheet

    A = "B"
    B = "F"

    'Note: whether or not you need quotes in the formula around
    '  A and B will depend on the expected datatypes
    f = "=(A1:A20=""{A}"")*(B1:B20=""{B}"")"
    f = Replace(f, "{A}", A)
    f = Replace(f, "{B}", B)
    Debug.Print f
    
    m = Application.Match(1, ws.Evaluate(f), 0)
    
    If Not IsError(m) Then
        Debug.Print ws.Cells(m, "C")
    Else
        Debug.Print "no match for", A, B
    End If

End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 多个查找值/查找值数组的索引匹配匹配公式或类似内容 - Index match match formula or similar for multiple look-up values/an array of look-up values 使用数组公式将具有多个条件的索引匹配转换为VBA - Index Match with multiple criteria conversion to VBA using an array formula 如何使用两个条件(数组公式[pref]或VBA)基于数据构建数组 - How do I build an array based on data using two criteria (Array Formula [pref] or VBA) 发出多标准INDEX MATCH公式 - Issue with a multiple criteria INDEX MATCH formula 根据日期条件返回匹配项的数组公式 - Array formula to return match based on date criteria 如何更改我的多标准索引匹配公式,使其对最接近今天的结果进行排序? - How do I change my multi criteria Index Match formula in such a way that it sorts results closest to today? 将具有动态范围的索引匹配公式转换为VBA - Converting Index Match Formula with Dynamic Range to VBA 数组公式索引匹配以基于第二,第三和第四最小值选择单元格 - Array Formula Index Match to Select Cell Based on Second, Third, and Fourth Minimum Value 在谷歌表格中匹配索引两列条件和一行条件 - Match index two column criteria and one row criteria in google sheets Excel:使用公式(无 VBA)如何检查范围内的元素是否在另一个由索引/匹配定义的数组中 - Excel: Using a formula (no VBA) how to check if elements in a range are in another array which is defined by index/match
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM