简体   繁体   English

使用数组公式将具有多个条件的索引匹配转换为VBA

[英]Index Match with multiple criteria conversion to VBA using an array formula

I have a table from which I am trying to retrieve data based on multiple criteria. 我有一张要尝试根据多个条件检索数据的表。 I am using Index Match for this. 我为此使用索引匹配。 If I manually paste the formula in excel it works in both the below cases. 如果我在excel中手动粘贴公式,则在以下两种情况下都可以使用。

First I entered the Array formula using the full column range from the raw table in the Index Match formula 首先,我使用Index Match公式中原始表中的完整列范围输入Array公式

    Selection.FormulaArray = _
    "=IF(ISNA((INDEX(Data!C2:C8,MATCH(1,(Data!C2=RC2)*(Data!C3=RC3)*(Data!C4=R3C)*(Data!C5=R4C),0),4))),""F"",(INDEX(Data!C2:C8,MATCH(1,(Data!C2=RC2)*(Data!C3=RC3)*(Data!C4=R3C)*(Data!C5=R4C),0),4)=R4C))"

This works but very runs very slow for large data. 这可以工作,但是对于大数据来说运行非常慢。 Hence I have modified the formula for a specific range from the table eg 30000 rows. 因此,我修改了表中特定范围(例如30000行)的公式。 This works when I enter it manually in excel with row and column references, this also reduces the processing time considerably. 当我在Excel中使用行和列引用手动输入它时,此方法有效,这也大大减少了处理时间。 But throws an error when I try to use it through the code in VBA using the R1C1 style. 但是,当我尝试通过R1C1样式在VBA中的代码中使用它时,会引发错误。 Below is the sample of the code with range for 40000 rows 下面是范围为40000行的代码示例

    Selection.FormulaArray = _
    "=IF(ISNA((INDEX(Data!R2C2:R40000C8,MATCH(1,(Data!R2C2:R40000C2=R[-1]C2)*(Data!R2C3:R40000C3=R[-1]C3)*(Data!R2C4:R40000C4=R3C)*(Data!R2C5:R40000C5=R4C),0),4))),""F"",(INDEX(Data!R2C2:R40000C8,MATCH(1,(Data!R2C2:R40000C2=R[-1]C2)*(Data!R2C3:R40000C3=R[-1]C3)*(Data!R2C4:R40000C4=R3C)*(Data!R2C5:R40000C5=R4C),0),4)=R4C))"

the error shown is 显示的错误是

"Run time error 1004. Unable to set the Formula Array property of the range class." “运行时错误1004。无法设置范围类别的公式数组属性。”

I feel the error is due to the style of syntax on how it should be entered. 我感觉到错误是由于应如何输入语法的风格所致。 Thanks in advance for the support 预先感谢您的支持

The easiest way is the following: 最简单的方法如下:

  1. Write the formula in Excel; 在Excel中编写公式;
  2. Go to the Developer tab and click "Record a macro"; 转到“开发人员”标签,然后点击“记录宏”;
  3. Before doing point 2, click on this small square: 在执行第2点之前,请点击以下小方块:

在此处输入图片说明

  1. Then press a few times the cell with the formula, it will be "translated"; 然后按几次带有公式的单元格,它将被“翻译”;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM