here is some code that you might start with. You can't activate an event in Excel on a hover or a double click, but you can capture a selection change. This code opens a reference workbook the first time a specific sheet is actuivated, and then wherever the selection is changed in that sheet, it does the lookup on the reference table and display the message. Note that there are a number of assumptions abou the structure of the reference workbook and the location of the unit numbers.
Put this code in VBA on the sheet object that you want the lookup to apply to. See if it gets close to what you want and we will go from there.
Option Explicit
' Global reference sheet name
Dim ReferenceSheet As Worksheet
' the name of the reference sheet to use
Const ReferenceBookName As String = "Reference.xls"
Dim ReferenceBook As Workbook
' make sure the reference workbook is open
Private Sub Worksheet_Activate()
' if the reference book has not yet been opened, then open it and address the reference sheet
If (ReferenceBook Is Nothing) Then
On Error Resume Next
Set ReferenceBook = Workbooks.Open(Filename:=ReferenceBookName, ReadOnly:=True)
If (Err.Number = 0) Then
Set ReferenceSheet = ReferenceBook.Worksheets(1) ' assumes the reference sheet is sheet1
Else ' can't open the reference book
Set ReferenceSheet = Nothing
End If
On Error GoTo 0
End If
' reactive this sheet
Me.Activate
End Sub
' produces a popup message containing data from a table
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' skip this is there is no reference sheet
If (ReferenceSheet Is Nothing) Then
Exit Sub
End If
' assumes that the lookup is to occur on values in column A
' select only column A and ignore the header row
If (Target.Column = 1 And Target.Row > 1) Then
' lookup the entry in the lookup table which is located in the on the reference sheet
On Error Resume Next
Dim theRow As Long
' assume the reference table is in column A
theRow = Application.WorksheetFunction.Match(Target.Value, ReferenceSheet.Range("A:A"), 0)
If (Err.Number = 0) Then
'get the data from the rest of the row
Dim theType As String
Dim theYear As String
Dim theStatus As String
theType = ReferenceSheet.Cells(theRow, "B").Value
theYear = ReferenceSheet.Cells(theRow, "C").Value
theStatus = ReferenceSheet.Cells(theRow, "D").Value
MsgBox "Unit = " & Target.Value & vbCr & _
"Type = " & theType & vbCr & _
"Year = " & theYear & vbCr & _
"Status = " & theStatus
End If
On Error GoTo 0
End If
End Sub
Bookmarks