简体   繁体   English

如何使Excel仅考虑具有给定值的行

[英]How to make Excel consider ONLY rows that have a given value

Here is an image, followed by description of data 这是一张图片,后面是数据说明 在此处输入图片说明

Description of Columns: 列说明:

  • Column A (Key) is strictly increasing sequence of decimals A列(键)严格按小数顺序排列
  • Column B (Group) represents a group the value in A belongs to. B列(组)表示A中的值所属的组。
  • Column C (Data) is assorted data C列(数据)是各种数据

Inputs (in column F) 输入(在F列中)

  • Exact Group number in ie {1, 2, 3, 4} in F4 F4中的确切组号,即{1、2、3、4}
  • A decimal value (unrestricted), call it DecimalValue , in F5 一个十进制值(无限制),在F5 DecimalValueDecimalValue

Task 任务

Find row that belongs to the given Group , where ABS( Key - DecimalValue ) value is minimized. 查找属于给定Group ,其中ABS( Key - DecimalValue )值被最小化。 Return Data from that row. 从该行返回Data

  • Ideally looking for an Excel-only solution, using INDEX, VLOOKUP, ABS, and the like. 理想情况下,使用INDEX,VLOOKUP,ABS等寻找仅Excel的解决方案。

This question is similar to my previous question, but different enough (involves a new Group column), where via comments it was determined that it is best to ask a new question, rather than try to update / modify the existing question: 这个问题与我之前的问题类似,但是又有足够的不同(涉及新的“ Group列),在该问题中,通过注释确定最好提出一个新问题,而不是尝试更新/修改现有问题:

Display Row Values based on Nearest Numeric Match of Key 根据键的最近数字匹配显示行值

Adding correction for Group column, if it is possible is what I am after, hence the title reflects that concern. 如果可能的话,请为“ Group列添加更正,因此标题反映了该关注。

(Incomplete Solution - does not consider Group column) (不完整的解决方案-不考虑“ Group列)

=INDEX(C4:C33,MATCH(MIN(ABS(A4:A33-F5)),ABS(A4:A33-F5),0))

Give this a go...it seems to work on my test sheet. 尝试一下...似乎可以在我的测试表上使用。 Be sure to adjust the ranges to suit your situation. 确保调整范围以适合您的情况。 Again, this is an array formula and needs to be confirmed with Ctrl+Shift+Enter: 同样,这是一个数组公式,需要通过Ctrl + Shift + Enter进行确认:

=INDEX(C2:C7,MATCH(MIN(ABS((B2:B7=F4)*A2:A7-F5)),ABS((B2:B7=F4)*A2:A7-F5),0),0)

It works by zeroing out keys that don't match your group assignment (that's the (B2:B7=F4)*A2:A7-F5) part. 它通过将与您的组分配不匹配的键归零(即(B2:B7 = F4)* A2:A7-F5)起作用。 So only keys w/ valid groups have some number to be used to match to the data column. 因此,只有带有有效组的键才有一些数字可用于与数据列匹配。

Hope that helps explain it. 希望能帮助解释它。 You can also utilize the "Evaluate Formula" function on the Formulas toolbar to see it in action. 您还可以使用“公式”工具栏上的“求值公式”功能来查看实际效果。

Note - it seems to return the 1st data value if 0 is used as the closest value (regardless of group selection). 注意-如果将0用作最接近的值,则似乎返回第一个数据值(与组选择无关)。 Not sure how to get around that... 不确定如何解决...

Here is sous2817's answer adjusted to avoid the problem of getting the wrong result when entering 0: 以下是sous2817的答案的调整,以避免输入0时得到错误结果的问题:

=INDEX(Data,MATCH(MIN(IFERROR(ABS(IF(GroupNo=Group,1," ")*Key-DecimalValue),
" ")),IFERROR(ABS(IF(GroupNo=Group,1," ")*Key-DecimalValue)," "),0))

The explanation for how it works is the same. 其工作原理的解释是相同的。 The problem is avoided by replacing zeroes with errors. 通过用错误替换零来避免该问题。

Note that Key is the key column, GroupNo is the Group number column, Data is the data column, Group is the designated group for searching, and DecimalNumber is the number entered for searching. 请注意, Key是键列, GroupNo是组号列, Data是数据列, Group是要搜索的指定组, DecimalNumber是输入的要搜索的编号。

EDIT: As discussed in comments below, this formula can be made much more readable by using a named range (AKA named formula). 编辑:如下面的注释中所述,可以通过使用命名范围(又名命名公式)使此公式更易读。 Set a named range searchRange equal to: 将命名范围searchRange设置为等于:

IFERROR(ABS(IF(GroupNo=Group,1," ")*Key-DecimalValue)," ")

Then the formula becomes: 然后公式变为:

=INDEX(Data,MATCH(MIN(searchRange),searchRange,0))

This has the added benefit of less Excel overhead, since the named formula only gets calculated once (whereas in the other version, it is calculated every time it appears). 这具有减少Excel开销的额外好处,因为命名公式只计算一次(而在另一个版本中,每次出现时都会计算一次)。

You should be able to do as follows 您应该可以执行以下操作

Function getLastRow()
    Dim i As Integer
    Dim l_row As Integer
    For i = 1 To 35
        If Sheet1.Cells(Rows.Count, i).End(xlUp).Row > l_row Then
            l_row = Sheet1.Cells(Rows.Count, i).End(xlUp).Row
        End If
    Next i
    getLastRow = l_row
End Function
Sub data_lookup
    Dim last_row As Integer
    Dim lcell as Range
    Dim col_a_lookup As Double
    Dim col_b_lookup AS Double
    Dim row_collection As New Collection
    Dim variance AS Double
    Dim closest_row AS Integer

    col_b_lookup = 0.04
    col_a_lookup = 8
    variance = 50

    last_row = getLastRow
    'Find All the Cells that match your lookup value for column B
    For Each lcell in Sheet1.Range("$B$2", "$B$" & last_row)
       If lcell.value = col_b_lookup Then
          row_collection.Add lcell
       End If
    Next lcell
    'Loop through the collection created above to find the closest absolute value to
    'your lookup value for Column A 
    For Each lcell in row_collection
        If Abs(Sheet1.Cells(lcell.row,"A") - col_a_lookup) < variance then
            variance = Abs(Sheet1.Cells(lcell.row,"A") - col_a_lookup)
            closest_row = lcell.row
        End If
    Next lcell
    'Return Results 
    If closest_row > 0 Then
        Msgbox "Closest Data: " & Sheet1.Cells(closest_row,"G")
    Else
        Msgbox "Cannot Locate"
    End If
End Sub

Obviously you will have to set col_a_lookup and col_b_lookup to the values specified and I am sure you want to change the Msgbox. 显然,您必须将col_a_lookupcol_b_lookup设置为指定的值,并且我确定要更改Msgbox。 But this should help you on your way. 但这应该对您有所帮助。

暂无
暂无

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

相关问题 Excel:仅考虑具有给定值的单元格-递归公式 - Excel: Consider only cells with given value - Recursive formula excel中的总和如何仅针对给定值 - How sum in excel only for given value 我在 excel 中添加的列对所有行只有一个值 - the column that i have added in excel have only one value for all rows 在 Excel 中按行生成随机(“是”,“否”)并使 10% 的随机行具有值“是”,并将 rest 设置为“否”值 - Generate Random (“Yes”,“No”) by row in Excel and make 10% of random rows have value “Yes” and the rest as “No” value 需要使范围内的公式仅作用于u行中具有给定值的行 - Need to make formula across a range act on only rows with a given value in row u 如何仅获取 EXCEL 工作表中具有值的那些列 - How to get only those colums in a EXCEL worksheet that have a value Excel:对一组行中的值取平均值,但仅当它们在列中具有特定值时才包括它们 - Excel: Averaging the values from a set of rows, but only include them if they have a specific value in a column 如何在 excel 中查找仅与一个特定字段重复的所有行中的给定条件匹配的行 - How to find rows in excel that that only match a given criteria across all rows where one specific field has duplicates 仅当 Excel 的另一列中存在给定值时如何替换值 - How to replace a value only when a given value is present in another column in Excel 如何使 Excel 只读 - How to make Excel read only
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM