Excel 公式查找表中包含值的最右边的列

发布于 2024-10-13 18:17:46 字数 340 浏览 9 评论 0 原文

我在 Excel 电子表格中有一些数据结构如下:

    A B C D E F

1   1 1 2 x 2 3
2   1 1 1 2 2 3
3   3 3 3 3 4 4

我正在尝试制定一个 Excel 公式,该公式将为我提供此表中最右列的索引,该表中有一个与特定值匹配的单元格。

在此示例中,包含值“1”的最右边的列位于 C 列中。对于“2”,它将是 E。该列索引就是我所追求的。

我使用列字母是为了与 Excel 一致,但最好使用数字列索引。

我已经尝试了一些其他解决方案来解决在线发现的类似 Excel 问题,但它们的行为并不完全正确。

I have some data structured like this in an Excel spreadsheet:

    A B C D E F

1   1 1 2 x 2 3
2   1 1 1 2 2 3
3   3 3 3 3 4 4

I am trying to formulate an Excel formula which will give me the index of the rightmost column in this table which has a cell matching a specific value.

In this example, the rightmost column containing the value '1' is in column C. For '2' it would be E. That column index is what I'm after.

I use column letters to be Excel-consistent, but a numeric column index is preferable.

I have tried some other solutions for similar Excel problems found online, but they don't quite have the right behavior.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

东走西顾 2024-10-20 18:17:46

如果你想使用辅助列,你可以把这个公式放在 G1 中

{=MAX((COLUMN(A1:F1)*(A1:F1=2)))}

输入的数组中。向下填充至G3。在 G4 中,输入

=MAX(G1:G3)

然后对每个数字重复。如果您不需要辅助列,您可以编写一个像这样的 UDF,

Public Function MaxColumn(rInput As Range, vValue As Variant) As Long

    Dim rFound As Range

    Set rFound = rInput.Find(vValue, rInput.Cells(1), xlValues, xlWhole, xlByColumns, xlPrevious)

    If Not rFound Is Nothing Then MaxColumn = rFound.Column

End Function

您可以这样调用

=maxcolumn(A1:F3,2)

If you want to use helper columns, you can put this formula in G1

{=MAX((COLUMN(A1:F1)*(A1:F1=2)))}

That's array entered. Fill down to G3. In G4, put

=MAX(G1:G3)

Then repeat for each number. If you don't want helper columns, you can write a UDF like this

Public Function MaxColumn(rInput As Range, vValue As Variant) As Long

    Dim rFound As Range

    Set rFound = rInput.Find(vValue, rInput.Cells(1), xlValues, xlWhole, xlByColumns, xlPrevious)

    If Not rFound Is Nothing Then MaxColumn = rFound.Column

End Function

Which you call like

=maxcolumn(A1:F3,2)
清君侧 2024-10-20 18:17:46

这是一种用公式来实现的方法。我将展示如何使用几个不同的公式来展示逻辑步骤,然后将它们组合成一个大公式。

首先,每列使用一个公式来查看目标值是否在该列中。例如,在 A 列中:

=COUNTIF(A1:A100,Goal)
=COUNTIF(B1:B100,Goal)
...
(where Goal can be a hardcoded search value,
 or a named range where you type your query)

然后,将 IF 语句添加到这些公式中,将其转换为列号。如果列中存在查询,则显示列号,否则显示零。

=IF(COUNTIF(A1:A100,Goal)>0, 1, 0)
=IF(COUNTIF(B1:B100,Goal)>0, 2, 0)
...

最后,添加一个公式以从先前的公式中获取最大列数。这将等于最右边包含您的查询值的列。

=MAX( IF(COUNTIF(A1:A100,Goal)>0, 1, 0), IF(COUNTIF(B1:B100,Goal)>0, 2, 0), ...)

Here is a way to do it with formulas. I'll show how to do it with a few different formulas to show the steps of the logic, and then put them together into one big formula.

First, use one formula per column to see if the target value is in the column. For example in column A:

=COUNTIF(A1:A100,Goal)
=COUNTIF(B1:B100,Goal)
...
(where Goal can be a hardcoded search value,
 or a named range where you type your query)

Then, add IF statements to these formulas to translate this into column numbers. If the query is present in the column, show the column number, else show zero.

=IF(COUNTIF(A1:A100,Goal)>0, 1, 0)
=IF(COUNTIF(B1:B100,Goal)>0, 2, 0)
...

Finally, add a formula to grab the maximum column number from the prior formulas. This will equal the rightmost column with your query value in it.

=MAX( IF(COUNTIF(A1:A100,Goal)>0, 1, 0), IF(COUNTIF(B1:B100,Goal)>0, 2, 0), ...)
七禾 2024-10-20 18:17:46

怎么样:

Function FindCol(ToFind)
Dim r As Range
Dim rfind As Range
Dim rfound As Range
Set r = ActiveSheet.UsedRange

For i = r.Columns.Count To 1 Step -1
    Set rfind = r.Columns(i)
    Set rfound = rfind.Find(ToFind)
    If Not rfound Is Nothing Then
        Result = rfound.Column
        Exit For
    End If
Next

FindCol = Result

End Function

How about:

Function FindCol(ToFind)
Dim r As Range
Dim rfind As Range
Dim rfound As Range
Set r = ActiveSheet.UsedRange

For i = r.Columns.Count To 1 Step -1
    Set rfind = r.Columns(i)
    Set rfound = rfind.Find(ToFind)
    If Not rfound Is Nothing Then
        Result = rfound.Column
        Exit For
    End If
Next

FindCol = Result

End Function
白鸥掠海 2024-10-20 18:17:46

这是使用新的 LAMBDABYROW 函数的解决方案。

BYROW 查看设定范围内的每一行,并返回目标编号所在的最右侧的列索引号。 IFERROR 用于捕获目标编号不存在的任何错误,MAX 用于返回最大编号/最右侧索引列。

=LET(
goal, $H$1,
range, A1:F3,
MAX(IFERROR(BYROW(range,LAMBDA(x,XMATCH(TRUE,INDEX(x=goal,),0,-1))),0)))

输入图片此处描述

完整教程在这里

Here's a solution using the new LAMBDA and BYROW functions.

The BYROW looks at each line in a set range and returns the right-most column index number where the goal number is present. The IFERROR is used to catch any errors where the goal number isn't present, and the MAX is used to return the highest number/rightmost indexed column.

=LET(
goal, $H$1,
range, A1:F3,
MAX(IFERROR(BYROW(range,LAMBDA(x,XMATCH(TRUE,INDEX(x=goal,),0,-1))),0)))

enter image description here

Full Tutorial Here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文