Excel/VBA - 使用动态范围的索引匹配函数

标签 excel vba excel-formula excel-match

如何在 VBA 中有效使用Index/Match公式?

背景:我有一个工作表,该工作表在很大程度上依赖于公式的使用,该公式根据将特定名称与其名称范围以及特定日期与其日期范围相匹配来检索输出。

=INDEX(OutputRange,MATCH(1,(Name=NameRange)*(Date=DateRange),FALSE),1)

此外,还有一个硬编码的 VBA 子程序可以产生相同的输出

Sub ExampleHardCode() 
Dim Result As Variant
  Result = Evaluate("INDEX($C$4:$C$13,MATCH(1,($G$6=$A$4:$A$13)*($G8=$B$4:$B$13),FALSE),1)")
  ActiveCell.Value = Result
End Sub

问题:我想生成一个函数,它返回与上述选项相同的输出,但允许用户(i)通过引用相应的单元格来选择名称和日期值,以及(ii)选择每个范围(名称范围) 、日期范围和输出范围)。本质上是在 Excel 中使用=examplefunction(名称值、名称范围、日期值、日期范围、输出范围)。

我尝试了多种不同的解决方案,但没有成功。下面是我迄今为止尝试过的示例,我认为匹配部分存在问题,因为即使我尝试设置范围(使用硬编码范围),它也会返回错误。

Function TestIndexMatch1(NameVal As Variant, DateVal As Date)

Dim NameRng As Range
Dim DateRng As Range
Dim OutputRng As Range
Dim Var1 As Variant  'should this be a range or integer?
Dim Result As Variant 

Set NameRng = Range("$A$4:$A$13")
Set DateRng = Range("$B$4:$B$13")
Set OutputRng = Range("$C$4:$D$13")

With Application.WorksheetFunction
    Var1 = .Match(1, (NameVal = NameRng) * (DateVal = DateRng), False)
    Result = .Index(OutputRng, Var1, 1)
End With
End Function

我有一个示例工作簿,如果有帮助我可以分享。我不确定这是否可行,但如果是这样,它确实可以帮助很多对 Excel 不太熟悉的用户正确使用索引/匹配 Excel 公式。不幸的是,对我来说,我的 Excel 技能远远超过了我的 VBA 技能。

最佳答案

要在 VBA 代码中使用数组公式将应用程序对象的 ReferenceStyle 设置为 xlR1C1(暂时,仅在执行函数期间)。最后调用Evaluate得到公式的结果。

Private Const TEMPLATE As String = "=INDEX({0},MATCH(1,({1}={2})*({3}={4}),{5}))"
Private Const MATCH_TYPE = 0

Public Function TestIndexMatch1(ByRef outputRange As Range, _
                                ByRef nameCriteria As Range, _
                                ByRef dateCriteria As Range, _
                                ByRef nameRange As Range, _
                                ByRef dateRange As Range)

    On Error GoTo Err_Handler
    Err.Number = 0

    Dim originalReferenceStyle
    originalReferenceStyle = Application.ReferenceStyle
    Application.ReferenceStyle = xlR1C1

    Dim myFormula As String
    myFormula = Replace(TEMPLATE, "{0}", outputRange.Address())
    myFormula = Replace(myFormula, "{1}", nameCriteria.Address())
    myFormula = Replace(myFormula, "{2}", nameRange.Address())
    myFormula = Replace(myFormula, "{3}", dateCriteria.Address())
    myFormula = Replace(myFormula, "{4}", dateRange.Address())
    myFormula = Replace(myFormula, "{5}", MATCH_TYPE)

    TestIndexMatch1 = Application.Evaluate(myFormula)

Err_Handler:
    If (Err.Number <> 0) Then MsgBox Err.Description
    Application.ReferenceStyle = originalReferenceStyle
End Function

所以它看起来在工作表上:

enter image description here

enter image description here

关于Excel/VBA - 使用动态范围的索引匹配函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16176726/

相关文章:

excel - 使用 VBA 宏 + 按钮在 Excel 工作表中前后移动,按顺序隐藏工作表会破坏功能

python - 使用Python的xlrd模块查找日期最多的列

excel - 如果单元格中添加了某些内容,则添加 "Now"

java - 在 Apache Poi 3.7 中以特定格式在数字单元格中写入 Double 值

vba - 如何从 VBA 标准库中重载 IsEmpty() 等函数?

excel - 获取具有另一列过滤范围的唯一列表

Excel 问题回复 : Method for 'Finding and Replacing' Multiple Values Within Single Cells

python - XlsxWriter write_formula() 不工作总是显示零 (0)

excel - 简单的excel添加两个单元格并按字母顺序显示

VBA SUM 函数计算第 n 行中的多个值