|
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim searchValue As Variant
Dim searchRange As Range
Dim matchRange As Range
Dim resultRange As Range
Dim match As Range
' 检查是否点击了B1:B15范围内的单元格
If Not Intersect(Target, Me.Range("B1:B15")) Is Nothing Then
' 设置查找值、查找范围、匹配范围、结果范围
searchValue = Target.Value
Set searchRange = ThisWorkbook.Worksheets("Sheet2").Range("A1:A20")
Set matchRange = ThisWorkbook.Worksheets("Sheet2").Range("A1:B20")
Set resultRange = ThisWorkbook.Worksheets("Sheet2").Range("B1:B20")
' 在查找范围内查找匹配的值
Set match = searchRange.Find(What:=searchValue, LookIn:=xlValues, LookAt:=xlWhole)
' 如果找到匹配的值,显示相应的结果范围内容并弹出文本框
If Not match Is Nothing Then
Dim matchRow As Long
matchRow = match.Row
Dim result As String
result = resultRange.Cells(matchRow, 1).Value
MsgBox "匹配的信息是: " & result
End If
End If
End Sub |
|