简体   繁体   English

Excel:使用多单元格数组公式将函数应用于一系列范围

[英]Excel: Applying functions to a range of ranges using multi-cell array-formula

Can you apply a builtin function that takes a range (eg AVERAGE()), and have it apply to a multiple ranges? 您可以应用一个带有范围的内置函数(例如AVERAGE()),并将其应用于多个范围吗?

I'm currently finishing up a pretty basic data-analysis unit with a small Excel segment and I had a past assignment that involved analysing some housing data. 我目前正在用一个很小的Excel段完成一个相当基本的数据分析单元,过去的工作涉及分析一些房屋数据。

One task involved averaging and rounding the residential housing prices of a few cities for each time period. 一项任务涉及在每个时间段内平均和舍入几个城市的住宅房价。

The target data is in the worksheet Residential_Houses , formatted such that cities are columns and time periods are rows spanning: C7:J30 . 目标数据在工作表Residential_Houses ,其格式设置为城市为列,时间段为行,范围为: C7:J30 With the goal to have the averages of each row in the worksheet Analysis spanning C7:C30 . 目的是使工作表Analysis中每一行的平均值跨越C7:C30

My simple solution was to write the formula: 我的简单解决方案是编写公式:

=ROUND(AVERAGE(Residential_Houses!C7:J7), 2)

Into Analysis!C7 and then used Excel's "AutoFill" feature to generate the formulae for Analysis!C8:C30 . 进入Analysis!C7 ,然后使用Excel的“自动填充”功能生成Analysis!C8:C30的公式。 This worked fine, and the results were correct but I lost marks for not explicitly using cell array formula. 这工作正常,结果是正确的,但由于未明确使用单元格数组公式而丢失了分数。

Instead the sample answer I was given was this monolithic line: 取而代之的是,我得到的示例答案是这行代码:

{=ROUND((Residential_Property!C7:C30+Residential_Property!D7:D30+Residential_Property!E7:E30+Residential_Property!F7:F30+Residential_Property!G7:G30+Residential_Property!H7:H30+Residential_Property!I7:I30+Residential_Property!J7:J30)/8,2)}

It seems clear that the intent of this code is to take the average of columns CJ for each row 7-30, but it avoids using the AVERAGE() builtin. 显然,此代码的目的是取每行7-30的CJ列的平均值,但避免使用内置的AVERAGE()。 This works, but it's cumbersome, and wouldn't scale if there were more than a few columns in this test data. 这可以工作,但是很麻烦,并且如果此测试数据中的列数多,则无法扩展。

The crux of my question here is how can you combine functions that already operate on ranges , with cell array formula to process multiple ranges with a single formula? 我的问题的症结在于,如何将已经对range操作的函数单元格数组公式结合起来,以单个公式处理多个范围?

My intuition tells me it might be something like this: 我的直觉告诉我,可能是这样的:

{=AVERAGE((C7:J7):(C30:J30))}

Or something involving indirect addressing, but I can't seem to find any detailed information on the topic. 或涉及间接寻址的东西,但我似乎找不到有关该主题的任何详细信息。 All of the array formula resources I've found so far have been limited to basic arithmetic and single dimension ranges. 到目前为止,我发现的所有数组公式资源都限于基本算术和一维范围。 Could someone point me in the right direction here, or is this just not possible with array formula? 有人可以在这里向我指出正确的方向,还是使用数组公式无法做到这一点?

EDIT : For anyone who stumbles upon this in the future. 编辑 :对于将来偶然发现此问题的任何人。 I chose Barry's solution because it helped me understand how to generalise this concept (replacing the range with a function that can slice a range). 我选择Barry的解决方案是因为它帮助我理解了如何概括这个概念(用可以分割范围的函数代替范围)。 But as Scott notes, OFFSET() is volatile; 但是正如Scott所说的,OFFSET()是易失的。 so INDEX() is a better way to do this if you're working with large datasets. 因此,如果使用大型数据集,则INDEX()是实现此目的的更好方法。

Here is a formula that can be entered as one array formula and get results. 这是一个公式,可以将其作为一个数组公式输入并获取结果。 It can also be entered normally and copied down. 也可以正常输入并复制下来。

=ROUND(AVERAGE(INDEX($C$7:$J$30,ROW()-MIN(ROW($C$7:$J$30))+1,0)),2)

在此处输入图片说明

Similar to Scott's suggestion, SUBTOTAL can process an array of ranges created by an OFFSET function, so with the first argument of SUBTOTAL as 1 (for AVERAGE ) this formula will give an array of rounded averages for each row 与Scott的建议类似, SUBTOTAL可以处理由OFFSET函数创建的范围数组,因此,在SUBTOTAL的第一个参数为1时(对于AVERAGE ),该公式将为每行给出一个舍入平均值的数组

=ROUND(SUBTOTAL(1,OFFSET(C7:J30,ROW(C7:J30)-ROW(C7),0,1)),2)

entered in a vertical range and confirmed with CTRL + SHIFT + ENTER 输入垂直范围并用CTRL + SHIFT + ENTER确认

or you can use the same OFFSET inside an AVERAGE function like this: 或者您可以在AVERAGE函数中使用相同的OFFSET如下所示:

=ROUND(AVERAGE(OFFSET(C7:J30,ROW(C7:J30)-ROW(C7),0,1)),2)

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

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