1

这是我要完成的工作,我有两张纸:

参考表: 点击查看图片

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

解决方案表:

点击查看解决方案示例

Length    Width   Height  Returning Code   Match_L   Match_W   Match_H

 10        6       8         C                12        7.4        5

“返回代码”列中的公式应在相应的参考表中查找最接近的值,即长度 <-> 长度、宽度 <-> 宽度、高度 <-> 高度,并从相应行返回匹配的“代码”。

如果我想在值相等时匹配它会更简单,但在我的情况下,它将在每个对应列中查找最接近的值(更大或更低)并返回匹配的“代码”和Match_L、Match_W、Match_H 列中的值。

非常感谢任何帮助或指示!

4

2 回答 2

1

假设只有一个地方可以输入所需的长度、宽度和高度,因此最大返回值只有一个:

在您的参考表中,在 E 到 G 中再添加三列length_difwidth_difheight_dif

这些列的公式将在单元格 E2 中:=ABS(B2-SolutionSheet!A$2)然后将其扩展到 G2 并将其绘制到解决方案表的末尾。

在 H: 的参考表中添加另一列,dif_abs其公式为:=Sum(E2:G2)

然后要返回您的值,请在单元格 D2 的 SolutionSheet 中添加以下公式:=Index(ReferenceSheet!$A$2:$H$9;MATCH(Min(ReferenceSheet!$H$2:$H$9);ReferenceSheet!$H$2:$H$9);1)

于 2017-08-15T08:28:03.730 回答
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

要使此宏工作,您需要根据这些列的排列在工作表上有数据:

在此处输入图像描述

在我的工作表中,我已设置在单元格中的值更改事件上运行此宏H2

于 2017-08-15T08:36:15.460 回答