[英]Excel VBA Lookup
Trying to use VBA in Excel to perform some VLookup equivalent function.尝试在 Excel 中使用 VBA 来执行一些 VLookup 等效功能。 I have this sample table:我有这个示例表:
Basically, this is what I want to do:基本上,这就是我想要做的:
1) Based on the value in B12, lookup the value in table A1:A8. 1) 根据 B12 中的值,查找表 A1:A8 中的值。 2) Set cell B13 with the lookup returned value 3) If no match found (example, B12=100000), throw an error message. 2) 将单元格 B13 设置为查找返回值 3) 如果未找到匹配项(例如,B12=100000),则抛出错误消息。
I am currently using a bunch of "IF.. ElseIf" statements and it is becoming too cumbersome to maintain.我目前正在使用一堆“IF .. ElseIf”语句,并且维护起来变得太麻烦了。
Thanks in advance.提前致谢。
You could just use formula at Cell B13:您可以在单元格 B13 中使用公式:
=IF(B10>A8,"Error",INDEX($A$2:$B$8, SUMPRODUCT(--(A2:A8<=B10)),2))
That's for inclusive (ie Qty 1-9 belongs to level 1) based on the example you given.这是基于您给出的示例的包容性(即数量 1-9 属于第 1 级)。
Try this in the worksheet's private code sheet (right-click worksheet name tab, View Code.在工作表的私有代码表中尝试此操作(右键单击工作表名称选项卡,查看代码。
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(0, 0) = "B12" Then
On Error GoTo meh
Application.EnableEvents = False
Dim m As Variant
If IsNumeric(Target) Then
If Target < 1 Or Target > Application.Max(Range("A2:A8")) Then
Target.Offset(1, 0) = "out of range"
Else
m = Application.Match(Target, Range("A1:A8"))
Target.Offset(1, 0) = Cells(m, "B").Value
End If
End If
End If
meh:
Application.EnableEvents = True
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.