简体   繁体   English

在公式中使用索引、匹配和行/列 excel 函数返回一个子集数组,其中仅包含带文本的单元格并排除空单元格

[英]Use index, match, and row/column excel functions in a formula to return a subset array with just cells with text and exclude the empty ones

I have this scenario where i want to extract a subset of a range in one row (using index, match and row functions preferably ) and return only smaller filtered array with cells that contain text/data and exclude empty cells.. I want to use this filtered array for further processing我有这种情况,我想在一行中提取范围的子集(最好使用索引、匹配和行函数)并只返回较小的过滤数组,其中包含包含文本/数据并排除空单元格的单元格。我想使用这个过滤后的数组用于进一步处理

The input 2-D array i want to get a subset array from is colored我想从中获取子集数组的输入二维数组是彩色的

The image shows the returned array size in last column该图显示了最后一列中返回的数组大小

A dropdown menu is provided for the user to select the desired row (say A1:A6).为用户提供了一个下拉菜单,用于 select 所需的行(例如 A1:A6)。

The row selected by the user determines which row of the 2-D colored range (say B1:G6 ) to extract the filtered array from then only cells with text/data are returned from that row用户选择的行确定要从中提取过滤数组的二维彩色范围(例如 B1:G6 )的哪一行,然后仅从该行返回带有文本/数据的单元格

I tried the following code我尝试了以下代码

I can get the array of whole correct row (all cells) using我可以使用获得整个正确行(所有单元格)的数组

index(B1:G6, match(A1:A6,value_from_dropdown_menue,0),0)

then i tried to apply row() function to filter out empty elements from that returned array.. lets refer to the returned_full_row_array as RFRA for readability然后我尝试应用 row() function 从返回的数组中过滤出空元素。为了便于阅读,让我们将返回的_full_row_array 称为RFRA

index(RFRA, row(indirect("1:"& counta(RFRA))))

the full formula looks like this ( B8 is the cell with dropdown )完整的公式看起来像这样( B8是带有下拉菜单的单元格)

=INDEX(INDEX(2D_Matrix,MATCH(B8,1D_Menue_List,0),0),ROW(indirect("1:"& counta(INDEX(2D_Matrix,MATCH(B8,1D_Menue_List,0),0)))))

but the row() function is not returning an array and thus i don't any array returned.. i only get the first element.. even if i hardcoded row(1:3).. i don't get 3 element array back... only the first element of the the whole row但是 row() function 没有返回数组,因此我没有返回任何数组..我只得到第一个元素..即使我硬编码行(1:3)..我没有得到 3 元素数组返回...只有整行的第一个元素

any ideas?有任何想法吗?

Edit: I can successfully get the whole row of my range using index match the result is a fixed size 1 by 6 array like this (example first and 2nd row ):编辑:我可以使用索引匹配成功地获得我的范围的整行结果是一个固定大小的 1 x 6 数组,如下所示(例如第一行和第二行):

for 1st row >> {"M1-item1",0,0,0,0,0}
for 2nd row >> {"M2-item1","M2-item2","M2-item3","M2-item4",0,0}

desired output however should be a variable length array without the empty/zero elements:所需的 output 但是应该是没有空/零元素的可变长度数组

for 1st row >> {"M1-item1"}
for 2nd row >> {"M2-item1","M2-item2","M2-item3","M2-item4"}

This is a classic, painful problem.这是一个经典而痛苦的问题。 I like to solve it by firstly moving all the elements in the range into a single column, like this:我喜欢通过首先将范围内的所有元素移动到单个列中来解决它,如下所示:

=LET(
     InputArray,
     B1:G8,
     InputRowCount,
     ROWS(InputArray),
     TotalCellCount,
     COLUMNS(InputArray)*InputRowCount,
     LookupRow,
     MOD(SEQUENCE(TotalCellCount,,0),InputRowCount),
     LookupColumn,
     (SEQUENCE(TotalCellCount,,0)-LookupRow)/InputRowCount,
     RearrangedAsColumn,
     INDEX(InputArray,LookupRow,LookupColumn),
     RearrangedAsColumn_IsBlank,
     INDEX(ISBLANK(InputArray),LookupRow,LookupColumn),
     Result,
     FILTER(RearrangedAsColumn,1-RearrangedAsColumn_IsBlank),
     Result
)

I used a LET function here, because it allows me to define variables, and makes the explanation a little clearer.我在这里使用了 LET function,因为它允许我定义变量,并使解释更清晰一些。

Stepping through: |步入:| Name |姓名 | What it does |它的作用 | | | ---- | ---- | ------------ | ------------ | | | InputArray | InputArray | That's where the data comes from |这就是数据的来源| | | InputRows | InputRows | Count up the rows |数行 | | | InputColumns | InputColumns | Count up the columns |数列| | | LookupRow | LookupRow | This uses division remainders to give us the row references |这使用除法余数给我们行引用 | | | LookupColumn | LookupColumn | This calculates our column lookup |这计算了我们的列查找 | | | RearrangedAsColumn | RearrangedAsColumn | This restates InputArray into a single column |这将 InputArray 重述为单列 | | | RearrangedAsColumn_IsBlank | RearrangedAsColumn_IsBlank | Gives us a column that shows whether or not the cell is blank |给我们一个显示单元格是否为空白的列 | | | Result | Result | Finally, filter RearrangedAsColumn for non-blank cells |最后,为非空白单元格过滤 RearrangedAsColumn |

I hope this is what you're after!我希望这就是你所追求的!

Given that the original question has changed, this is a much simpler problem.鉴于最初的问题已经改变,这是一个更简单的问题。 Let's say that we have some named ranges:假设我们有一些命名范围:

Named Range命名范围 Formula公式 Comment评论
FullRange =L15:Q22 The full range of the data全方位的数据
MenuList =K15:K22 The list of menu items菜单项列表
DropdownSelection ? ? The cell with the dropdown list带有下拉列表的单元格

This would be the formula to do the job:这将是完成这项工作的公式:

=INDEX(
    FullRange,
    XMATCH(DropdownSelection,MenuList,0),
    XMATCH(
        SEQUENCE(
            ,
            MAX(
                MMULT(
                    SEQUENCE(,ROWS(FullRange),1,0),
                    MMULT((1-ISBLANK(FullRange))*(DropdownSelection=MenuList),
                    (SEQUENCE(COLUMNS(FullRange))<=SEQUENCE(,COLUMNS(FullRange)))*1)
                )
            )
        ),
        MMULT(
            SEQUENCE(,ROWS(FullRange),1,0),
            MMULT(
                (1-ISBLANK(FullRange))*(DropdownSelection=MenuList),
                (SEQUENCE(COLUMNS(FullRange))<=SEQUENCE(,COLUMNS(FullRange)))*1
            )
        ),
        0
    )
)

Sorry it's so ugly in the middle.对不起,中间太丑了。 It uses ISBLANK to find which cells are used.它使用ISBLANK来查找使用了哪些单元格。

In particular, this part:特别是这部分:

SEQUENCE(COLUMNS(FullRange))<=SEQUENCE(,COLUMNS(FullRange)))*1

produces an upper triangular matrix.产生一个上三角矩阵。

If we have a matrix that contains this:如果我们有一个包含这个的矩阵:

(1-ISBLANK(FullRange))*(DropdownSelection=MenuList)

it will contain only those cells we actually want.它将只包含我们真正想要的那些单元格。

By taking the matrix product, we get a matrix with 0's everywhere except on the row we're after.通过矩阵乘积,我们得到一个除了我们后面的行之外到处都是 0 的矩阵。 On the row we're after, it has 1 on the first cell we want up to the 2nd cell, 2 on the second cell we want up to the third, and so on.在我们之后的行上,我们想要的第一个单元格有 1 到第二个单元格,我们想要的第二个单元格有 2 到第三个单元格,依此类推。 By XMATCH ing from the left (going right), it will only match on those cells we want.通过XMATCH从左边(向右),它只会匹配我们想要的那些单元格。

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

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