[英]How to make Excel consider ONLY rows that have a given value
Here is an image, followed by description of data 这是一张图片,后面是数据说明
Description of Columns: 列说明:
Inputs (in column F) 输入(在F列中)
DecimalValue
, in F5 DecimalValue
其DecimalValue
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
。
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_lookup
和col_b_lookup
设置为指定的值,并且我确定要更改Msgbox。 But this should help you on your way. 但这应该对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.