多列查找和下一列中的 return 对应值(最接近的值)
Multiple Column lookup and return corresponding value in next column (nearest value)
这是我想要完成的,我有两张纸:
参考资料Sheet:
Click to see the image
Code Length Width Height
A 78 48 25
B 78 48 34
C 12 7.4 5
D 12 15 5
E 12 15 7.5
F 12 15 9
G 24 15 5
H 24 15 7
解决方案Sheet:
Length Width Height Returning Code Match_L Match_W Match_H
10 6 8 C 12 7.4 5
“返回代码”列中的公式应在相应的参考 Sheet 中查找最接近的值,即长度 <-> 长度、宽度 <-> 宽度、高度 <-> 高度和 return 相应行中匹配的“代码”。
如果我想在值相等时匹配它会更简单,但在我的例子中,它将在每个相应的列中寻找最接近的值(更大或更小)并且 return 匹配的“代码”和 Match_L、Match_W、Match_H 列中的值。
非常感谢任何帮助或指点!
假设只有一个地方可以输入所需的长度、宽度和高度,因此只有一个 returned 最大值:
在您的参考资料中 sheet 在 E 到 G 中再添加三列:length_dif
、width_dif
和 height_dif
。
这些列的公式将在单元格 E2 中:=ABS(B2-SolutionSheet!A)
然后将其扩展到 G2 并向下绘制直到解决方案结束 table。
在 H: dif_abs
中的引用 sheet 中添加另一列,公式为:=Sum(E2:G2)
然后到 return 您的值在单元格 D2 的 SolutionSheet 中添加以下公式:=Index(ReferenceSheet!$A:$H;MATCH(Min(ReferenceSheet!$H:$H);ReferenceSheet!$H:$H);1)
以下 VBA 将完成这项工作。
Sub LookupNearestValue()
Dim ws As Worksheet: Set ws = Worksheets("Sheet1")
Dim LastRow As Long: LastRow = ws.UsedRange.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Dim i As Long, RowCounter As Long: RowCounter = 2
Dim tRowCounter As Long
Dim tValue As Long
Dim tempValue As Long
Dim tLength As Long, tWidth As Long, tHeight As Long
Dim tempLength As Long, tempWidth As Long, tempHeight As Long
tLength = ws.Cells(2, 6)
tWidth = ws.Cells(2, 7).Value
tHeight = ws.Cells(2, 8).Value
With ws
For i = 2 To LastRow
tempLength = ws.Cells(RowCounter, 2)
tempWidth = ws.Cells(RowCounter, 3).Value
tempHeight = ws.Cells(RowCounter, 4).Value
tempValue = Abs(tLength - tempLength) + Abs(tWidth - tempWidth) + Abs(tHeight - tempHeight)
If RowCounter = 2 Then
tValue = tempValue
tRowCounter = RowCounter
ElseIf RowCounter > 2 And tempValue < tValue Then
tValue = tempValue
tRowCounter = RowCounter
End If
RowCounter = RowCounter + 1
Next i
ws.Cells(2, 9) = ws.Cells(tRowCounter, 1)
ws.Cells(2, 10) = ws.Cells(tRowCounter, 2)
ws.Cells(2, 11) = ws.Cells(tRowCounter, 3).Value
ws.Cells(2, 12) = ws.Cells(tRowCounter, 4).Value
End With
End Sub
要使此宏起作用,您需要根据这些列排列在 sheet 上获得数据:
在我的 sheet 中,我已将此宏设置为 运行 H2
单元格中的值更改事件。
这是我想要完成的,我有两张纸:
参考资料Sheet: Click to see the image
Code Length Width Height A 78 48 25 B 78 48 34 C 12 7.4 5 D 12 15 5 E 12 15 7.5 F 12 15 9 G 24 15 5 H 24 15 7
解决方案Sheet:
Length Width Height Returning Code Match_L Match_W Match_H 10 6 8 C 12 7.4 5
“返回代码”列中的公式应在相应的参考 Sheet 中查找最接近的值,即长度 <-> 长度、宽度 <-> 宽度、高度 <-> 高度和 return 相应行中匹配的“代码”。
如果我想在值相等时匹配它会更简单,但在我的例子中,它将在每个相应的列中寻找最接近的值(更大或更小)并且 return 匹配的“代码”和 Match_L、Match_W、Match_H 列中的值。
非常感谢任何帮助或指点!
假设只有一个地方可以输入所需的长度、宽度和高度,因此只有一个 returned 最大值:
在您的参考资料中 sheet 在 E 到 G 中再添加三列:length_dif
、width_dif
和 height_dif
。
这些列的公式将在单元格 E2 中:=ABS(B2-SolutionSheet!A)
然后将其扩展到 G2 并向下绘制直到解决方案结束 table。
在 H: dif_abs
中的引用 sheet 中添加另一列,公式为:=Sum(E2:G2)
然后到 return 您的值在单元格 D2 的 SolutionSheet 中添加以下公式:=Index(ReferenceSheet!$A:$H;MATCH(Min(ReferenceSheet!$H:$H);ReferenceSheet!$H:$H);1)
以下 VBA 将完成这项工作。
Sub LookupNearestValue()
Dim ws As Worksheet: Set ws = Worksheets("Sheet1")
Dim LastRow As Long: LastRow = ws.UsedRange.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Dim i As Long, RowCounter As Long: RowCounter = 2
Dim tRowCounter As Long
Dim tValue As Long
Dim tempValue As Long
Dim tLength As Long, tWidth As Long, tHeight As Long
Dim tempLength As Long, tempWidth As Long, tempHeight As Long
tLength = ws.Cells(2, 6)
tWidth = ws.Cells(2, 7).Value
tHeight = ws.Cells(2, 8).Value
With ws
For i = 2 To LastRow
tempLength = ws.Cells(RowCounter, 2)
tempWidth = ws.Cells(RowCounter, 3).Value
tempHeight = ws.Cells(RowCounter, 4).Value
tempValue = Abs(tLength - tempLength) + Abs(tWidth - tempWidth) + Abs(tHeight - tempHeight)
If RowCounter = 2 Then
tValue = tempValue
tRowCounter = RowCounter
ElseIf RowCounter > 2 And tempValue < tValue Then
tValue = tempValue
tRowCounter = RowCounter
End If
RowCounter = RowCounter + 1
Next i
ws.Cells(2, 9) = ws.Cells(tRowCounter, 1)
ws.Cells(2, 10) = ws.Cells(tRowCounter, 2)
ws.Cells(2, 11) = ws.Cells(tRowCounter, 3).Value
ws.Cells(2, 12) = ws.Cells(tRowCounter, 4).Value
End With
End Sub
要使此宏起作用,您需要根据这些列排列在 sheet 上获得数据:
在我的 sheet 中,我已将此宏设置为 运行 H2
单元格中的值更改事件。