简体   繁体   English

Excel VBA-循环公式数组并将其传递给变量

[英]Excel VBA - looping formula array and pass it into variables

i'm working on an exercise where i have the following columns 我正在练习以下几列

  • first column for primary key 主键的第一列
  • second column for open date 开放日期的第二列
  • third column for open price 开盘价第三栏
  • fourth column for close date 截止日期第四栏
  • fifth column for close price 收盘价第五栏

The primary key will repeat multiple time due to different open date and close date through the year. 由于一年中的打开日期和关闭日期不同,因此主键将重复多次。 So currently i'm able to use advance filtering function through vba and get an unique record of primary key, but i also need to find the min and max of open date and close date based on each primary key and identify the open price based on min of open date and closed price based on max of closed date. 因此,目前,我能够通过vba使用高级过滤功能并获得主键的唯一记录,但是我还需要根据每个主键找到打开日期和关闭日期的最小值和最大值,并根据打开日期和收盘价的最小值(基于关闭日期的最大值)。

can anyone help? 有人可以帮忙吗? because i can solve this with array formula within excel but no luck of converting each array formula into vba and pass it to variables. 因为我可以在excel中使用数组公式解决此问题,但是没有将每个数组公式转换为vba并将其传递给变量的运气。

sample list: 样本清单:

样品清单

expected result: 预期结果:

预期结果

array formulas used: 使用的数组公式:

=MIN(IF(A1:A70926=J2,B1:B70926)) = MIN(IF(A1:A70926 = J2,B1:B70926))

=INDEX(C:C,MATCH(1,(J2=$A:$A)*(M2=$B:$B),0)) = INDEX(C:C,MATCH(1,(J2 = $ A:$ A)*(M2 = $ B:$ B),0)))

=MAX(IF($A:$A=J2,$B:$B)) = MAX(IF($ A:$ A = J2,$ B:$ B))

=INDEX(F:F,MATCH(1,(J2=$A:$A)*(O2=$B:$B),0)) = INDEX(F:F,MATCH(1,(J2 = $ A:$ A)*(O2 = $ B:$ B),0))

I am not entirely clear on which max and mins you want but provided you convert your date strings to actual dates, you can use a pivottable to get any combination of max, mins from the date eg 我尚不清楚您想要的最大和最小值,但是如果您将日期字符串转换为实际日期,则可以使用数据透视表从日期中获取最大,最小值的任意组合,例如

例

With the above: 具有以上内容:

Helper column F has the formula in F2, which autofills down as source is set up as Excel table. Helper列F在F2中具有公式,当源设置为Excel表时,该公式将自动填充。

=DATE(LEFT(B2,4),MID(B2,5,2),RIGHT(B2,2))

This generates an actual date. 这将生成一个实际日期。

Pressing Ctrl + T , with a cell selected in the range, convertes the data into an Excel table, then Alt + N + V generates a pivottable from that table. 按下Ctrl + T并在范围内选择一个单元格,将数据转换为Excel表,然后Alt + N + V从该表生成数据透视表。

Then arrange as you wish. 然后根据需要安排。 For example, with date in the rows area you can easily then select max and min dates via filter. 例如,在行区域中使用日期,您可以轻松地通过过滤器选择最大和最小日期。 You can have ticker either as a page field filter or add to the rows in front of the converted date. 您可以将股票行情指示器用作页面字段过滤器,也可以将其添加到转换日期之前的行中。 And then add open and closed fields to the values area. 然后将打开和关闭的字段添加到值区域。 You can add the same field more than once. 您可以多次添加同一字段。 Make sure to right click on the first time you add and do value > field settings > min 确保在第一次添加时右键单击并执行值>字段设置>最小值

值字段设置

敏

Repeat for adding the same field and set as max. 重复此操作以添加相同的字段并设置为最大值。

Close up on pivot: 紧靠枢轴:

枢


If you want the all time max and min value, simply remove the date field from the rows: 如果要获得所有时间的最大值和最小值,只需从行中删除日期字段即可:

例


If you want for a particular year, when date field is in the rows area, right click and use the date filters functionality: 如果要使用特定年份,则当日期字段位于行区域中时,右键单击并使用日期过滤器功能:

日期过滤器

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

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