EDIT:切换回Find()以匹配行。。。。
您可以使用字典执行以下操作:
Private Sub id_Change()
Dim id As Variant, headers As Object, ws As Worksheet, f As range
id = Me.id.Value
Set ws = ThisWorkbook.Sheets("Sheet1")
Set headers = AllHeaders(ws, 1) 'get column headers from first row
Set f = ws.Columns(headers("ID")).Find(id, Lookat:=xlWhole, _
Lookin:=xlValues)
If Not f Is Nothing Then
With f.EntireRow
desc1.Value = .Cells(headers("Description 1"))
desc2.Value = .Cells(headers("Description 2"))
desc3.Value = .Cells(headers("Description 3"))
desc4.Value = .Cells(headers("Description 4"))
End With
Else
desc1.Value = ""
desc2.Value = ""
desc3.Value = ""
desc4.Value = ""
End If
End Sub
'Return a Dictionary mapping all headers on row `rw` of sheet `ws`
' to their column positions. Assumes all headers are unique.
Function AllHeaders(ws As Worksheet, rw As Long) As Object
Dim dict As Object, v As String, c As Range
Set dict = CreateObject("scripting.dictionary")
dict.comparemode = 1 'vbTextCompare: case-insensitive
For Each c In ws.Range(ws.Cells(rw, 1), ws.Cells(rw, Columns.Count).End(xlToLeft)).Cells
v = c.Value
If Len(v) > 0 Then dict.Add v, c.Column 'map headers to column number
Next c
Set AllHeaders = dict
End Function