遵循 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
。