[英]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 = 3
和SAP_B = 1
,则mA = 4
和mB = 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.