使用OFFSET从矩阵中取值

    技术2022-05-11  11

    这是我前段时间在sapkevin.cublog.cn上写的博客,现转载在此。

    人事部的同事找我让我帮忙作个公式,以便她从一个矩阵中按要求取出一个需要的值。

    矩阵见下图,其中的数据为举例。

    取值区域为

    要求是在Score一列,按前两列的要求在上面的矩阵中取出对应的数据,其中Grade列中的A、B、C等是按精确值进行匹配。而Percent列的百分比是按模糊值进行匹配,即按高值执行,如5%按10%走,32%按40%走,以此类推。

    取值的结果是应该下图。

    人事部同事是看到别的公司这么用的,所以她们也想参照作,但由于保密的原因,拿不到具体的公式,那家公司告诉是用MATCH公式作的。

    我听完了全部要求,我的第一感觉是MATCH可以作,但我暂时想不出具体方法。我改了一下思路,决定用OFFSET+LOOKUP作。

    具体的方法如下:

    1、 因为我们想在这个矩阵中取值,就需要知道符合条件的那个单元格的位置。

    2、 为了知道这个位置,我们需要对表格作些处理。我加了一行(第2行)和一列(B列)。见下图。

    3、 在对应的数据行和列上填写1、2、3等,详见图中行和列的内容。

    4、 填写这些数据的意义是我们要将取值的条件转化为相应的数值。这些数值用来表达你所要找的单元格的位置。

    5、 对于列中的A、B、C、D、E,我们用HLOOKUP函数来取出第2行的数值。公式为HLOOKUP($D16,$1:$2,2,FALSE),其中的含义是根据D16的单元格数据,在第1行到第2行之间查找,找到符合条件的后返回第2行的数据,最后的FALSE表示按精确值进行查找。

    6、 对于行中的百分比数据,我们用VLOOKUP函数来取出第2列的数值。公式为VLOOKUP($C16,$A:$B,2,TRUE),其中的含义是根据C16的单元格数据,在第1列到第2列之间查找,找到符合条件的后返回第2列的数据,最后的TRUE表示按模糊值进行查找。EXCEL的模糊查找规则是向上一档靠,即5%这个数值,在0%和10%这两档中会自动靠到0%这一档,所以为满足我们的要求,在公式中我给加了个1,就相当于向下靠到10%这一档。

    7、 有了这两个行和列的数据,我们就可以找到符合条件的单元格。

    8、 我们先确定一个基准单元格,在这里以A1单元格为基准单元格,这样就会找到一个规律,在查找的行和列数值上再加1,就是相对于基准单元格的偏移量。如以5%和A为例,我们找到的列值是2,行值是1,而结果6所在的C4单元格,相当于基准单元格A1的偏移量是3和2。可以这样理解:即以A1单元格为基准,向下走3个单元格,再向右走2个单元格,就是我们要找到的单元格C4。

    9、 下一步,就需要用OFFSET的公式了。

    10、 OFFSET的用法简单叙述如下: OFFSET(reference,rows,cols,height,width) 简单理解就是 reference是基准单元格的位置。 rows是行偏移量。 Cols是列偏移量。 height和width目标区域的高和宽,如果我们取值是一个单元格的话,这两个值缺省值就是1。

    11、 有了上面的简单理解,我们就可以应用公式了。仍以5%和A为取值条件。公式就是OFFSET($A$1,2+1,1+1,1,1)其中的带下划线的粗体2和1就是我们上面找出的行和列偏移量。其结果就是C4单元格中的6。

    12、 通用公式就是 OFFSET($A$1,VLOOKUP($C16,$A:$B,2,TRUE)+2,HLOOKUP($D16,$1:$2,2,FALSE)+1,1,1) 这个公式是应用于E16单元格的,其它单元格的公式复制即可。

    13、 问题解决完成,详细的OFFSET的HELP内容请见下面内容(我用的是英文版的OFFICE,所以帮助内容是英文的,你可以参照中文版的帮助文件看)。

    OFFSET

    Returns a reference to a range that is a specified number of rows and columns from a cell or range of cells. The reference that is returned can be a single cell or a range of cells. You can specify the number of rows and the number of columns to be returned.

    Syntax

    OFFSET(reference,rows,cols,height,width)

    Reference    is the reference from which you want to base the offset. Reference must refer to a cell or range of adjacent cells; otherwise, OFFSET returns the #VALUE! error value.

    Rows    is the number of rows, up or down, that you want the upper-left cell to refer to. Using 5 as the rows argument specifies that the upper-left cell in the reference is five rows below reference. Rows can be positive (which means below the starting reference) or negative (which means above the starting reference).

    Cols    is the number of columns, to the left or right, that you want the upper-left cell of the result to refer to. Using 5 as the cols argument specifies that the upper-left cell in the reference is five columns to the right of reference. Cols can be positive (which means to the right of the starting reference) or negative (which means to the left of the starting reference).

    Height    is the height, in number of rows, that you want the returned reference to be. Height must be a positive number.

    Width    is the width, in number of columns, that you want the returned reference to be. Width must be a positive number.

    Remarks

    If rows and cols offset reference over the edge of the worksheet, OFFSET returns the #REF! error value.If height or width is omitted, it is assumed to be the same height or width as reference.OFFSET doesn't actually move any cells or change the selection; it just returns a reference. OFFSET can be used with any function expecting a reference argument. For example, the formula SUM(OFFSET(C2,1,2,3,1)) calculates the total value of a 3-row by 1-column range that is 1 row below and 2 columns to the right of cell C2.

    Example

    The example may be easier to understand if you copy it to a blank worksheet.

    How?

    Create a blank workbook or worksheet. Select the example in the Help topic. Do not select the row or column headers.

    Selecting an example from Help

    Press CTRL+C. In the worksheet, select cell A1, and press CTRL+V. To switch between viewing the results and viewing the formulas that return the results, press CTRL+` (grave accent), or on the Tools menu, point to Formula Auditing, and then click Formula Auditing Mode.

    1

    2

    3

    4

    A

    B

    Formula

    Description (Result)

    =OFFSET(C3,2,3,1,1)

    Displays the value in cell F5 (0)

    =SUM(OFFSET(C3:E5,-1,0,3,3))

    Sums the range C2:E4 (0)

    =OFFSET(C3:E5,0,-3,3,3)

    Returns an error, because the reference is not on the worksheet (#REF!)


    最新回复(0)