[英]Excel, Trying Index, Match, to lookup values with VBA
I am trying to look-up values at cell locations using VBA, I have searched google and all over stackoverflow before asking this because I can't seem to get it to work. 我正在尝试使用VBA在单元格位置查找值,在询问之前我已经搜索了Google以及整个stackoverflow,因为我似乎无法使其正常工作。
The following is the code I am trying to use, note that budgetcode references a cell containing one of the codes in the first column and mo references a cell that contains a number (1-12) or contains a short code (ytd, 1qtr, 2qtr, 3qtr). 以下是我要使用的代码,请注意,budgetcode引用包含第一列中一个代码的单元格,mo引用包含数字(1-12)或包含短代码(ytd,1qtr, 2qtr,3qtr)。 Example is that I want to pull CAD-NS for February (2), and I should get 5666.40. 例如,我想在2月(2)期间提取CAD-NS,我应该得到5666.40。
Function eBudgetl(budgetcode As String, mo As String)
eBudgetl = Application.WorksheetFunction.Index(Range("Budget!G1:X5000"), _
Application.WorksheetFunction.Match(budgetcode, Range("Budget!B1:B5000"), 0), _
Application.WorksheetFunction.Match(mo, Range("Budget!G1:X1"), 0))
End Function
Here is part of the data I wish to lookup: 这是我要查找的部分数据:
1 2 3 4
CAD-NS I Net Sales 5264.0 5666.4 5614.9 5966.6
COSMAT E Material 6207.5 3660.0 3661.9 3560.9
COSDL E Direct Labor 610.4 105.3 167.1 123.6
CAD-MOIL E Indirect Labor 671.2 163.4 181.6 161.7
CAD-MOSAL E Salary Overhead 601.0 106.0 101.0 101.0
Here is the code in the cell that works, but I need to do in VBA. 这是起作用的单元格中的代码,但是我需要在VBA中进行操作。 (The reason I need to do in vba is sometimes the budgetcode will contain 2+ references separated by a comma and I am going to use vba to separate them and look each up independently.) (我需要在vba中执行此操作的原因是,有时预算代码将包含2个以上的引用,并以逗号分隔,而我将使用vba对其进行分隔,并分别进行查找。)
=INDEX(Budget!$G$1:$X$5000,MATCH($F12,Budget!$B$1:$B$5000,0),MATCH(AN$1,Budget!$G$1:$X$1,0))
I appreciate any help very much, I have been at this for 3 days now. 非常感谢您的帮助,我已经参加了3天。
Thanks, 谢谢,
Enoch 以诺
The issue is the Function
parameter types. 问题是Function
参数类型。
When you call your Function with mo
= cell AN1
, containing the number 1
, the Function type casts it to the String "1"
, which doen't exisit in the range Budget!$G$1:$X$1
, since these are also numbers . 当你调用你的功能mo
=细胞AN1
,包含数字 1
,功能类型它转换为字符串 "1"
,在其范围由doen't exisit Budget!$G$1:$X$1
,因为这些也是数字 。
The solution is to use Variant
as the function paramters type. 解决方案是使用Variant
作为函数参数类型。
To make debugging this type of error easier, try not to do too much in a single line of code. 为了使调试这种类型的错误更加容易,请尝试不要在一行代码中做太多事情。 Splitting the line into 2 x Match
functions and an Index
, would allow you to see the second match return an error. 将行拆分为2 x Match
函数和Index
,将使您看到第二个匹配返回错误。
Couple of other points: 其他几点:
eBudgetl
would not automatically recalculate when any of their data changes. 按照书面说明,对eBudgetl
的调用在其任何数据更改时都不会自动重新计算。 Application
object has a version of Index
and Match
so the WorksheetFunction
calls are not required Application
对象具有Index
和Match
的版本,因此不需要WorksheetFunction
调用 Refactored version to demonstrate: 重构版演示:
Function eBudgetl(rData As Range, rBudCode As Range, rMo As Range, budgetcode As Variant, mo As Variant)
Dim rw As Variant
Dim col As Variant
With Application
col = .Match(budgetcode, rBudCode, 0)
rw = .Match(mo, rMo, 0)
eBudgetl = .Index(rData, col, rw)
End With
End Function
Called as 称为
=eBudgetl(Budget!$G$1:$X$5000,Budget!$B$1:$B$5000,Budget!$G$1:$X$1,$F12,AN$1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.